I am trying to implement another button to my reaction time game, but the code is not working as the game seems to work while still pressing one button. Additionally, it seems that the determination of the winning player is not working. Any advice would be super helpful! Thank you!
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int LEDR = 10; // Red Led pin 11
int LEDY1 = 9; // First Set Yellow LED Pin 9
int LEDY2 = 8; // Second Set Yellow LED Pin 8
int LEDY3 = 7; // Third Set Yellow LED Pin 7
int LEDGr = 6; // Green Led pin 6
int Button1 = 11; // Player 1 Button Pin 10
int Button2 = 12;
int Beep;
int PSE; // Variable pause
int TME; // Time
int RTME1 = 0; // Reaction Time for Button 1
int RTME2 = 0;
void setup() {
lcd.init();
lcd.backlight();
pinMode(LEDR, OUTPUT);
pinMode(LEDY1, OUTPUT);
pinMode(LEDY2, OUTPUT);
pinMode(LEDY3, OUTPUT);
pinMode(LEDGr, OUTPUT);
pinMode(Button1, INPUT);
digitalWrite(LEDR, HIGH); // All lights turn on in a sequence
delay(100);
digitalWrite(LEDY1, HIGH);
delay(100);
digitalWrite(LEDY2, HIGH);
delay(100);
digitalWrite(LEDY3, HIGH);
delay(100);
digitalWrite(LEDGr, HIGH);
}
void loop() {
lcd.clear(); // Clear screen.
lcd.print("Hold Button 1");
lcd.setCursor(0, 1); // Move to the second line.
lcd.print("to Start.");
while (digitalRead(Button1) == LOW) // Button 1 must be pressed to start
{
tone(13, 1200, 30);
delay(1400);
noTone(13);
}
if (digitalRead(Button1) == HIGH)
{
lcd.clear();
digitalWrite(LEDR, HIGH); // Switch on Red; turn off other lights
digitalWrite(LEDY1, LOW);
digitalWrite(LEDY2, LOW);
digitalWrite(LEDY3, LOW);
digitalWrite(LEDGr, LOW);
delay(random(100, 3000));
Beep = random(1, 4); // random beep
digitalWrite(LEDY1, HIGH); // LEDs light in sequence with random time
delay(random(100,3000));
if (digitalRead(Button1) == LOW){
lcd.print("Released too");
lcd.setCursor(0, 1); // Move to the second line.
lcd.print("soon");
tone(13, 3000, 1500);
delay(5000);
noTone(13);
return;
}
digitalWrite(LEDY2, HIGH);
delay(random(100,3000));
if (digitalRead(Button1) == LOW){
lcd.print("Released too");
lcd.setCursor(0, 1); // Move to the second line.
lcd.print("soon");
tone(13, 3000, 1500);
delay(5000);
noTone(13);
return;
}
digitalWrite(LEDY3, HIGH);
delay(random(100,3000));
if (digitalRead(Button1) == LOW){
lcd.print("Released too");
lcd.setCursor(0, 1); // Move to the second line.
lcd.print("soon");
tone(13, 3000, 1500);
delay(5000);
noTone(13);
return;
}
digitalWrite(LEDGr, HIGH);
}
TME = millis();
while (digitalRead(LEDGr) == HIGH) {
while (digitalRead(Button1) == LOW || digitalRead(Button2) == LOW) {
if (digitalRead(Button1) == LOW) {
RTME1 = millis() - TME; // Reaction time for Button 1
}
if (digitalRead(Button2) == LOW) {
RTME2 = millis() - TME; // Reaction time for Button 2
}
if (RTME1 > 0 && RTME2 > 0) {
if (RTME1 < RTME2) {
lcd.clear();
lcd.print("Player 1 wins!");
delay(5000);
return;
} else if (RTME1 > RTME2) {
lcd.clear();
lcd.print("Player 2 wins!");
delay(5000);
return;
} else {
lcd.clear();
lcd.print("Tie");
delay(5000);
return;
}
}
}
}
}