First implementation with flashes for feedback
This commit is contained in:
1 parent
f5236476ed
commit
bf27305563
2 files changed
+70
-44
No files matched your search
+67
-42
@@ -54,13 +54,15 @@ const uint8_t DIM_BRIGHTNESS = 12; // inactive (still visible for the gradi
|
||||
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 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;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +123,9 @@ void resetGame() {
|
||||
for (int q = 0; q < NUM_QUESTIONS; q++) {
|
||||
selectedAnswer[q] = -1;
|
||||
}
|
||||
tempSections = BASELINE_SECTIONS;
|
||||
gameWon = false;
|
||||
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,32 +358,27 @@ 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 i = 0; i < BASE_LEDS; i++) {
|
||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||
FastLED.show();
|
||||
fadeAllLEDs();
|
||||
delay(10);
|
||||
}
|
||||
for (int i = BASE_LEDS - 1; i >= 0; i--) {
|
||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||
FastLED.show();
|
||||
fadeAllLEDs();
|
||||
delay(10);
|
||||
for (int rep = 0; rep < 3; rep++) {
|
||||
for (int i = 0; i < BASE_LEDS; i++) {
|
||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||
FastLED.show();
|
||||
fadeAllLEDs();
|
||||
delay(10);
|
||||
}
|
||||
for (int i = BASE_LEDS - 1; i >= 0; i--) {
|
||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||
FastLED.show();
|
||||
fadeAllLEDs();
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user