/* Grade 8: reaction timer - classic ESP32 only. Teacher must confirm the exact development board and its labelled GPIOs. GPIO25 -> 330 ohm resistor -> LED anode (+); LED cathode (-) -> GND. GPIO27 -> normally-open push button -> GND (INPUT_PULLUP). USB powers the development board. Never connect 5 V to a GPIO. Serial Monitor: 115200 baud. No Wi-Fi, extra library or screen required. Release the button. After it is stably released, wait for the LED. Press as soon as it lights. The next round starts automatically. The first OBSERVED raw falling edge is timed; no press-debounce delay is added. Release is debounced between rounds. Polling, millis() granularity and board scheduling still limit accuracy: this is not a laboratory timer. */ #include constexpr uint8_t LED_PIN = 25; constexpr uint8_t BUTTON_PIN = 27; constexpr uint32_t WAIT_MIN_MS = 2000; constexpr uint32_t WAIT_MAX_MS = 5000; static_assert(WAIT_MIN_MS <= WAIT_MAX_MS && WAIT_MAX_MS < 60000, "Keep minimum <= maximum and maximum below 60000 ms"); constexpr uint32_t RELEASE_STABLE_MS = 40; constexpr uint32_t ROUND_PAUSE_MS = 1000; constexpr uint32_t RESPONSE_TIMEOUT_MS = 5000; enum class State { WAIT_FOR_RELEASE, RANDOM_WAIT, WAIT_FOR_PRESS }; State state = State::WAIT_FOR_RELEASE; bool wasPressed = false; bool checkingRelease = false; uint32_t releaseStartedAt = 0; uint32_t lastRoundEndedAt = 0; uint32_t waitStartedAt = 0; uint32_t waitDuration = 0; uint32_t ledStartedAt = 0; uint32_t roundNumber = 0; // Unsigned subtraction stays correct across a millis() rollover. bool elapsed(uint32_t now, uint32_t since, uint32_t duration) { return uint32_t(now - since) >= duration; } void finishRound(const char *result, uint32_t now, uint32_t reactionMs = 0) { digitalWrite(LED_PIN, LOW); state = State::WAIT_FOR_RELEASE; // Latch this result: ignore further bounce. checkingRelease = false; lastRoundEndedAt = now; Serial.print("Round "); Serial.print(roundNumber); Serial.print(": "); Serial.print(result); if (result[0] == 'O') { // Only "OK" includes a reaction measurement. Serial.print("; reaction_ms="); Serial.print(reactionMs); } Serial.println(". Release the button for the next round."); } void setup() { pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); pinMode(BUTTON_PIN, INPUT_PULLUP); Serial.begin(115200); wasPressed = (digitalRead(BUTTON_PIN) == LOW); lastRoundEndedAt = millis(); Serial.println("REACTION TIMER: release; wait for LED; then press."); Serial.println("Early presses are FALSE_START. No press within 5 s is TIMEOUT."); } void loop() { // Read and timestamp BEFORE doing any printing or other slow work. const bool pressed = (digitalRead(BUTTON_PIN) == LOW); const uint32_t now = millis(); const bool pressEdge = pressed && !wasPressed; wasPressed = pressed; switch (state) { case State::WAIT_FOR_RELEASE: if (pressed) { checkingRelease = false; } else { if (!checkingRelease) { checkingRelease = true; releaseStartedAt = now; } if (elapsed(now, releaseStartedAt, RELEASE_STABLE_MS) && elapsed(now, lastRoundEndedAt, ROUND_PAUSE_MS)) { ++roundNumber; // Upper bound is exclusive: possible waits are 2000..5000 ms. // Arduino-ESP32 supplies random(); no Wi-Fi is started here. waitDuration = uint32_t(random(long(WAIT_MIN_MS), long(WAIT_MAX_MS) + 1L)); Serial.print("Round "); Serial.print(roundNumber); Serial.println(": ready - watch the LED, not the Serial Monitor."); waitStartedAt = millis(); state = State::RANDOM_WAIT; } } break; case State::RANDOM_WAIT: // Check the button FIRST. A press seen before LED-on is an early press, // even if the random wait also expires on this loop iteration. if (pressed) { finishRound("FALSE_START", now); } else if (elapsed(now, waitStartedAt, waitDuration)) { digitalWrite(LED_PIN, HIGH); ledStartedAt = millis(); state = State::WAIT_FOR_PRESS; // Do NOT print or delay in the timed response window. } break; case State::WAIT_FOR_PRESS: if (elapsed(now, ledStartedAt, RESPONSE_TIMEOUT_MS)) { finishRound("TIMEOUT", now); } else if (pressEdge) { const uint32_t reactionMs = uint32_t(now - ledStartedAt); finishRound("OK", now, reactionMs); } break; } }