Electronic Dice

Aim

To make a simple Electronic Dice.

Components required for the experiment

Arduino UNO Board, Seven Segment Display, Push Button Switch and Two Resistors (220 Ohms).

What is Dice and where it is used?

A dice is a small cube with each of its six faces marked with a different number of dots or pips from one to six. It is typically used as a random number generator in games or activities where chance or luck is a factor. Dice can be made from various materials such as plastic, wood, or bone, and can be found in different shapes and sizes. They are commonly used in board games, tabletop games, gambling games, and as teaching aids in mathematics and probability.

Circuit Diagram

To practice, make the circuit diagram with the exact pin-to-pin connection as shown below. This will help you to complete the experiment successfully.

Code

int pinLedA = 4;    //pin A of 7-segment LED is connected to pin 4 of Arduino
int pinLedB = 5;    //pin B of 7-segment LED is connected to pin 5 of Arduino
int pinLedC = 6;    //pin C of 7-segment LED is connected to pin 6 of Arduino
int pinLedD = 7;    //pin D of 7-segment LED is connected to pin 7 of Arduino
int pinLedE = 8;    //pin E of 7-segment LED is connected to pin 8 of Arduino
int pinLedF = 3;    //pin F of 7-segment LED is connected to pin 3 of Arduino
int pinLedG = 2;    //pin G of 7-segment LED is connected to pin 2 of Arduino
int buttonPin = 12; //push button is connected to pin 12 of Arduino  
int buttonState;    //create a variable for push button to check the input
long ran;           //variable to store the random number created by Arduino
int time = 500;     //defined time as 500 to be used for delay purpose
void setup()
{
pinMode (pinLedA, OUTPUT);  //define the A,B,C,D,E,F,G pins of the 7-segment LED as OUTPUT
pinMode (pinLedB, OUTPUT);
pinMode (pinLedC, OUTPUT);
pinMode (pinLedD, OUTPUT);
pinMode (pinLedE, OUTPUT);
pinMode (pinLedF, OUTPUT);
pinMode (pinLedG, OUTPUT);  
pinMode (buttonPin, INPUT);  //setting the push button connected to Arduino as INPUT
randomSeed(analogRead(0));
}
void loop()
{
buttonState = digitalRead(buttonPin);  //digitalRead function is used to check if it HIGH or LOW, and store the value in 						buttonState variable
if (buttonState == HIGH)
{					// if the buttonState variable is HIGH, the Arduino generates random numbers from 1 to 7
ran = random(1, 7);
if (ran == 1)
{					//This line of code is executed if the selected number is 1. The number is displayed on the 7-segemnt LED for 500 millisecond
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedC, HIGH);
delay (time);
}
if (ran == 2)                       //This line of code is executed if the selected number is 2. The number is displayed on the 7-segemnt LED for 500 millisecond.
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedG, HIGH);
digitalWrite (pinLedE, HIGH);
digitalWrite (pinLedD, HIGH);
delay (time);
}
if (ran == 3)                       //This line of code is executed if the selected number is 3. The number is displayed on the 7-segemnt LED for 500 millisecond.
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedD, HIGH);
digitalWrite (pinLedG, HIGH);
delay (time);
}
if (ran == 4)                       //This line of code is executed if the selected number is 4. The number is displayed on the 7-segemnt LED for 500 millisecond.
{
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedF, HIGH);
digitalWrite (pinLedG, HIGH);
delay (time);
}
if (ran == 5)                       //This line of code is executed if the selected number is 5. The number is displayed on the 7-segemnt LED for 500 millisecond.
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedF, HIGH);
digitalWrite (pinLedG, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedD, HIGH);
delay (time);
}
if (ran == 6)                         //This line of code is executed if the selected number is 6. The number is displayed on the 7-segemnt LED for 500 millisecond.
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedD, HIGH);
digitalWrite (pinLedE, HIGH);
digitalWrite (pinLedF, HIGH);
digitalWrite (pinLedG, HIGH);
delay (time);
}
}
digitalWrite (pinLedA, LOW);
digitalWrite (pinLedB, LOW);
digitalWrite (pinLedC, LOW);
digitalWrite (pinLedD, LOW);
digitalWrite (pinLedE, LOW);
digitalWrite (pinLedF, LOW);
digitalWrite (pinLedG, LOW);
}     //After showing the randomly selected number on 7-segment LED for 500 millisecond, the LED is turned off untill the next time the push button is pressed and the system generates another random number ranging from 1 to 6.

 

