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.