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
+72 -84

No files matched your search

+62 -75
View File
@@ -55,7 +55,6 @@ const uint8_t BRIGHT_BRIGHTNESS = 200; // active (lit)
// Timing // Timing
const unsigned long DEBOUNCE_MS = 50; 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 const unsigned long INACTIVITY_TIMEOUT = 300000; // 5 minutes in ms — auto-reset after this long with no input
// ===================== GAME STATE ===================== // ===================== GAME STATE =====================
@@ -76,9 +75,7 @@ void onButtonPressed(int buttonIdx);
void recalculateTemp(); void recalculateTemp();
bool checkWin(); bool checkWin();
void displayTemperature(); void displayTemperature();
void animateTempChange(int fromSections, int toSections); void playTempAnimation(int fromSections, int toSections, CRGB color);
void playAnswerAnimation(AnswerType type);
void blinkAll(CRGB color, int times, int onMs);
void playWinAnimation(); void playWinAnimation();
void setMirroredLED(int i, CRGB color); void setMirroredLED(int i, CRGB color);
CRGB getThermometerColor(int i); CRGB getThermometerColor(int i);
@@ -137,8 +134,11 @@ void onButtonPressed(int buttonIdx) {
lastInputTime = millis(); lastInputTime = millis();
AnswerType type = getAnswerType(question, answer); AnswerType type = getAnswerType(question, answer);
int prevTemp = tempSections;
selectedAnswer[question] = answer; selectedAnswer[question] = answer;
recalculateTemp(); // tempSections now holds the new value
// --- DEBUG --- // --- DEBUG ---
Serial.println("----------------------------"); Serial.println("----------------------------");
@@ -146,20 +146,13 @@ void onButtonPressed(int buttonIdx) {
Serial.print(" / Button "); Serial.print(answer + 1); Serial.print(" / Button "); Serial.print(answer + 1);
Serial.print(" (pin D"); Serial.print(BUTTON_PINS[buttonIdx]); Serial.println(")"); Serial.print(" (pin D"); Serial.print(BUTTON_PINS[buttonIdx]); Serial.println(")");
switch (type) { switch (type) {
case ANSWER_CORRECT: Serial.println("[RESULT] CORRECT"); break; case ANSWER_CORRECT: Serial.println("[RESULT] CORRECT"); break;
case ANSWER_HALF: Serial.println("[RESULT] HALF-CORRECT"); break; case ANSWER_HALF: Serial.println("[RESULT] HALF-CORRECT"); break;
case ANSWER_WRONG: Serial.println("[RESULT] WRONG"); break; case ANSWER_WRONG: Serial.println("[RESULT] WRONG"); break;
} }
playAnswerAnimation(type);
int prevTemp = tempSections;
recalculateTemp();
Serial.print("[TEMP] "); Serial.print(prevTemp); Serial.print("[TEMP] "); Serial.print(prevTemp);
Serial.print(" → "); Serial.print(tempSections); Serial.print(" → "); Serial.print(tempSections);
Serial.println(" sections"); Serial.println(" sections");
Serial.print("[STATE] "); Serial.print("[STATE] ");
for (int q = 0; q < NUM_QUESTIONS; q++) { for (int q = 0; q < NUM_QUESTIONS; q++) {
Serial.print("Q"); Serial.print(q + 1); Serial.print(":"); Serial.print("Q"); Serial.print(q + 1); Serial.print(":");
@@ -167,8 +160,8 @@ void onButtonPressed(int buttonIdx) {
Serial.print("-"); Serial.print("-");
} else { } else {
switch (getAnswerType(q, selectedAnswer[q])) { switch (getAnswerType(q, selectedAnswer[q])) {
case ANSWER_CORRECT: Serial.print("OK"); break; case ANSWER_CORRECT: Serial.print("OK"); break;
case ANSWER_HALF: Serial.print("HALF"); break; case ANSWER_HALF: Serial.print("HALF"); break;
case ANSWER_WRONG: Serial.print("WRONG"); break; case ANSWER_WRONG: Serial.print("WRONG"); break;
} }
} }
@@ -177,7 +170,12 @@ void onButtonPressed(int buttonIdx) {
Serial.println(); Serial.println();
// --- END DEBUG --- // --- 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(); displayTemperature();
if (checkWin()) { if (checkWin()) {
@@ -292,69 +290,58 @@ void displayTemperature() {
FastLED.show(); 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 ===================== // ===================== ANSWER FEEDBACK ANIMATIONS =====================
// Plays the appropriate feedback animation for the given answer type, // Feedback animation for a button press.
// then restores the thermometer display. // fromSections = temp before, toSections = temp after.
void playAnswerAnimation(AnswerType type) { //
switch (type) { // Correct (drop): all affected LEDs appear instantly in color, then vanish top→bottom.
case ANSWER_CORRECT: // Wrong/half (rise): LEDs added one by one bottom→top in color, then stay put
blinkAll(CRGB(0, 180, 0), 3, 60); // green blink × 3 // (displayTemperature() corrects colors immediately after).
break; // Re-press (no change): section at boundary flashes instantly, then restores.
case ANSWER_HALF: void playTempAnimation(int fromSections, int toSections, CRGB color) {
blinkAll(CRGB(255, 120, 0), 3, 60); // amber blink × 3 if (toSections < fromSections) {
break; // ---- CORRECT: temperature drops ----
case ANSWER_WRONG: int ledStart = START_LED + toSections * LEDS_PER_SECTION;
blinkAll(CRGB(180, 0, 0), 3, 60); // red blink × 3 int ledEnd = START_LED + fromSections * LEDS_PER_SECTION;
break;
}
// Restore thermometer after animation
displayTemperature();
}
// Blinks every LED with color for `times` flashes, each `onMs` long. // Show all affected LEDs in color at once
void blinkAll(CRGB color, int times, int onMs) { for (int led = ledStart; led < ledEnd; led++) setMirroredLED(led, color);
for (int t = 0; t < times; t++) {
for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, color);
FastLED.show(); FastLED.show();
delay(onMs); delay(200);
for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB::Black);
// 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(); 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 | | Button type | LED feedback | Temperature |
|---|---|---| |---|---|---|
| Correct | Blinks **green** | −1 section | | Correct | Affected sections light up **green** bottom→top, then fade out | −1 section (−2 if switching from wrong) |
| Half-correct | Glows **orange/yellow** | +1 section | | Half-correct | Affected sections light up **yellow** bottom→top, then fade out | +1 section |
| Wrong | Blinks **red** | +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. - 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. - **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 ### Feedback animations
| Type | Animation | Temp effect | All three types share `playTempAnimation(fromSections, toSections, color)`:
|---|---|---| - **Dropping** (correct): animates sections `toSections..fromSections-1` in green
| `ANSWER_CORRECT` | Green blink × 3 | −1 section | - **Rising** (wrong/half): animates sections `fromSections..toSections-1` in red/yellow
| `ANSWER_HALF` | Orange/yellow steady glow (700 ms) | +1 section | - **Re-press** (no change): animates one section at the current boundary for feedback
| `ANSWER_WRONG` | Red blink × 3 | +1 section |
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 ### Win animation
BIN
View File
Binary file not shown.