Solution Mini Project I
Design an electronic dice using an Arduino. Circuit will look like:
Step1: Use pushbutton to start the dice roll. When push button is pressed generate a random
number and store it in some variable. You can choose any digital pin to connect pushbutton.
Step2: Use random function to generate a random number between 1 and 6. random(1,7)
generates a random number between 1 and 6.
Step3: Based on the number generated in Step 2 you can use if..else if…else statement to turn
on the LED connected to the digital pins. You can choose any six digital pin to connect the LED.
int kled1 = 3; //this is where 1st LED is connected
int kled2 = 4; //this is where 2nd LED is connected
int kled3 = 5; //this is where 3rd LED is connected
int kled4 = 6; //this is where 4th LED is connected
int kled5 = 7; //this is where 5th LED is connected
int kled6 = 8; //this is where 6th LED is connected
int kpushbutton = 9; //this is where pushbutton is connected
void setup(){
pinMode(kled1, OUTPUT); //for 1st LED
pinMode(kled2, OUTPUT); //for 2nd LED
pinMode(kled3, OUTPUT); //for 3rd LED
pinMode(kled4, OUTPUT); //for 4th LED
pinMode(kled5, OUTPUT); //for 5th LED
pinMode(kled6, OUTPUT); //for 6th LED
pinMode(kpushbutton, INPUT); //for pushbutton
[Link](9600);
[Link]("Dice is ready to roll!!!");
}
void loop(){
randomSeed(millis()); //helps in generating new random number every time
you start
char inputValue = digitalRead(kpushbutton); //read the status of the
pushbutton and store in inputValue
char diceRollValue = 0; //variable to hold the value of dice when rolled
if(inputValue == HIGH)
{
diceRollValue = random(1,7); //generate a random value for a dice
}
else{
diceRollValue = 0; //0 indicates dice is not rolled and turn off all
the LED
}
if(diceRollValue == 1) //when rolled 1 turn on the 1st LED
{
digitalWrite(kled1, HIGH);
delay(5000); //wait for 5 sec to start a new dice roll
}
else if(diceRollValue == 2) //when rolled 2 turn on the 1st, 2nd LED
{
digitalWrite(kled1, HIGH);
digitalWrite(kled2, HIGH);
delay(5000); //wait for 5 sec to start a new dice roll
}
else if(diceRollValue == 3) //when rolled 3 turn on the 1st, 2nd, 3rd
LED
{
digitalWrite(kled1, HIGH);
digitalWrite(kled2, HIGH);
digitalWrite(kled3, HIGH);
delay(5000); //wait for 5 sec to start a new dice roll
}
else if(diceRollValue == 4) //when rolled 4 turn on the 1st, 2nd, 3rd,
4th LED
{
digitalWrite(kled1, HIGH);
digitalWrite(kled2, HIGH);
digitalWrite(kled3, HIGH);
digitalWrite(kled4, HIGH);
delay(5000); //wait for 5 sec to start a new dice roll
}
else if(diceRollValue == 5) //when rolled 5 turn on the 1st, 2nd, 3rd,
4th, 5th LED
{
digitalWrite(kled1, HIGH);
digitalWrite(kled2, HIGH);
digitalWrite(kled3, HIGH);
digitalWrite(kled4, HIGH);
digitalWrite(kled5, HIGH);
delay(5000); //wait for 5 sec to start a new dice roll
}
else if(diceRollValue == 6) //when rolled 6 turn on the 1st, 2nd, 3rd,
4th, 5th, 6th LED
{
digitalWrite(kled1, HIGH);
digitalWrite(kled2, HIGH);
digitalWrite(kled3, HIGH);
digitalWrite(kled4, HIGH);
digitalWrite(kled5, HIGH);
digitalWrite(kled6, HIGH);
delay(5000); //wait for 5 sec to start a new dice roll
}
else //any other condition will turn off all the LEDs
{
digitalWrite(kled1, LOW);
digitalWrite(kled2, LOW);
digitalWrite(kled3, LOW);
digitalWrite(kled4, LOW);
digitalWrite(kled5, LOW);
digitalWrite(kled6, LOW);
}