#include // ===================== LED CONFIGURATION ===================== // 240 LEDs total: 4 strips of 60 LEDs connected in series. // Each strip is physically reversed relative to the next, creating a snake/mirror // pattern. setMirroredLED(i, color) writes to all 4 strips simultaneously so they // appear as a single thermometer scale. #define DATA_PIN 14 // A0 used as digital out #define NUM_LEDS 240 #define BASE_LEDS 60 // logical positions (0..59), each mirrored to 4 strips CRGB leds[NUM_LEDS]; // ===================== BUTTON CONFIGURATION ===================== // 12 game buttons on D2–D13 (INPUT_PULLUP: LOW = pressed, HIGH = released). // Button groups map to questions: // Q1: pins D2, D3, D4 → button indices 0, 1, 2 // Q2: pins D5, D6, D7 → button indices 3, 4, 5 // Q3: pins D8, D9, D10 → button indices 6, 7, 8 // Q4: pins D11,D12,D13 → button indices 9,10,11 const int BUTTON_PINS[12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Reset button on A1 (pin 15). Wiring: connect to GND, use INPUT_PULLUP. const int RESET_PIN = 15; // ===================== GAME CONFIGURATION ===================== const int NUM_QUESTIONS = 4; const int BUTTONS_PER_QUESTION = 3; // For each question, which button index (0, 1, or 2) is correct / half-correct. // The remaining button is automatically wrong. // TODO: update with the actual correct and half-correct buttons for your questions. const int CORRECT_BUTTON[NUM_QUESTIONS] = {0, 1, 2, 0}; const int HALF_BUTTON[NUM_QUESTIONS] = {1, 2, 0, 1}; enum AnswerType { ANSWER_CORRECT, ANSWER_HALF, ANSWER_WRONG }; AnswerType getAnswerType(int question, int button) { if (button == CORRECT_BUTTON[question]) return ANSWER_CORRECT; if (button == HALF_BUTTON[question]) return ANSWER_HALF; return ANSWER_WRONG; } // Thermometer: 8 sections × 7 LEDs = 56 active LEDs. // The bottom 4 physical LEDs (i=0..3) are always off; the thermometer uses i=4..59. const int NUM_SECTIONS = 8; const int LEDS_PER_SECTION = 7; const int ACTIVE_LEDS = NUM_SECTIONS * LEDS_PER_SECTION; // 56 const int START_LED = BASE_LEDS - ACTIVE_LEDS; // LEDs 0..3 always off (physical bottom of strip) const int BASELINE_SECTIONS = 4; // start position = middle of the thermometer // LED brightness levels const uint8_t DIM_BRIGHTNESS = 12; // inactive (still visible for the gradient) 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]; bool buttonLastReading[12]; unsigned long buttonDebounceTime[12]; // ===================== FORWARD DECLARATIONS ===================== void resetGame(); void checkButtons(); 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 playWinAnimation(); void setMirroredLED(int i, CRGB color); CRGB getThermometerColor(int i); void fadeAllLEDs(); // ===================== SETUP ===================== void setup() { Serial.begin(9600); FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(84); for (int i = 0; i < 12; i++) { pinMode(BUTTON_PINS[i], INPUT_PULLUP); buttonCurrentState[i] = false; buttonLastReading[i] = false; buttonDebounceTime[i] = 0; } pinMode(RESET_PIN, INPUT_PULLUP); resetGame(); } // ===================== MAIN LOOP ===================== void loop() { if (!gameWon) { if (millis() - lastInputTime >= INACTIVITY_TIMEOUT) { Serial.println("[TIMEOUT] No input for 5 minutes — resetting."); resetGame(); } else { checkButtons(); } } else { playWinAnimation(); resetGame(); } } // ===================== GAME LOGIC ===================== void resetGame() { for (int q = 0; q < NUM_QUESTIONS; q++) { selectedAnswer[q] = -1; } tempSections = BASELINE_SECTIONS; gameWon = false; lastInputTime = millis(); displayTemperature(); Serial.println("Game reset. Temperature at baseline (4 sections)."); } // Called when button i (0–11) is pressed. void onButtonPressed(int buttonIdx) { int question = buttonIdx / BUTTONS_PER_QUESTION; int answer = buttonIdx % BUTTONS_PER_QUESTION; 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("[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("[WIN] ALL CORRECT – You win!"); } } // Recalculates tempSections from the currently selected answers. // Correct → -1 section. Half-correct or wrong → +1 section. void recalculateTemp() { int sections = BASELINE_SECTIONS; for (int q = 0; q < NUM_QUESTIONS; q++) { if (selectedAnswer[q] == -1) continue; if (getAnswerType(q, selectedAnswer[q]) == ANSWER_CORRECT) { sections--; } else { sections++; // both WRONG and HALF raise the temperature } } tempSections = constrain(sections, 0, NUM_SECTIONS); } bool checkWin() { for (int q = 0; q < NUM_QUESTIONS; q++) { if (selectedAnswer[q] == -1) return false; if (getAnswerType(q, selectedAnswer[q]) != ANSWER_CORRECT) return false; } return true; } // ===================== BUTTON READING ===================== void checkButtons() { // Reset button check if (digitalRead(RESET_PIN) == LOW) { delay(50); if (digitalRead(RESET_PIN) == LOW) { resetGame(); while (digitalRead(RESET_PIN) == LOW) {} // wait for release } return; } unsigned long now = millis(); for (int i = 0; i < 12; i++) { bool reading = (digitalRead(BUTTON_PINS[i]) == LOW); // LOW = pressed if (reading != buttonLastReading[i]) { buttonDebounceTime[i] = now; } if ((now - buttonDebounceTime[i]) > DEBOUNCE_MS) { if (reading && !buttonCurrentState[i]) { // Rising edge: button just became stably pressed buttonCurrentState[i] = true; onButtonPressed(i); } else if (!reading) { buttonCurrentState[i] = false; } } buttonLastReading[i] = reading; } } // ===================== LED DISPLAY ===================== // Sets logical position i (0–59) on all 4 physical strips simultaneously. // Strip layout: // Strip A leds[0..59] – forward // Strip B leds[119..60] – reversed (leds[119-i]) // Strip C leds[120..179] – forward (leds[120+i]) // Strip D leds[239..180] – reversed (leds[239-i]) void setMirroredLED(int i, CRGB color) { if (i < 0 || i >= BASE_LEDS) return; leds[i] = color; leds[119 - i] = color; leds[120 + i] = color; leds[239 - i] = color; } // Returns the thermometer gradient color for logical position i. // i = START_LED (4) → blue (cold), i = BASE_LEDS-1 (59) → red (hot). CRGB getThermometerColor(int i) { uint8_t pos = (uint8_t)map(i, START_LED, BASE_LEDS - 1, 0, 255); return blend(CRGB(0, 0, 180), CRGB(180, 0, 0), pos); } // Renders the thermometer state onto the LED strips. void displayTemperature() { int litCount = tempSections * LEDS_PER_SECTION; // Active LEDs run from START_LED upward. int activeStart = START_LED; int activeEnd = START_LED + litCount; // exclusive upper bound for (int i = 0; i < BASE_LEDS; i++) { CRGB color; if (i < START_LED) { // Bottom 4 LEDs: always off color = CRGB::Black; } else { color = getThermometerColor(i); if (i < activeEnd) { color.nscale8(BRIGHT_BRIGHTNESS); // active (bright) } else { color.nscale8(DIM_BRIGHTNESS); // inactive (dim gradient still visible) } } setMirroredLED(i, color); } 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(); } // 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); FastLED.show(); delay(onMs); for (int i = 0; i < BASE_LEDS; i++) setMirroredLED(i, CRGB::Black); FastLED.show(); delay(onMs / 2); } } // ===================== 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(); fadeAllLEDs(); delay(10); } for (int i = BASE_LEDS - 1; i >= 0; i--) { setMirroredLED(i, CRGB(CHSV(hue++, 255, 255))); FastLED.show(); fadeAllLEDs(); delay(10); } } } void fadeAllLEDs() { for (int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }