Starting with the actual game \o/
This commit is contained in:
1 parent
66e5a8c120
commit
f5236476ed
8 files changed
+132395
-257
No files matched your search
+315
-255
@@ -1,304 +1,364 @@
|
||||
//multiple RFID readers with SPI
|
||||
/**************************************************************************/
|
||||
#include <Wire.h>
|
||||
#include <SPI.h>
|
||||
#include <Adafruit_PN532.h>
|
||||
|
||||
// If using the breakout with SPI, define the pins for SPI communication.
|
||||
#define PN532_SCK (2) //yellow
|
||||
#define PN532_MOSI (3) //red
|
||||
#define PN532_SS (4) //orange
|
||||
#define PN532_MISO (5) //green
|
||||
|
||||
|
||||
enum UIDResult {
|
||||
UID_NONE,
|
||||
UID_RIGHT,
|
||||
UID_WRONG
|
||||
};
|
||||
|
||||
// Use this line for a breakout with a SPI connection:
|
||||
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, 4); //orange
|
||||
Adafruit_PN532 nfc2(PN532_SCK, PN532_MISO, PN532_MOSI, 6); //brown
|
||||
Adafruit_PN532 nfc3(PN532_SCK, PN532_MISO, PN532_MOSI, 7); //purple
|
||||
|
||||
//LED strip baseline
|
||||
/**************************************************************************/
|
||||
#include <FastLED.h>
|
||||
|
||||
#define LED_PIN 8
|
||||
#define NUM_LEDS 60
|
||||
#define LED_TYPE WS2812B
|
||||
#define COLOR_ORDER GRB
|
||||
// ===================== 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];
|
||||
|
||||
// Base colors (pure RGB, no green component)
|
||||
CRGB blue = CRGB(0, 0, 180);
|
||||
CRGB red = CRGB(180, 0, 0);
|
||||
// ===================== 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};
|
||||
|
||||
// Brightness levels
|
||||
const uint8_t DIM_SCALE = 2;
|
||||
const uint8_t BRIGHT_SCALE = 100;
|
||||
// Reset button on A1 (pin 15). Wiring: connect to GND, use INPUT_PULLUP.
|
||||
const int RESET_PIN = 15;
|
||||
|
||||
void setupNFC(Adafruit_PN532 nfc){
|
||||
nfc.begin();
|
||||
// ===================== GAME CONFIGURATION =====================
|
||||
const int NUM_QUESTIONS = 4;
|
||||
const int BUTTONS_PER_QUESTION = 3;
|
||||
|
||||
uint32_t versiondata = nfc.getFirmwareVersion();
|
||||
if (! versiondata) {
|
||||
Serial.print("Didn't find PN53x board on");
|
||||
while (1); // halt
|
||||
}
|
||||
// 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};
|
||||
|
||||
// Got ok data, print it out!
|
||||
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
|
||||
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
|
||||
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
|
||||
|
||||
// Set the max number of retry attempts to read from a card
|
||||
// This prevents us from waiting forever for a card, which is
|
||||
// the default behaviour of the PN532.
|
||||
nfc.setPassiveActivationRetries(0xFF);
|
||||
|
||||
Serial.println("Waiting for an ISO14443A card");
|
||||
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;
|
||||
}
|
||||
|
||||
unsigned long uid1HoldStart = 0; // When reader 1 first sees a card
|
||||
const unsigned long UID1_HOLD_MS = 5000; // 5 seconds to trigger win
|
||||
bool winAnimationPlayed = false; // To prevent multiple triggers
|
||||
// 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)
|
||||
|
||||
/**************************************************************************/
|
||||
void setup(void) {
|
||||
// Timing
|
||||
const unsigned long DEBOUNCE_MS = 50;
|
||||
const int ANIM_STEP_MS = 40; // ms between each LED step during temp animation
|
||||
|
||||
//LED set-up
|
||||
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
|
||||
FastLED.setBrightness(255); // Control brightness manually
|
||||
// ===================== 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;
|
||||
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(10); // for Leonardo/Micro/Zero
|
||||
Serial.println("Hello!");
|
||||
setupNFC(nfc);
|
||||
setupNFC(nfc2);
|
||||
setupNFC(nfc3);
|
||||
}
|
||||
// ===================== DEBOUNCE STATE =====================
|
||||
bool buttonCurrentState[12];
|
||||
bool buttonLastReading[12];
|
||||
unsigned long buttonDebounceTime[12];
|
||||
|
||||
boolean getCardID(Adafruit_PN532 &nfc, uint8_t *uid, uint8_t &uidLength) {
|
||||
// ===================== 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 flashAll(CRGB color, int durationMs);
|
||||
void playWinAnimation();
|
||||
void setMirroredLED(int i, CRGB color);
|
||||
CRGB getThermometerColor(int i);
|
||||
void fadeAllLEDs();
|
||||
|
||||
// Try to read a card (timeout = 100 ms)
|
||||
boolean success = nfc.readPassiveTargetID(
|
||||
PN532_MIFARE_ISO14443A,
|
||||
uid,
|
||||
&uidLength,
|
||||
100
|
||||
);
|
||||
// ===================== SETUP =====================
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
return success;
|
||||
}
|
||||
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
|
||||
FastLED.setBrightness(84);
|
||||
|
||||
const unsigned long CARD_TIMEOUT_MS = 2000;
|
||||
|
||||
unsigned long lastSeen1 = 0;
|
||||
unsigned long lastSeen2 = 0;
|
||||
unsigned long lastSeen3 = 0;
|
||||
|
||||
|
||||
void loop(void) {
|
||||
|
||||
delay(50);
|
||||
|
||||
const int8_t DEFAULT_TEMP = 24;
|
||||
int8_t temperature = DEFAULT_TEMP;
|
||||
|
||||
unsigned long now = millis();
|
||||
|
||||
uint8_t uid1[7], uid2[7], uid3[7];
|
||||
uint8_t uidLength1 = 0, uidLength2 = 0, uidLength3 = 0;
|
||||
|
||||
// -------- Reader 1 --------
|
||||
if (getCardID(nfc, uid1, uidLength1)) {
|
||||
lastSeen1 = millis();
|
||||
|
||||
// Start hold timer if not already started
|
||||
if (uid1HoldStart == 0) {
|
||||
uid1HoldStart = millis();
|
||||
}
|
||||
|
||||
// Check if card has been held for 5 seconds
|
||||
if (!winAnimationPlayed && (millis() - uid1HoldStart >= UID1_HOLD_MS)) {
|
||||
Serial.println("WIN Animation Triggered!");
|
||||
playWinAnimation(); // Call our function
|
||||
winAnimationPlayed = true; // prevent repeat until card is removed
|
||||
}
|
||||
} else {
|
||||
// No card detected, reset hold timer and animation flag
|
||||
uid1HoldStart = 0;
|
||||
winAnimationPlayed = false;
|
||||
}
|
||||
|
||||
|
||||
// -------- Reader 2 --------
|
||||
if (getCardID(nfc2, uid2, uidLength2)) {
|
||||
lastSeen2 = now;
|
||||
}
|
||||
|
||||
// -------- Reader 3 --------
|
||||
if (getCardID(nfc3, uid3, uidLength3)) {
|
||||
lastSeen3 = now;
|
||||
}
|
||||
|
||||
// -------- Presence check (2s timeout) --------
|
||||
bool reader1Active = (now - lastSeen1) < CARD_TIMEOUT_MS;
|
||||
bool reader2Active = (now - lastSeen2) < CARD_TIMEOUT_MS;
|
||||
bool reader3Active = (now - lastSeen3) < CARD_TIMEOUT_MS;
|
||||
|
||||
if (reader1Active) {
|
||||
Serial.println("Reader 1 active");
|
||||
temperature -= 6;
|
||||
}
|
||||
|
||||
if (reader2Active) {
|
||||
Serial.println("Reader 2 active");
|
||||
temperature += 6;
|
||||
}
|
||||
|
||||
if (reader3Active) {
|
||||
Serial.println("Reader 3 active");
|
||||
temperature += 6;
|
||||
}
|
||||
|
||||
temperature = constrain(temperature, 0, NUM_LEDS - 12);
|
||||
displayTemp(temperature);
|
||||
}
|
||||
|
||||
|
||||
void displayTemp(uint8_t temp) {
|
||||
|
||||
// Clamp temp so we don’t overflow the strip
|
||||
uint8_t maxTempLeds = NUM_LEDS - 12;
|
||||
if (temp > maxTempLeds) temp = maxTempLeds;
|
||||
|
||||
uint16_t brightEnd = 12 + temp;
|
||||
|
||||
// --------------------------------------------------
|
||||
// First 12 LEDs OFF
|
||||
for (int i = 0; i < 12; i++) {
|
||||
leds[i] = CRGB::Black;
|
||||
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
|
||||
buttonCurrentState[i] = false;
|
||||
buttonLastReading[i] = false;
|
||||
buttonDebounceTime[i] = 0;
|
||||
}
|
||||
pinMode(RESET_PIN, INPUT_PULLUP);
|
||||
|
||||
// Bottom 6 LEDs solid BLUE
|
||||
for (int i = 12; i < 18; i++) {
|
||||
CRGB color = blue;
|
||||
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
|
||||
color.nscale8_video(scale);
|
||||
leds[i] = color;
|
||||
}
|
||||
|
||||
// Gradient section (between solid colors)
|
||||
for (int i = 18; i < 54; i++) {
|
||||
|
||||
uint8_t pos = map(i, 18, 53, 0, 255);
|
||||
uint8_t eased = ease8InOutQuad(pos);
|
||||
CRGB color = blend(blue, red, eased);
|
||||
|
||||
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
|
||||
color.nscale8_video(scale);
|
||||
|
||||
leds[i] = color;
|
||||
}
|
||||
|
||||
// Top 6 LEDs solid RED
|
||||
for (int i = 54; i < NUM_LEDS; i++) {
|
||||
CRGB color = red;
|
||||
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
|
||||
color.nscale8_video(scale);
|
||||
leds[i] = color;
|
||||
}
|
||||
|
||||
FastLED.show();
|
||||
resetGame();
|
||||
}
|
||||
|
||||
|
||||
// ---- Known UIDs ----
|
||||
|
||||
// Right answers (4-byte UIDs)
|
||||
const uint8_t RIGHT_UIDS[][4] = {
|
||||
{0x4E, 0xBF, 0xCA, 0x06},
|
||||
{0xF6, 0x4D, 0x1C, 0x06},
|
||||
{0xA3, 0xBC, 0xCA, 0x06},
|
||||
{0xC3, 0xDD, 0xCA, 0x06}
|
||||
};
|
||||
const uint8_t NUM_RIGHT_UIDS = 4;
|
||||
|
||||
// Wrong answers (7-byte UIDs)
|
||||
const uint8_t WRONG_UIDS[][7] = {
|
||||
{0x04, 0x97, 0xCF, 0x1F, 0x3F, 0x61, 0x80},
|
||||
{0x04, 0x7C, 0xA9, 0x00, 0xA0, 0x61, 0x80},
|
||||
{0x04, 0x6D, 0xC2, 0x2E, 0x5F, 0x61, 0x80},
|
||||
{0x04, 0x22, 0x85, 0xFF, 0x9F, 0x61, 0x80}
|
||||
};
|
||||
const uint8_t NUM_WRONG_UIDS = 4;
|
||||
|
||||
|
||||
bool compareUID(const uint8_t *uidA, const uint8_t *uidB, uint8_t length) {
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
if (uidA[i] != uidB[i]) {
|
||||
return false;
|
||||
// ===================== MAIN LOOP =====================
|
||||
void loop() {
|
||||
if (!gameWon) {
|
||||
checkButtons();
|
||||
} else {
|
||||
// Continuously play the win animation until reset is pressed
|
||||
playWinAnimation();
|
||||
if (digitalRead(RESET_PIN) == LOW) {
|
||||
resetGame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== GAME LOGIC =====================
|
||||
|
||||
void resetGame() {
|
||||
for (int q = 0; q < NUM_QUESTIONS; q++) {
|
||||
selectedAnswer[q] = -1;
|
||||
}
|
||||
tempSections = BASELINE_SECTIONS;
|
||||
gameWon = false;
|
||||
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;
|
||||
|
||||
Serial.print("Q"); Serial.print(question + 1);
|
||||
Serial.print(" Button "); Serial.print(answer + 1);
|
||||
|
||||
AnswerType type = getAnswerType(question, answer);
|
||||
selectedAnswer[question] = answer;
|
||||
|
||||
switch (type) {
|
||||
case ANSWER_CORRECT: Serial.println(" → CORRECT"); break;
|
||||
case ANSWER_HALF: Serial.println(" → HALF-CORRECT"); break;
|
||||
case ANSWER_WRONG: Serial.println(" → WRONG"); break;
|
||||
}
|
||||
|
||||
playAnswerAnimation(type);
|
||||
|
||||
int prevTemp = tempSections;
|
||||
recalculateTemp();
|
||||
animateTempChange(prevTemp, tempSections);
|
||||
displayTemperature();
|
||||
|
||||
if (checkWin()) {
|
||||
gameWon = true;
|
||||
Serial.println("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;
|
||||
}
|
||||
UIDResult checkUID(const uint8_t *uid, uint8_t uidLength) {
|
||||
|
||||
// Check RIGHT UIDs (4 bytes)
|
||||
if (uidLength == 4) {
|
||||
for (uint8_t i = 0; i < NUM_RIGHT_UIDS; i++) {
|
||||
if (compareUID(uid, RIGHT_UIDS[i], 4)) {
|
||||
Serial.println("correct");
|
||||
return UID_RIGHT;
|
||||
}
|
||||
// ===================== 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;
|
||||
}
|
||||
|
||||
// Check WRONG UIDs (7 bytes)
|
||||
if (uidLength == 7) {
|
||||
for (uint8_t i = 0; i < NUM_WRONG_UIDS; i++) {
|
||||
if (compareUID(uid, WRONG_UIDS[i], 7)) {
|
||||
Serial.println("wrong");
|
||||
return UID_WRONG;
|
||||
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;
|
||||
}
|
||||
Serial.println("unknown");
|
||||
return UID_NONE;
|
||||
}
|
||||
|
||||
// ===================== 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, 150); // green blink × 3
|
||||
break;
|
||||
case ANSWER_HALF:
|
||||
flashAll(CRGB(200, 80, 0), 700); // orange/yellow steady glow
|
||||
break;
|
||||
case ANSWER_WRONG:
|
||||
blinkAll(CRGB(180, 0, 0), 3, 150); // 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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
void playWinAnimation() {
|
||||
const int waitMs = 10; // delay between each LED step
|
||||
static uint8_t hue = 0; // hue counter
|
||||
static uint8_t hue = 0;
|
||||
|
||||
// Forward sweep
|
||||
for (int i = 0; i < NUM_LEDS; i++) {
|
||||
leds[i] = CHSV(hue++, 255, 255); // light up the LED with current hue
|
||||
for (int i = 0; i < BASE_LEDS; i++) {
|
||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||
FastLED.show();
|
||||
fadeAllSweep(); // fade all LEDs slightly
|
||||
delay(waitMs);
|
||||
fadeAllLEDs();
|
||||
delay(10);
|
||||
}
|
||||
|
||||
// Backward sweep
|
||||
for (int i = NUM_LEDS - 1; i >= 0; i--) {
|
||||
leds[i] = CHSV(hue++, 255, 255); // light up the LED with current hue
|
||||
for (int i = BASE_LEDS - 1; i >= 0; i--) {
|
||||
setMirroredLED(i, CRGB(CHSV(hue++, 255, 255)));
|
||||
FastLED.show();
|
||||
fadeAllSweep(); // fade all LEDs slightly
|
||||
delay(waitMs);
|
||||
fadeAllLEDs();
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
// exactly like your previous 'fadeall()'
|
||||
void fadeAllSweep() {
|
||||
void fadeAllLEDs() {
|
||||
for (int i = 0; i < NUM_LEDS; i++) {
|
||||
leds[i].nscale8(250); // dim LEDs gradually
|
||||
leds[i].nscale8(250);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user