/*
Wokwi | general
dumnewbie - Thursday, March 26, 2026 9:20 PM
Holy im clueless
2 player game checking reaction time of each
Creator: Anurag Saha
https://raw.githubusercontent.com/sahaanurag256-cmyk/My-Asic-Journey/refs/heads/main/arduino/code/Reaction_tester.ino
*/
const int BTN_1_PIN = 12; // 1st player button
const int BTN_2_PIN = 10; // 2nd player button
const int LED_PIN = 11;
bool isRunning = false;
bool isStarted = false;
int player1score = 0; //initial score 0 for scoreboard
int player2score = 0;
uint32_t startTime = 0;
uint32_t stopTime = 0;
uint32_t randomTime = 0;
uint32_t reactionTime = 0;
void showResult(int player, int score, uint32_t reactTime) {
Serial.print("Player ");
Serial.print(player);
Serial.print(" wins! Score: ");
Serial.println(score);
Serial.print("Reaction time: ");
Serial.print(reactTime);
Serial.println(" ms\n");
digitalWrite(LED_PIN, LOW);
isRunning = false;
delay(2000);
isStarted = false;
}
void setup() {
Serial.begin(115200);
pinMode(BTN_1_PIN, INPUT_PULLUP);
pinMode(BTN_2_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
randomSeed(analogRead(0)); // so random delay time changes each turn by getting electrical noise as its a floating pin
Serial.println("Reaction Competition\n");
}
void loop() {
if (!isStarted) {
Serial.println("Wait for the LED to glow ... first clicker WINS!!!");
randomTime = millis() + random(0, 5000);
isStarted = true;
}
if (millis() >= randomTime && !isRunning) {
digitalWrite(LED_PIN, HIGH);
startTime = millis();
isRunning = true;
}
if (digitalRead(BTN_1_PIN) == LOW && isRunning) {
stopTime = millis();
reactionTime = stopTime - startTime;
player1score++;
showResult(1, player1score, reactionTime);
}
if (digitalRead(BTN_2_PIN) == LOW && isRunning) {
stopTime = millis();
reactionTime = stopTime - startTime;
player2score++;
showResult(2, player2score, reactionTime);
}
}