Arduino Project: Random Electronic Dice

547

3

1

Posted

Introduction: Arduino Project: Random Electronic Dice

Hello!

This project was built for an assignment for my college.

It was born from my interest in D&D and the use of dice in that roleplaying game.

It is an electronic die, which randomly chooses a type of die (having the options of a 4, 6, 8, 10 or 12 sided-die) and then randomly chooses a number on that die.

Step 1: Gathering Materials

For this project, you need

• An Arduino Uno with USB cable
• 12 LED-lights (any colour)
• 25 wires (preferably
• A soldering prototyping board or a solderless breadboard
• One 560 Ω resistor

You can get all of these items in a normal Arduino starting kit.

Step 2: Connecting the Wires

Putting this project together is really simple.

Each of your LEDs has to be connected to a node on the Arduino through a wire. The given schematic only has six LEDs, but this can easily be expanded to twelve by using up to the twelfth digital pin on the Arduino.

Another set of wires leads away from the LEDs, and comes together at the resistor. If you're using a solderless breadboard, it'll be enough to put them all on the - strip on the side; if you're soldering the thing, you'll have to connect the wires through solder.

Another wire leads from the resistor to the Ground pin on the Arduino.

Step 3: Writing the Code

This is the code I used;

void setup()
{

for ( int z = 1 ; z < 13 ; z++ ) // LEDs on pins 1-12 are output

{

pinMode(z, OUTPUT);

}

}

void randomLED(int del, int d) //int del indicates the delay, int d indicates which type of die is/how many LEDs are being used

{

int r;

if (d == 1){

r = random(1, 5); //get a random number from 1-4

}

if (d == 2){

r = random(1, 7); //get a random number from 1-6

}

if (d == 3){

r = random(1, 9); //get a random number from 1-8

}

if (d == 4){

r = random(1, 11); //get a random number from 1-10

}

if (d == 5){

r = random(1, 13); //get a random number from 1-12

}

digitalWrite(r, HIGH); //output to the LED matching the randomly generated number

if (del > 0)

{

delay(del); //hold the LED for the amount of delay that is given

}

else if (del == 0)

{

do //with 0 delay given, hold the LED forever

{}

while (1);

}

digitalWrite(r, LOW); //turn off the LED

}

void loop()

{

int a; //cycle through LEDs to simulate the rolling of the die

int d = random(1, 6); //choose how many sides you're using (see the if-statements above)

for ( a = 0 ; a < 50 ; a++ ) //cycle through 50 LEDs

{

randomLED(50, d); //delay is 50 milliseconds, int d has been chosen earlier in the void loop

}

for ( a = 1 ; a <= 5 ; a++ ) //cycle through 5 LEDs

{

randomLED(a * 100, d); //delay is 100 milliseconds

}

randomLED(0, d); //display the definite LED

}

Step 4: Go Further!

You now have a working randomly generating electronic dice!

You can build further on this idea by, for example, making it possible to choose which die you're using. Or you can build a beautiful case for it to shine through.

Recommendations

• Arduino Class

68,172 Enrolled

• Pocket-Sized Contest

We have a be nice policy.