Another Arduino project for newbies

Arduino board with LED

Electronic devices  love accuracy. If you give them the same task twice, you will get the same resault. But sometimes  you want some random result . This can be used in games. You can create your own lottery game. We will help you to do that kind of game with this Arduino project.

Begginer Arduino project : How to create LED Dice

It can  also be  used in other situations. Like a “random walk,” where a robot makes a random turn, then walks  a random distance or until it hits some object. It is more natural movement if robot moves this way. You newer know what it will do next. It might be very funny.

The Arduino library has a algorithm for generating random numbers. There are two versions of the algorithm random. It can either take only one argument (max) or two  arguments (min and max). In first case the minimum is assumed to be zero. Be careful, because the max argument can be misleading, as the biggest number you can get back is the max minus 1. So, the this line will give x a value of  1 to 6:

int x = random(1, 7);

So we will use this Arduino algorithm for our project. We will need this components:

One Arduino Diecimila or Duemilanove board or clone
23 of D1-7 Standard red LEDs
6 R1-7 270  0.5W metal film resistors
48 S1 Miniature push-to-make switches
13 R8 100K  0.5W metal film resistors

Next step is to connect this components by the schematics below. You can build the circuit by connecting the parts directly or you can use the breadboard. Using breadboard is a better way because it looks better and orderly.Arduino project LED schematics

Even though a dice can only have a max of six dots, we will need seven diodes to have the of a dot in the middle for one, three and five rolls. It is a dot in the middle of the area.

After you created the circuit, next step is to program the Arduino board. You can program the board by using the Arduino software provided in the board package. Here is the software. You can copy/paste it, but it is not complex, the best way it to write it by yourself and when you have a problem, you can look  below.

int ledPins[7] = {2, 3, 4, 5, 6, 7, 8};
int dicePatterns[7][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 1
{0, 0, 1, 1, 0, 0, 0}, // 2
{0, 0, 1, 1, 0, 0, 1}, // 3
{1, 0, 1, 1, 0, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 1, 1, 1, 1, 1, 0}, // 6
{0, 0, 0, 0, 0, 0, 0} // BLANK
};
int switchPin = 9;
int blank = 6;
void setup()
{
for (int i = 0; i < 7; i++)
{
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
randomSeed(analogRead(0));
}
void loop()
{

if (digitalRead(switchPin))
{
rollTheDice();
}
delay(100);
}
void rollTheDice()
{
int result = 0;
int lengthOfRoll = random(15, 25);
for (int i = 0; i < lengthOfRoll; i++)
{
result = random(0, 6); // result will be 0 to 5 not 1 to 6
show(result);
delay(50 + i * 10);
}
for (int j = 0; j < 3; j++)
{
show(blank);
delay(500);
show(result);
delay(500);
}
}
void show(int result)
{
for (int i = 0; i < 7; i++)
{
digitalWrite(ledPins[i], dicePatterns[result][i]);
}
}

We  have 7 diodes to initialize in the method, so it is good putting them in an array and have a loop over the array to initialize each pin. There is also call to randomSeed in the method. If we did not have this, each time we reset the Arduino, we would have the same sequence of lights.

The dicePatterns array manage the diodes, which LEDs should be enabled and witch one disabled for a particular case. When we need to display a result of the dice, we need to loop over the array for the particular throw, setting each LED accordingly. This Arduino project is very simple, we encourage you to try to build it by yourself. We are confident that you are succeed without our help.

ElectronicKits.com
Responses are currently closed, but you can trackback from your own site.

Comments are closed.