Code Explained

int pinLedA = 4;
int pinLedB = 5;
int pinLedC = 6;
int pinLedD = 7;
int pinLedE = 8;
int pinLedF = 3;
int pinLedG = 2;

These lines of code shows the connection between 7-segment LED and Arduino Uno. The 7 different segments of the LED is connected to Arduino pins. 

There is one more pin connection on 7-segment LED, i.e. DP pin,  used to display the DOT on the LED, but we are not going to use it in this experiment.

int buttonPin = 12;
int buttonState;

The push button is connected to pin 12 of Arduino Uno. The variable buttonState is created  to check the value  of the push button when it is pressed.

long ran;
int time = 500;

When the Arduino Uno selects any random number from 1 to 6, it is stored in long ran variable.

We can also declare the delay time before writing the loop section of the code. We have taken 500 as time which we will be using later in the delay code.

void setup()
{
pinMode (pinLedA, OUTPUT);
pinMode (pinLedB, OUTPUT);
pinMode (pinLedC, OUTPUT);
pinMode (pinLedD, OUTPUT);
pinMode (pinLedE, OUTPUT);
pinMode (pinLedF, OUTPUT);
pinMode (pinLedG, OUTPUT);
pinMode (buttonPin, INPUT);
randomSeed(analogRead(0));
}

The setup functions declares all the LED pins connected to Arduino Uno as OUTPUT pins and the push button connection as INPUT pin. 

The randomSeed function is used to generate an array of random numbers. The analogRead is required to generate the random number.

void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
ran = random(1, 7);
if (ran == 1)
{
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedC, HIGH);
delay (time);
}
if (ran == 2)
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedG, HIGH);
digitalWrite (pinLedE, HIGH);
digitalWrite (pinLedD, HIGH);
delay (time);
}
if (ran == 3)
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedD, HIGH);
digitalWrite (pinLedG, HIGH);
delay (time);
}
if (ran == 4)
{
digitalWrite (pinLedB, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedF, HIGH);
digitalWrite (pinLedG, HIGH);
delay (time);
}
if (ran == 5)
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedF, HIGH);
digitalWrite (pinLedG, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedD, HIGH);
delay (time);
}
if (ran == 6)
{
digitalWrite (pinLedA, HIGH);
digitalWrite (pinLedC, HIGH);
digitalWrite (pinLedD, HIGH);
digitalWrite (pinLedE, HIGH);
digitalWrite (pinLedF, HIGH);
digitalWrite (pinLedG, HIGH);
delay (time);
}
}
digitalWrite (pinLedA, LOW);
digitalWrite (pinLedB, LOW);
digitalWrite (pinLedC, LOW);
digitalWrite (pinLedD, LOW);
digitalWrite (pinLedE, LOW);
digitalWrite (pinLedF, LOW);
digitalWrite (pinLedG, LOW);
}

The "loop" function is another built-in function that runs repeatedly as long as the Arduino board has power.

These lines of code first checks the button state if it is pressed or not. When the push button is pressed each time, the Arduino analog pin generates a random number ranging from 1 to 6 and depending on the number, 7-segment LED displays the same number. Each time after displaying the number, all the 7 segments of the LED are made low to show no value on the LED.

Output

  1. Press the start simulation button in the interface.

  2. Move the mouse cursor to the center of the push button.

  3. Press the push button and then release it, as if you have pressed the push button.

  4. As a result, the simulator will generate a random number between 1 and 6.

  5. The generated number will be displayed on the seven segment display.

 

Conclusion

The Seven Segment Display displays a random number between 1 to 6 when the Pushbutton is pressed.

Assignment

Change the code to print every value on serial monitor displayed on the 7-segment LED.