First implementation with flashes for feedback
This commit is contained in:
1 parent
f5236476ed
commit
bf27305563
2 files changed
+52
-26
No files matched your search
+49
-24
@@ -56,11 +56,13 @@ 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 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 =====================
|
// ===================== GAME STATE =====================
|
||||||
int selectedAnswer[NUM_QUESTIONS]; // -1 = no answer, 0/1/2 = button within group
|
int selectedAnswer[NUM_QUESTIONS]; // -1 = no answer, 0/1/2 = button within group
|
||||||
int tempSections; // current number of lit sections (0–8)
|
int tempSections; // current number of lit sections (0–8)
|
||||||
bool gameWon;
|
bool gameWon;
|
||||||
|
unsigned long lastInputTime; // millis() of the last button press
|
||||||
|
|
||||||
// ===================== DEBOUNCE STATE =====================
|
// ===================== DEBOUNCE STATE =====================
|
||||||
bool buttonCurrentState[12];
|
bool buttonCurrentState[12];
|
||||||
@@ -77,7 +79,6 @@ void displayTemperature();
|
|||||||
void animateTempChange(int fromSections, int toSections);
|
void animateTempChange(int fromSections, int toSections);
|
||||||
void playAnswerAnimation(AnswerType type);
|
void playAnswerAnimation(AnswerType type);
|
||||||
void blinkAll(CRGB color, int times, int onMs);
|
void blinkAll(CRGB color, int times, int onMs);
|
||||||
void flashAll(CRGB color, int durationMs);
|
|
||||||
void playWinAnimation();
|
void playWinAnimation();
|
||||||
void setMirroredLED(int i, CRGB color);
|
void setMirroredLED(int i, CRGB color);
|
||||||
CRGB getThermometerColor(int i);
|
CRGB getThermometerColor(int i);
|
||||||
@@ -87,7 +88,7 @@ void fadeAllLEDs();
|
|||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
|
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
|
||||||
FastLED.setBrightness(84);
|
FastLED.setBrightness(84);
|
||||||
|
|
||||||
for (int i = 0; i < 12; i++) {
|
for (int i = 0; i < 12; i++) {
|
||||||
@@ -104,13 +105,15 @@ void setup() {
|
|||||||
// ===================== MAIN LOOP =====================
|
// ===================== MAIN LOOP =====================
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!gameWon) {
|
if (!gameWon) {
|
||||||
checkButtons();
|
if (millis() - lastInputTime >= INACTIVITY_TIMEOUT) {
|
||||||
} else {
|
Serial.println("[TIMEOUT] No input for 5 minutes — resetting.");
|
||||||
// Continuously play the win animation until reset is pressed
|
|
||||||
playWinAnimation();
|
|
||||||
if (digitalRead(RESET_PIN) == LOW) {
|
|
||||||
resetGame();
|
resetGame();
|
||||||
|
} else {
|
||||||
|
checkButtons();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
playWinAnimation();
|
||||||
|
resetGame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +125,7 @@ void resetGame() {
|
|||||||
}
|
}
|
||||||
tempSections = BASELINE_SECTIONS;
|
tempSections = BASELINE_SECTIONS;
|
||||||
gameWon = false;
|
gameWon = false;
|
||||||
|
lastInputTime = millis();
|
||||||
displayTemperature();
|
displayTemperature();
|
||||||
Serial.println("Game reset. Temperature at baseline (4 sections).");
|
Serial.println("Game reset. Temperature at baseline (4 sections).");
|
||||||
}
|
}
|
||||||
@@ -131,28 +135,54 @@ void onButtonPressed(int buttonIdx) {
|
|||||||
int question = buttonIdx / BUTTONS_PER_QUESTION;
|
int question = buttonIdx / BUTTONS_PER_QUESTION;
|
||||||
int answer = buttonIdx % BUTTONS_PER_QUESTION;
|
int answer = buttonIdx % BUTTONS_PER_QUESTION;
|
||||||
|
|
||||||
Serial.print("Q"); Serial.print(question + 1);
|
lastInputTime = millis();
|
||||||
Serial.print(" Button "); Serial.print(answer + 1);
|
|
||||||
|
|
||||||
AnswerType type = getAnswerType(question, answer);
|
AnswerType type = getAnswerType(question, answer);
|
||||||
selectedAnswer[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) {
|
switch (type) {
|
||||||
case ANSWER_CORRECT: Serial.println(" → CORRECT"); break;
|
case ANSWER_CORRECT: Serial.println("[RESULT] CORRECT"); break;
|
||||||
case ANSWER_HALF: Serial.println(" → HALF-CORRECT"); break;
|
case ANSWER_HALF: Serial.println("[RESULT] HALF-CORRECT"); break;
|
||||||
case ANSWER_WRONG: Serial.println(" → WRONG"); break;
|
case ANSWER_WRONG: Serial.println("[RESULT] WRONG"); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
playAnswerAnimation(type);
|
playAnswerAnimation(type);
|
||||||
|
|
||||||
int prevTemp = tempSections;
|
int prevTemp = tempSections;
|
||||||
recalculateTemp();
|
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);
|
animateTempChange(prevTemp, tempSections);
|
||||||
displayTemperature();
|
displayTemperature();
|
||||||
|
|
||||||
if (checkWin()) {
|
if (checkWin()) {
|
||||||
gameWon = true;
|
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) {
|
void playAnswerAnimation(AnswerType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ANSWER_CORRECT:
|
case ANSWER_CORRECT:
|
||||||
blinkAll(CRGB(0, 180, 0), 3, 150); // green blink × 3
|
blinkAll(CRGB(0, 180, 0), 3, 60); // green blink × 3
|
||||||
break;
|
break;
|
||||||
case ANSWER_HALF:
|
case ANSWER_HALF:
|
||||||
flashAll(CRGB(200, 80, 0), 700); // orange/yellow steady glow
|
blinkAll(CRGB(255, 120, 0), 3, 60); // amber blink × 3
|
||||||
break;
|
break;
|
||||||
case ANSWER_WRONG:
|
case ANSWER_WRONG:
|
||||||
blinkAll(CRGB(180, 0, 0), 3, 150); // red blink × 3
|
blinkAll(CRGB(180, 0, 0), 3, 60); // red blink × 3
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Restore thermometer after animation
|
// 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 =====================
|
// ===================== WIN ANIMATION =====================
|
||||||
|
|
||||||
// Rainbow sweep that plays continuously while the game is in the won state.
|
// 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() {
|
void playWinAnimation() {
|
||||||
static uint8_t hue = 0;
|
static uint8_t hue = 0;
|
||||||
|
|
||||||
|
for (int rep = 0; rep < 3; rep++) {
|
||||||
for (int i = 0; i < BASE_LEDS; i++) {
|
for (int i = 0; i < BASE_LEDS; i++) {
|
||||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||||
FastLED.show();
|
FastLED.show();
|
||||||
@@ -356,6 +380,7 @@ void playWinAnimation() {
|
|||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void fadeAllLEDs() {
|
void fadeAllLEDs() {
|
||||||
for (int i = 0; i < NUM_LEDS; i++) {
|
for (int i = 0; i < NUM_LEDS; i++) {
|
||||||
|
|||||||
@@ -51,8 +51,9 @@ All buttons wired with `INPUT_PULLUP`: connect each button between its pin and G
|
|||||||
| Wrong | Blinks **red** | +1 section |
|
| Wrong | Blinks **red** | +1 section |
|
||||||
|
|
||||||
- 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.
|
- **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.
|
- 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);
|
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.
|
||||||
Reference in new issue
Block a user