First implementation with flashes for feedback

This commit is contained in:
Andreas committed 2026-04-13 16:42:49 +02:00
1 parent f5236476ed
commit bf27305563
2 files changed
+52 -26

No files matched your search

+49 -24
View File
@@ -56,11 +56,13 @@ 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 =====================
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<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(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();
}
}
@@ -122,6 +125,7 @@ void resetGame() {
}
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,21 +358,15 @@ 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 rep = 0; rep < 3; rep++) {
for (int i = 0; i < BASE_LEDS; i++) {
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
FastLED.show();
@@ -355,6 +379,7 @@ void playWinAnimation() {
fadeAllLEDs();
delay(10);
}
}
}
void fadeAllLEDs() {
+3 -2
View File
@@ -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<WS2812, DATA_PIN, RGB>(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.