diff --git a/ExploringUncertanties.ino b/ExploringUncertanties.ino index dddcc26..a28a72b 100644 --- a/ExploringUncertanties.ino +++ b/ExploringUncertanties.ino @@ -55,7 +55,6 @@ 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 INACTIVITY_TIMEOUT = 300000; // 5 minutes in ms — auto-reset after this long with no input // ===================== GAME STATE ===================== @@ -76,9 +75,7 @@ void onButtonPressed(int buttonIdx); void recalculateTemp(); bool checkWin(); void displayTemperature(); -void animateTempChange(int fromSections, int toSections); -void playAnswerAnimation(AnswerType type); -void blinkAll(CRGB color, int times, int onMs); +void playTempAnimation(int fromSections, int toSections, CRGB color); void playWinAnimation(); void setMirroredLED(int i, CRGB color); CRGB getThermometerColor(int i); @@ -137,8 +134,11 @@ void onButtonPressed(int buttonIdx) { lastInputTime = millis(); - AnswerType type = getAnswerType(question, answer); + AnswerType type = getAnswerType(question, answer); + int prevTemp = tempSections; + selectedAnswer[question] = answer; + recalculateTemp(); // tempSections now holds the new value // --- DEBUG --- Serial.println("----------------------------"); @@ -146,20 +146,13 @@ void onButtonPressed(int buttonIdx) { 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("[RESULT] CORRECT"); break; - case ANSWER_HALF: Serial.println("[RESULT] HALF-CORRECT"); break; - case ANSWER_WRONG: Serial.println("[RESULT] 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(":"); @@ -167,8 +160,8 @@ void onButtonPressed(int buttonIdx) { Serial.print("-"); } else { switch (getAnswerType(q, selectedAnswer[q])) { - case ANSWER_CORRECT: Serial.print("OK"); break; - case ANSWER_HALF: Serial.print("HALF"); break; + case ANSWER_CORRECT: Serial.print("OK"); break; + case ANSWER_HALF: Serial.print("HALF"); break; case ANSWER_WRONG: Serial.print("WRONG"); break; } } @@ -177,7 +170,12 @@ void onButtonPressed(int buttonIdx) { Serial.println(); // --- END DEBUG --- - animateTempChange(prevTemp, tempSections); + switch (type) { + case ANSWER_CORRECT: playTempAnimation(prevTemp, tempSections, CRGB(0, 180, 0)); break; + case ANSWER_HALF: playTempAnimation(prevTemp, tempSections, CRGB(255, 180, 0)); break; + case ANSWER_WRONG: playTempAnimation(prevTemp, tempSections, CRGB(180, 0, 0)); break; + } + displayTemperature(); if (checkWin()) { @@ -292,69 +290,58 @@ void displayTemperature() { FastLED.show(); } -// Animates the thermometer rising or falling one section at a time. -void animateTempChange(int fromSections, int toSections) { - if (fromSections == toSections) return; - - bool goingUp = (toSections > fromSections); - int current = fromSections; - - while (current != toSections) { - int next = goingUp ? current + 1 : current - 1; - int ledStart = START_LED + min(current, next) * LEDS_PER_SECTION; - int ledEnd = START_LED + max(current, next) * LEDS_PER_SECTION; // exclusive - - if (goingUp) { - for (int led = ledStart; led < ledEnd; led++) { - CRGB color = getThermometerColor(led); - color.nscale8(BRIGHT_BRIGHTNESS); - setMirroredLED(led, color); - FastLED.show(); - delay(ANIM_STEP_MS); - } - } else { - for (int led = ledEnd - 1; led >= ledStart; led--) { - CRGB color = getThermometerColor(led); - color.nscale8(DIM_BRIGHTNESS); - setMirroredLED(led, color); - FastLED.show(); - delay(ANIM_STEP_MS); - } - } - - current = next; - } -} // ===================== ANSWER FEEDBACK ANIMATIONS ===================== -// Plays the appropriate feedback animation for the given answer type, -// then restores the thermometer display. -void playAnswerAnimation(AnswerType type) { - switch (type) { - case ANSWER_CORRECT: - blinkAll(CRGB(0, 180, 0), 3, 60); // green blink × 3 - break; - case ANSWER_HALF: - blinkAll(CRGB(255, 120, 0), 3, 60); // amber blink × 3 - break; - case ANSWER_WRONG: - blinkAll(CRGB(180, 0, 0), 3, 60); // red blink × 3 - break; - } - // Restore thermometer after animation - displayTemperature(); -} +// Feedback animation for a button press. +// fromSections = temp before, toSections = temp after. +// +// Correct (drop): all affected LEDs appear instantly in color, then vanish top→bottom. +// Wrong/half (rise): LEDs added one by one bottom→top in color, then stay put +// (displayTemperature() corrects colors immediately after). +// Re-press (no change): section at boundary flashes instantly, then restores. +void playTempAnimation(int fromSections, int toSections, CRGB color) { + if (toSections < fromSections) { + // ---- CORRECT: temperature drops ---- + int ledStart = START_LED + toSections * LEDS_PER_SECTION; + int ledEnd = START_LED + fromSections * LEDS_PER_SECTION; -// Blinks every LED with color for `times` flashes, each `onMs` long. -void blinkAll(CRGB color, int times, int onMs) { - for (int t = 0; t < times; t++) { - for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, color); + // Show all affected LEDs in color at once + for (int led = ledStart; led < ledEnd; led++) setMirroredLED(led, color); FastLED.show(); - delay(onMs); - for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB::Black); + delay(200); + + // Remove one by one, top to bottom + for (int led = ledEnd - 1; led >= ledStart; led--) { + CRGB c = getThermometerColor(led); + c.nscale8(DIM_BRIGHTNESS); + setMirroredLED(led, c); + FastLED.show(); + delay(60); + } + + } else if (toSections > fromSections) { + // ---- WRONG / HALF: temperature rises ---- + int ledStart = START_LED + fromSections * LEDS_PER_SECTION; + int ledEnd = START_LED + toSections * LEDS_PER_SECTION; + + // Add one by one, bottom to top + for (int led = ledStart; led < ledEnd; led++) { + setMirroredLED(led, color); + FastLED.show(); + delay(60); + } + delay(150); + + } else { + // ---- RE-PRESS: no temperature change ---- + int section = constrain(fromSections, 0, NUM_SECTIONS - 1); + int ledStart = START_LED + section * LEDS_PER_SECTION; + int ledEnd = ledStart + LEDS_PER_SECTION; + + for (int led = ledStart; led < ledEnd; led++) setMirroredLED(led, color); FastLED.show(); - delay(onMs / 2); + delay(300); } } diff --git a/README.md b/README.md index 2e6d552..e83d61d 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,11 @@ All buttons wired with `INPUT_PULLUP`: connect each button between its pin and G | Button type | LED feedback | Temperature | |---|---|---| -| Correct | Blinks **green** | −1 section | -| Half-correct | Glows **orange/yellow** | +1 section | -| Wrong | Blinks **red** | +1 section | +| Correct | Affected sections light up **green** bottom→top, then fade out | −1 section (−2 if switching from wrong) | +| Half-correct | Affected sections light up **yellow** bottom→top, then fade out | +1 section | +| Wrong | Affected sections light up **red** bottom→top, then fade out | +1 section | + +The animation always plays on every button press, even if the temperature doesn't change (re-press). If switching from a wrong answer to correct, 14 LEDs (2 sections) are animated. - 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 → game automatically resets and starts over. @@ -164,13 +166,12 @@ Each value (0, 1, or 2) is the button index within that question's group. The re ### Feedback animations -| Type | Animation | Temp effect | -|---|---|---| -| `ANSWER_CORRECT` | Green blink × 3 | −1 section | -| `ANSWER_HALF` | Orange/yellow steady glow (700 ms) | +1 section | -| `ANSWER_WRONG` | Red blink × 3 | +1 section | +All three types share `playTempAnimation(fromSections, toSections, color)`: +- **Dropping** (correct): animates sections `toSections..fromSections-1` in green +- **Rising** (wrong/half): animates sections `fromSections..toSections-1` in red/yellow +- **Re-press** (no change): animates one section at the current boundary for feedback -After any feedback animation, the thermometer is immediately redrawn before the slide animation runs. +The preview temp is computed before state is updated, so the animation always targets the right LEDs. ### Win animation diff --git a/fragen/all.pdf b/fragen/all.pdf index 90290df..df67544 100644 Binary files a/fragen/all.pdf and b/fragen/all.pdf differ