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
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);
}
}