/*
Arduino | general-help
Nono - January 31, 2026 2:06 PM
I do a game of reaction
*/
const unsigned long SPEED = 150; // lower value is faster
const int NUM_LEDS = 5;
const int LED_PINS[NUM_LEDS] = {12, 11, 10, 9, 8};
const int BTN_PIN = 7;
int count = 0;
int increment = 1;
unsigned long prevTime = 0;
void checkResult() {
if ((count - increment) == 2) {
Serial.println("Winner!");
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PINS[2], HIGH);
delay(250);
digitalWrite(LED_PINS[2], LOW);
delay(250);
}
} else {
Serial.println("Loser!");
delay(2000);
}
count = 0;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
pinMode (BTN_PIN, INPUT_PULLUP);
}
void loop() {
int stop = !digitalRead(BTN_PIN);
if (stop) checkResult();
//Serial.println(count);
if (millis() - prevTime >= SPEED) {
prevTime = millis();
// turn all LEDs off
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED_PINS[i], LOW);
}
// turn next LED on
digitalWrite(LED_PINS[count], HIGH);
count = count + increment;
if (count <= 0) increment = 1;
if (count >= (NUM_LEDS - 1)) increment = -1;
}
}