I am creating a three-person reaction time game but I would like to be able to read all three buttons' proximity to the start time - currently only the winner is displayed on the serial read.
Here is my code:
int ledPin = 12;
int buttonPin1 = 7;
int buttonPin2 = 6;
int buttonPin3 = 5;
void setup() {
Serial.begin(9600);
Serial.println("Setup start");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
randomSeed(analogRead(A0));
Serial.println("Setup complete");
}
void loop() {
Serial.println("Game start");
digitalWrite(ledPin, HIGH);
delay(2000);
delay(random(1000, 3000));
digitalWrite(ledPin, LOW);
Serial.println("LED off, waiting for button press");
unsigned long start_time = millis();
int winner = 0;
while (winner == 0) {
winner = check_buttons();
}
unsigned long end_time = millis();
unsigned long total_time = end_time - start_time;
Serial.print("Player ");
Serial.print(winner);
Serial.println(" wins!");
Serial.print("It took ");
Serial.print(total_time);
Serial.println(" ms to press the button.");
delay(5000);
}
int check_buttons() {
int player1 = digitalRead(buttonPin1);
int player2 = digitalRead(buttonPin2);
int player3 = digitalRead(buttonPin3);
if (player1 == LOW) {
return 1;
} else if (player2 == LOW) {
return 2;
} else if (player3 == LOW) {
return 3;
} else {
return 0;
}
}
while (second == 0) ...?