From bf27305563b635df5659952ad9e2d55950e5e3f5 Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Mon, 13 Apr 2026 16:42:49 +0200 Subject: [PATCH] First implementation with flashes for feedback --- ExploringUncertanties.ino | 109 +++++++++++++++++++++++--------------- README.md | 5 +- 2 files changed, 70 insertions(+), 44 deletions(-) diff --git a/ExploringUncertanties.ino b/ExploringUncertanties.ino index 08ed714..dddcc26 100644 --- a/ExploringUncertanties.ino +++ b/ExploringUncertanties.ino @@ -54,13 +54,15 @@ const uint8_t DIM_BRIGHTNESS = 12; // inactive (still visible for the gradi const uint8_t BRIGHT_BRIGHTNESS = 200; // active (lit) // Timing -const unsigned long DEBOUNCE_MS = 50; -const int ANIM_STEP_MS = 40; // ms between each LED step during temp animation +const unsigned long DEBOUNCE_MS = 50; +const int ANIM_STEP_MS = 40; // ms between each LED step during temp animation +const unsigned long INACTIVITY_TIMEOUT = 300000; // 5 minutes in ms — auto-reset after this long with no input // ===================== GAME STATE ===================== -int selectedAnswer[NUM_QUESTIONS]; // -1 = no answer, 0/1/2 = button within group -int tempSections; // current number of lit sections (0–8) -bool gameWon; +int selectedAnswer[NUM_QUESTIONS]; // -1 = no answer, 0/1/2 = button within group +int tempSections; // current number of lit sections (0–8) +bool gameWon; +unsigned long lastInputTime; // millis() of the last button press // ===================== DEBOUNCE STATE ===================== bool buttonCurrentState[12]; @@ -77,7 +79,6 @@ void displayTemperature(); void animateTempChange(int fromSections, int toSections); void playAnswerAnimation(AnswerType type); void blinkAll(CRGB color, int times, int onMs); -void flashAll(CRGB color, int durationMs); void playWinAnimation(); void setMirroredLED(int i, CRGB color); CRGB getThermometerColor(int i); @@ -87,7 +88,7 @@ void fadeAllLEDs(); void setup() { Serial.begin(9600); - FastLED.addLeds(leds, NUM_LEDS); + FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(84); for (int i = 0; i < 12; i++) { @@ -104,13 +105,15 @@ void setup() { // ===================== MAIN LOOP ===================== void loop() { if (!gameWon) { - checkButtons(); - } else { - // Continuously play the win animation until reset is pressed - playWinAnimation(); - if (digitalRead(RESET_PIN) == LOW) { + if (millis() - lastInputTime >= INACTIVITY_TIMEOUT) { + Serial.println("[TIMEOUT] No input for 5 minutes — resetting."); resetGame(); + } else { + checkButtons(); } + } else { + playWinAnimation(); + resetGame(); } } @@ -120,8 +123,9 @@ void resetGame() { for (int q = 0; q < NUM_QUESTIONS; q++) { selectedAnswer[q] = -1; } - tempSections = BASELINE_SECTIONS; - gameWon = false; + tempSections = BASELINE_SECTIONS; + gameWon = false; + lastInputTime = millis(); displayTemperature(); Serial.println("Game reset. Temperature at baseline (4 sections)."); } @@ -131,28 +135,54 @@ void onButtonPressed(int buttonIdx) { int question = buttonIdx / BUTTONS_PER_QUESTION; int answer = buttonIdx % BUTTONS_PER_QUESTION; - Serial.print("Q"); Serial.print(question + 1); - Serial.print(" Button "); Serial.print(answer + 1); + lastInputTime = millis(); AnswerType type = getAnswerType(question, answer); selectedAnswer[question] = answer; + // --- DEBUG --- + Serial.println("----------------------------"); + Serial.print("[BUTTON] Q"); Serial.print(question + 1); + Serial.print(" / Button "); Serial.print(answer + 1); + Serial.print(" (pin D"); Serial.print(BUTTON_PINS[buttonIdx]); Serial.println(")"); switch (type) { - case ANSWER_CORRECT: Serial.println(" → CORRECT"); break; - case ANSWER_HALF: Serial.println(" → HALF-CORRECT"); break; - case ANSWER_WRONG: Serial.println(" → WRONG"); break; + case ANSWER_CORRECT: Serial.println("[RESULT] CORRECT"); break; + case ANSWER_HALF: Serial.println("[RESULT] HALF-CORRECT"); break; + case ANSWER_WRONG: Serial.println("[RESULT] WRONG"); break; } playAnswerAnimation(type); int prevTemp = tempSections; recalculateTemp(); + + Serial.print("[TEMP] "); Serial.print(prevTemp); + Serial.print(" → "); Serial.print(tempSections); + Serial.println(" sections"); + + Serial.print("[STATE] "); + for (int q = 0; q < NUM_QUESTIONS; q++) { + Serial.print("Q"); Serial.print(q + 1); Serial.print(":"); + if (selectedAnswer[q] == -1) { + Serial.print("-"); + } else { + switch (getAnswerType(q, selectedAnswer[q])) { + case ANSWER_CORRECT: Serial.print("OK"); break; + case ANSWER_HALF: Serial.print("HALF"); break; + case ANSWER_WRONG: Serial.print("WRONG"); break; + } + } + if (q < NUM_QUESTIONS - 1) Serial.print(" "); + } + Serial.println(); + // --- END DEBUG --- + animateTempChange(prevTemp, tempSections); displayTemperature(); if (checkWin()) { gameWon = true; - Serial.println("ALL CORRECT – You win!"); + Serial.println("[WIN] ALL CORRECT – You win!"); } } @@ -303,13 +333,13 @@ void animateTempChange(int fromSections, int toSections) { void playAnswerAnimation(AnswerType type) { switch (type) { case ANSWER_CORRECT: - blinkAll(CRGB(0, 180, 0), 3, 150); // green blink × 3 + blinkAll(CRGB(0, 180, 0), 3, 60); // green blink × 3 break; case ANSWER_HALF: - flashAll(CRGB(200, 80, 0), 700); // orange/yellow steady glow + blinkAll(CRGB(255, 120, 0), 3, 60); // amber blink × 3 break; case ANSWER_WRONG: - blinkAll(CRGB(180, 0, 0), 3, 150); // red blink × 3 + blinkAll(CRGB(180, 0, 0), 3, 60); // red blink × 3 break; } // Restore thermometer after animation @@ -328,32 +358,27 @@ void blinkAll(CRGB color, int times, int onMs) { } } -// Lights every LED with color for `durationMs` milliseconds, then turns off. -void flashAll(CRGB color, int durationMs) { - for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, color); - FastLED.show(); - delay(durationMs); - for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB::Black); - FastLED.show(); -} // ===================== WIN ANIMATION ===================== // Rainbow sweep that plays continuously while the game is in the won state. +// Plays 3 rainbow sweeps up and down the thermometer, then returns. void playWinAnimation() { static uint8_t hue = 0; - for (int i = 0; i < BASE_LEDS; i++) { - setMirroredLED(i, CRGB(CHSV(hue++, 255, 255))); - FastLED.show(); - fadeAllLEDs(); - delay(10); - } - for (int i = BASE_LEDS - 1; i >= 0; i--) { - setMirroredLED(i, CRGB(CHSV(hue++, 255, 255))); - FastLED.show(); - fadeAllLEDs(); - delay(10); + for (int rep = 0; rep < 3; rep++) { + for (int i = 0; i < BASE_LEDS; i++) { + setMirroredLED(i, CRGB(CHSV(hue++, 255, 255))); + FastLED.show(); + fadeAllLEDs(); + delay(10); + } + for (int i = BASE_LEDS - 1; i >= 0; i--) { + setMirroredLED(i, CRGB(CHSV(hue++, 255, 255))); + FastLED.show(); + fadeAllLEDs(); + delay(10); + } } } diff --git a/README.md b/README.md index 59164d2..2e6d552 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,9 @@ All buttons wired with `INPUT_PULLUP`: connect each button between its pin and G | Wrong | Blinks **red** | +1 section | - You can change an answer at any time by pressing a different button in the same group. -- **Win condition**: all 4 correct answers selected → thermometer reaches 0 → rainbow win animation plays. +- **Win condition**: all 4 correct answers selected → thermometer reaches 0 → rainbow win animation plays → game automatically resets and starts over. - Press the **reset button** (A1) at any time to reset all answers and return the thermometer to the midpoint. +- If no button is pressed for **5 minutes** (`INACTIVITY_TIMEOUT`), the game resets automatically. --- @@ -182,4 +183,4 @@ FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(84); ``` -Note: the strip is WS2812 (not WS2812B) with `RGB` color order — this matches `test.ino`. If colors look wrong, try `GRB`. +Note: the strip is WS2812 (not WS2812B) with `GRB` color order.