Arduino Project: Random Electronic Dice

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(){

randomSeed(analogRead(0));

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