Arduino Code for Project

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;
}
}
}
}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

  • Do not use delay(...) in your sketches.
    delay(...) stops/blocks all code execution during the delay period.

  • When you want a TIMER, use the millis( ) function as mentioned here:



  • Also, do not acted on switch levels, instead use changes in switch state.

  • Avoid using while( ) as it can block code execution.

Your code seems to work. What do you see is not working?
(1) How is the game played?
(2) How is the game won?
(3) How is the game lost?
(4) How is the game restarted?

Add these things in a welcome() function.

I can press the buttons. "1" does not react very well. "2" does not get noticed until "1" releases too early. All the LEDs light, and the buzzer beeps.

Is this part of your problem?

int LEDR = 10; // Red Led pin 11
.
.
int Button1 = 11; // Player 1 Button Pin 10

Are you working with this person?

who seems to have the similar problems with similar code.

a7

I had not seen the other thread, nor had a response from this thread. Their "Joined" time is an hour apart. Hmm.

Two near identical topics:

Both closed pending an explanation from the OPs.