The COVID - 19 lock-down is really a bummer for us all so, while the world is digitalizing why can't the old fashioned dice do too? So here we go..
This project works with a push of a button (Literally XD). It uses a random generator to generate a random number from 1 to 6.
Here we need first to initialize the pins for the LEDs and the PushButton. Also to check whether the button is being pressed or not we will create a variable buttonState.
int led1 = 2;
int led2 = 3;
int led3 = 4; //Initializing the leds from pins 2 to 7
int led4 = 5;
int led5 = 6;
int led6 = 7;
int button = 11; //Initializing the button to pin 11
int buttonState = 0;
Next we need to set the LEDs to output and the Button to Input to make receive input. Also we need to check whether the button is working or not so we will start the Serial monitor.
void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}
Next we need to continuously loop the following code, but how do we do that? Thankfully Arduino has a void loop()function which constantly loops the program. As a dice roll is completely random, we can use the random function in Arduino. We need also need to delay or else there will be too many rolls, and we don't want anything like that, do we? In my code I have written delay(10000) as it is perfect for me. I recommend you to change to your own values as it might be different for everyone. The dice roll will be displayed on the Serial monitor as well as the button state which will be 0 if not pressed else it will be 1 if it is pressed.
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(button);
Serial.println(buttonState);
int roll = random(1, 7);
Serial.println(roll);
if(buttonState == 1){
if(roll == 1){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
delay(10000);
}
else if(roll == 2){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
delay(10000);
}
else if(roll == 3){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
delay(10000);
}
else if(roll == 4){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, LOW);
delay(10000);
}
else if(roll == 5){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, LOW);
delay(10000);
}
else if(roll == 6){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
delay(10000);
}
}
else{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
}
}
Comments