Project 1.11 Reaction Time Game
Project 1.11 Reaction Time Game We have now covered enough basic concepts to create a game. It pits two players against each other to see who can react first by pressing their button (SW1 or SW2) when the RGB LED changes from red to green. We also check to make sure neither player faults by pressing their button too soon. The single LED on each player’s side will flash along with the green channel of the RGB LED for the winning player. The RGB LED will flash red if a player faults, along with the single LED on the player’s side. We’ll look at the code and explain it afterwards.
Concepts: multiple inputs, random function
Circuits:
Concepts: multiple inputs, random function
Circuits:
At the beginning of the loop() block, the red channel of the RGB LED is turned on and a timer is set to wait before turning green:
now = millis();
wait = now + random(3000,6000);
while(millis() < wait && digitalRead(SW1)==HIGH && digitalRead(SW2)==HIGH){
}
The random function returns a value between the first and second argument passed to the function. The variable wait holds the value that millis() must reach before the RGB LED turns green. The while loop pauses the execution of the sketch until either enough time has passed or until a player presses their button (i.e., SW1 or SW2 is no longer HIGH).
If while loop ends with one of the player’s buttons pressed, we record a fault:
if(digitalRead(SW1)==LOW)fault = LED2;
if(digitalRead(SW2)==LOW)fault = LED3;
digitalWrite(redLED,LOW);
If no fault has occurred, the RGB LED changes to green and the sketch waits for a player to press their button:
if(fault == 0){
digitalWrite(greenLED,HIGH);
//wait until a player presses a button
while(digitalRead(SW1)==HIGH &&
digitalRead(SW2)==HIGH){
}
Once a button is pressed, we determine a winner and blink the correct LED 10 times along with the green RGB channel:
//find which player won
if(digitalRead(SW1)==LOW){
winner = LED2;
}
else{
winner = LED3;
}
for(int k = 0; k < 10; k++){
digitalWrite(greenLED,HIGH);
digitalWrite(winner,HIGH);
delay(50);
digitalWrite(winner,LOW);
digitalWrite(greenLED,LOW);
delay(50);
}
}
We now return to the else block that is executed if a fault occurred:
else{
//show that someone faulted and start over
for(int k = 0; k < 10; k++){
digitalWrite(redLED,HIGH);
digitalWrite(fault,HIGH);
delay(50);
digitalWrite(redLED,LOW);
digitalWrite(fault,LOW);
delay(50);
}
}
now = millis();
wait = now + random(3000,6000);
while(millis() < wait && digitalRead(SW1)==HIGH && digitalRead(SW2)==HIGH){
}
The random function returns a value between the first and second argument passed to the function. The variable wait holds the value that millis() must reach before the RGB LED turns green. The while loop pauses the execution of the sketch until either enough time has passed or until a player presses their button (i.e., SW1 or SW2 is no longer HIGH).
If while loop ends with one of the player’s buttons pressed, we record a fault:
if(digitalRead(SW1)==LOW)fault = LED2;
if(digitalRead(SW2)==LOW)fault = LED3;
digitalWrite(redLED,LOW);
If no fault has occurred, the RGB LED changes to green and the sketch waits for a player to press their button:
if(fault == 0){
digitalWrite(greenLED,HIGH);
//wait until a player presses a button
while(digitalRead(SW1)==HIGH &&
digitalRead(SW2)==HIGH){
}
Once a button is pressed, we determine a winner and blink the correct LED 10 times along with the green RGB channel:
//find which player won
if(digitalRead(SW1)==LOW){
winner = LED2;
}
else{
winner = LED3;
}
for(int k = 0; k < 10; k++){
digitalWrite(greenLED,HIGH);
digitalWrite(winner,HIGH);
delay(50);
digitalWrite(winner,LOW);
digitalWrite(greenLED,LOW);
delay(50);
}
}
We now return to the else block that is executed if a fault occurred:
else{
//show that someone faulted and start over
for(int k = 0; k < 10; k++){
digitalWrite(redLED,HIGH);
digitalWrite(fault,HIGH);
delay(50);
digitalWrite(redLED,LOW);
digitalWrite(fault,LOW);
delay(50);
}
}