Change button animations

This commit is contained in:
Andreas committed 2026-04-13 16:58:57 +02:00
1 parent bf27305563
commit 08e4f6c5cc
3 files changed
+65 -77

No files matched your search

+55 -68
View File
@@ -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);
@@ -138,7 +135,10 @@ void onButtonPressed(int buttonIdx) {
lastInputTime = millis();
AnswerType type = getAnswerType(question, answer);
int prevTemp = tempSections;
selectedAnswer[question] = answer;
recalculateTemp(); // tempSections now holds the new value
// --- DEBUG ---
Serial.println("----------------------------");
@@ -150,16 +150,9 @@ void onButtonPressed(int buttonIdx) {
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(":");
@@ -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;
// Show all affected LEDs in color at once
for (int led = ledStart; led < ledEnd; led++) setMirroredLED(led, color);
FastLED.show();
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);
}
// 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);
} 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(onMs);
for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB::Black);
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);
}
}
+10 -9
View File
@@ -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
BIN
View File
Binary file not shown.