Holiday

Search Site

Dice Project using Arduino

Building the circuit:

 

 

To build this circuit you will need:

Ω      One Arduino (preferably Uno)

Ω      Seven LED’s

Ω      One Proto-Shield (and headers)

Ω      Seven 120Ω resistors

Ω      One tactile switch

Ω      One Battery pack (4 AA’s)

Ω      One DC male connector

Ω      Some hook-up wire

Overview/How it works:

This circuit works using a micro-controller – in this case the Arduino Uno to control a series of LED’s and recreate the faces of a dice. Because the micro-controller has a function inside it that allows it to pick a number truly randomly we can use this to create the random-ness of a dice! We will use a push switch to ‘roll’ the dice.

When attaching a puch switch to a micro-controller, we must make sure that it is not ‘floating’. When a wire is not connected to either a voltage source or ground, electro-magnetic waves in the air can induce current into them. When a micro-controller is reading the state of that wire, it can sometimes think it is on when it is off, or off when it is on. To avoid this, we use a pull-up of pull-down resistor on the wire. This is a resistor that connects the wire to either ground, or voltage, ensuring it is not ‘floating’. The pull-down or pull-up resistor must be quite a high value resistor so that when the button is pressed and the state of the wire changes, only a small amount of voltage is lost (or gained) through the resistor.

In our circuit, we will use the pull-up resistors that are already embedded in the micro-controller. The advantage of this is that we do not need to wire this ourselves, but the dissadvantage is that we must attach the button to ground instead of voltage (so the wire will actually flip states when we puch it) and we must make sure in our code that when we read in the state of the button ‘LOW’ means pressed, and ‘HIGH’ means not pressed.

Building the project:

Start by soldering the headers on to the proto-shield: ( photographs open in a new window)

*photo1*

Next solder the push button on as shown, with a wire going to the point next to each diagonal of the switch. One should go to pin 0, and one to Ground:

**photo2**

On the underside of the board join the solder joints for these together so as to connect them:

***photo3***

Next solder in the 120Ω resistors as shown, from pins 1-4 and 10-12 to spaces making an even ‘H’ shape where the resistors enter the middle section of the board:

****photo4****

Next solder the green LED’s next to each of these, with the long leg next to the resistors:

*****photo5*****

On the underside of the board, join together the hole with the long leg of the LED’s and the hole with the resistors together:

******photo6******

Then use solder to make a track to join all the grounds together:

*******photo7*******

 Next take the leads from the battery pack and attach the red one to the smaller of the two connectors on the DC male jack, make sure to slide some heat shrink over the red wire so you can heat shrink it later:

********photo8********

Then heat shrink the red wire to make it insulated from the black one:

*********photo9*********

Next attach the black wire to the other connection on the DC jack

**********photo10**********

Finally heat shrink the wires and attach the plastic back onto the DC barrel jack

***********photo11***********

The code:

The code for this project is as follows (cut and paste):

#define button 0

 

int numbers[6][6] = {

  {4, 4, 4, 4, 4, 4},   //Table of the LED's required to create each number

  {3, 10, 3, 3, 3, 3},

  {1, 4, 12, 1, 1, 1},

  {1, 3, 10, 12, 1, 1},

  {1, 3, 4, 10, 12, 1},

  {1, 2, 3, 10, 11, 12}

};

 

void setup()             //Setup function - Runs once

{

  pinMode(0, INPUT);     //Set the pin the button is connected to as an input

  digitalWrite(0, HIGH); //Enables the internal pullup resistor

  pinMode(1, OUTPUT);    //Set the LED's as outputs

  pinMode(2, OUTPUT);

  pinMode(3, OUTPUT);

  pinMode(4, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(11, OUTPUT);

  pinMode(12, OUTPUT);

}

 

void loop()                              //Loop function - repeats

{

  while(digitalRead(button) == HIGH);    //While the button is not pressed - do nothing

  while(digitalRead(button) == LOW)      //When the button is pressed

  {

    rollDice();                          //Roll the dice - Refers to a function defined below

  }

  clearNumber();                         //Turn all the LED's off - refers to a function defined below

  outcome();                             //Display the outcome of the roll - refers to a function defined below

}

 

void clearNumber()                       //Turns all the LED's off

{

  for(int b = 1; b < 13; b++)            //Will repeat 12 times - with b increasing from 1 - 12 each time

  {

    digitalWrite(b, LOW);                //Turns the corresponding LED off

  }

}

 

void outcome()                                //Displays the outcome of the roll

{

  int number = random(0, 6);                  //Sets the variable 'number' to any number between 1 and 6

  for(int i = 0; i < 6; i++)                  //Will repeat 6 times - with i increasing from 0 - 5 each time

  {

    digitalWrite(numbers[number][i], HIGH);   //Refers to the table at the top - turns on the LED that corresponds to the current number

                                              //(number) and the current position in the table (i), this function combined with the for 

                                              //loop will display the number

  }

}

 

void rollDice()                           //Plays the 'rolling' animation for the dice

{

  for(int i = 0; i < 6; i++)              //Will repeat 6 times, with i increasing from 0 - 5 each time

  {

    clearNumber();                        //Turns all the LED's off - refers to the function above

    for(int a = 0; a < 6; a++)            //Will repeat 6 times - with a increasing from 0 - 5

    {

      digitalWrite(numbers[i][a], HIGH);  //Refers to the table at the top - turns on the LED that corresponds to the current number (i) and the

                                          //current position in the table (a), this function combined with the two for loops will repeatedly run

                                          //through all 6 numbers and display them

    }

    delay(80);                            //delay by 80 milli-seconds

  }

}

Back to top