Arduino LED Dice

arduino_dice_2sm_1532890560
arduino_dice_2sm_1532890560arduino_dice_1smdice_4smdice_1smdice_2smdice_3smdice_5smdice_6sm
3.5 5 4 Product


This is a simple dice project based on LEDs, a push-button switch, and an Arduino Uno microcontroller development board. You may build it on a breadboard. The block diagram of the project is shown on Figure1.

Block diagram of the LED Dice
Figure 1. Block diagram of the LED Dice

As shown in Figure 2, the LEDs are organized such that when they turn On, they indicate numbers such as on a real dice. Pressing the switch rolls the Dice. Releasing the switch generates a random number between 1 and 6 which is displayed on the LEDs.

LED Dice
Figure 2. LED Dice

 

Project Hardware

A simplified circuit diagram of the project is shown in Figure 3. Seven LEDs representing the faces of a dice are connected to the I/O pins of an Arduino Uno board. The microcontroller sources the Leds  through  220 ohm current - limiting resistors. Leds A, B, C, D, E, F and G are sourced from I/O pins 6,7,8,9,10,11 and 12 respectively. A push- button switch is connected to the I/O pin 5 which is configured to use an internal pull-up resistor.

Circuit diagram of the LED Dice
Figure 3. Circuit diagram of the LED Dice

 

Project code

The operation of the project is described in Figure 4. At the beginning of the program, I/O pins 7 to 12 are configured as digital outputs and pin 5 is configured as digital intput with an internal pull-up resistor. The program waits until the button is pressed. During the button being hold pressed, the program executes in a loop continuously and increment a variable (named i)  between 1 and 6. In every loop, the current number of the variable is sent to the LEDs and this way the dice rolls. At the time when the button is released, the dice stops rolling and the last value of the variable is displayed until the next button – press. The rolling speed is depended on a delay command, used in the end of the loop.

The process is actually a pseudorandom number generator because during the time when the dice is rolled (the button is pressed) the value of the variable i changes very fast among the numbers between 1 and 6.The random process is actually generated by the random - time used to roll the dice.

The code flow chart of the Arduino LED Dice
Figure 4. The code flow chart of the Arduino LED Dice

 
Below is the code:    

//Simple LED Dice based on Arduino
/*circuitlib.com*/
//By G. Adam

int i;
int button=5;
int A=6;
int B=7;
int C=8;
int D=9;
int E=10;
int F=11;
int G=12;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital IO direction
  pinMode(button, INPUT_PULLUP);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);

  // initialize counter value
  i=1;
  
}

// Turn on LEDs for mode 6
void turn_on_6 ()
 {
    digitalWrite(A,HIGH);
    digitalWrite(B,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(E,HIGH);
    digitalWrite(F,HIGH);
 }

// Turn on LEDs for mode 5
 void turn_on_5 ()
 {
    digitalWrite(A,HIGH);
    digitalWrite(C,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(F,HIGH);
    digitalWrite(G,HIGH);
  }

// Turn on LEDs for mode 4
void turn_on_4 ()
 {  
    digitalWrite(A,HIGH);
    digitalWrite(F,HIGH);
    digitalWrite(D,HIGH);
    digitalWrite(C,HIGH);
  }

// Turn on LEDs for mode 3
void turn_on_3 ()
 {  
    digitalWrite(C,HIGH);
    digitalWrite(G,HIGH);
    digitalWrite(F,HIGH);
 }

// Turn on LEDs for mode 2
void turn_on_2 ()
 {  
    digitalWrite(F,HIGH);
    digitalWrite(C,HIGH);
 }

// Turn on LEDs for mode 1
void turn_on_1 ()
 {  
  digitalWrite(G,HIGH);
 }

// Turn off all LEDs
 void turn_off_all ()
 {  
    digitalWrite(A,LOW);
    digitalWrite(B,LOW);
    digitalWrite(C,LOW);
    digitalWrite(D,LOW);
    digitalWrite(E,LOW);
    digitalWrite(F,LOW);
    digitalWrite(G,LOW);
 }

// the loop function runs over and over again forever
void loop() 
{
  bool state;
  state=digitalRead(button);   
  if (state==LOW) // Button is pressed?
  {
    turn_off_all();
    if (i==1) turn_on_1();
    if (i==2) turn_on_2();      
    if (i==3) turn_on_3();
    if (i==4) turn_on_4();
    if (i==5) turn_on_5();
    if (i==6) turn_on_6();
  }
 
 i=i+1;
 if (i>6) i=1;
 delay(15);
 
 } 

List of the comments:
No comments have been posted yet.
Vote:

Give your advice about this item:

Username:
E-mail: