Starting with the actual game \o/
This commit is contained in:
1 parent
66e5a8c120
commit
f5236476ed
8 files changed
+132391
-253
No files matched your search
+311
-251
@@ -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};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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);
|
||||
// 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
|
||||
|
||||
// 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);
|
||||
// LED brightness levels
|
||||
const uint8_t DIM_BRIGHTNESS = 12; // inactive (still visible for the gradient)
|
||||
const uint8_t BRIGHT_BRIGHTNESS = 200; // active (lit)
|
||||
|
||||
Serial.println("Waiting for an ISO14443A card");
|
||||
// Timing
|
||||
const unsigned long DEBOUNCE_MS = 50;
|
||||
const int ANIM_STEP_MS = 40; // ms between each LED step during temp animation
|
||||
|
||||
}
|
||||
// ===================== 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 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
|
||||
// ===================== 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 flashAll(CRGB color, int durationMs);
|
||||
void playWinAnimation();
|
||||
void setMirroredLED(int i, CRGB color);
|
||||
CRGB getThermometerColor(int i);
|
||||
void fadeAllLEDs();
|
||||
|
||||
/**************************************************************************/
|
||||
void setup(void) {
|
||||
// ===================== SETUP =====================
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
//LED set-up
|
||||
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
|
||||
FastLED.setBrightness(255); // Control brightness manually
|
||||
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
|
||||
FastLED.setBrightness(84);
|
||||
|
||||
Serial.begin(115200);
|
||||
while (!Serial) delay(10); // for Leonardo/Micro/Zero
|
||||
Serial.println("Hello!");
|
||||
setupNFC(nfc);
|
||||
setupNFC(nfc2);
|
||||
setupNFC(nfc3);
|
||||
}
|
||||
|
||||
boolean getCardID(Adafruit_PN532 &nfc, uint8_t *uid, uint8_t &uidLength) {
|
||||
|
||||
// Try to read a card (timeout = 100 ms)
|
||||
boolean success = nfc.readPassiveTargetID(
|
||||
PN532_MIFARE_ISO14443A,
|
||||
uid,
|
||||
&uidLength,
|
||||
100
|
||||
);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
resetGame();
|
||||
}
|
||||
|
||||
// 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;
|
||||
// ===================== MAIN LOOP =====================
|
||||
void loop() {
|
||||
if (!gameWon) {
|
||||
checkButtons();
|
||||
} else {
|
||||
// Continuously play the win animation until reset is pressed
|
||||
playWinAnimation();
|
||||
if (digitalRead(RESET_PIN) == LOW) {
|
||||
resetGame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Gradient section (between solid colors)
|
||||
for (int i = 18; i < 54; i++) {
|
||||
// ===================== GAME LOGIC =====================
|
||||
|
||||
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;
|
||||
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).");
|
||||
}
|
||||
|
||||
// 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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
FastLED.show();
|
||||
playAnswerAnimation(type);
|
||||
|
||||
int prevTemp = tempSections;
|
||||
recalculateTemp();
|
||||
animateTempChange(prevTemp, tempSections);
|
||||
displayTemperature();
|
||||
|
||||
if (checkWin()) {
|
||||
gameWon = true;
|
||||
Serial.println("ALL CORRECT – You win!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ---- 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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
CRGB leds[NUM_LEDS];
|
||||
|
||||
void setup() {
|
||||
Serial.begin(57600);
|
||||
Serial.begin(9600);
|
||||
Serial.println("resetting");
|
||||
FastLED.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
|
||||
FastLED.setBrightness(84);
|
||||
@@ -26,7 +26,7 @@ void setMirroredLED(int i, CHSV color) {
|
||||
leds[i] = color;
|
||||
if (120 - i >= 0 && 120 - i < NUM_LEDS) leds[119 - i] = color;
|
||||
if (120 + i < NUM_LEDS) leds[120 + i] = color;
|
||||
if (180 + i < NUM_LEDS) leds[240 - i] = color;
|
||||
if (239 - i >= 0 && 239 - i < NUM_LEDS) leds[239 - i] = color;
|
||||
}
|
||||
|
||||
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
# Exploring Uncertainties
|
||||
|
||||
An interactive Arduino installation using 12 push buttons and a 240-LED thermometer strip. Players answer 4 questions by pressing buttons; correct answers cool the thermometer down, wrong answers heat it up. Getting all 4 right brings the temperature to zero and triggers a rainbow win animation.
|
||||
|
||||
---
|
||||
|
||||
## Hardware
|
||||
|
||||
| Component | Details |
|
||||
|---|---|
|
||||
| Microcontroller | Arduino Uno |
|
||||
| LED strip | WS2812B, 60 LEDs/m, 5V — 4 strips of 60 connected in series (240 LEDs total) |
|
||||
| Buttons | 12× momentary push buttons (IP65 aluminium, 19 mm, 1NO) |
|
||||
| Reset button | 1× additional momentary button (on A1) |
|
||||
| Power | External 5V PSU for LED strip; USB powers the Arduino |
|
||||
|
||||
## Wiring Summary
|
||||
|
||||
See [Wiring.md](Wiring.md) for full details and diagrams.
|
||||
|
||||
| Component | Arduino pin |
|
||||
|---|---|
|
||||
| LED strip data | A0 (= digital pin 14) |
|
||||
| Button 1 (Q1-A) | D2 |
|
||||
| Button 2 (Q1-B) | D3 |
|
||||
| Button 3 (Q1-C) | D4 |
|
||||
| Button 4 (Q2-A) | D5 |
|
||||
| Button 5 (Q2-B) | D6 |
|
||||
| Button 6 (Q2-C) | D7 |
|
||||
| Button 7 (Q3-A) | D8 |
|
||||
| Button 8 (Q3-B) | D9 |
|
||||
| Button 9 (Q3-C) | D10 |
|
||||
| Button 10 (Q4-A) | D11 |
|
||||
| Button 11 (Q4-B) | D12 |
|
||||
| Button 12 (Q4-C) | D13 |
|
||||
| Reset button | A1 (= digital pin 15) |
|
||||
|
||||
All buttons wired with `INPUT_PULLUP`: connect each button between its pin and GND. LOW = pressed.
|
||||
|
||||
---
|
||||
|
||||
## Game Rules
|
||||
|
||||
- **4 questions**, each answered by 3 physical buttons: 1 correct, 1 half-correct, 1 wrong.
|
||||
- The LED strip acts as a **thermometer** starting at the midpoint (4 of 8 sections lit).
|
||||
|
||||
| Button type | LED feedback | Temperature |
|
||||
|---|---|---|
|
||||
| Correct | Blinks **green** | −1 section |
|
||||
| Half-correct | Glows **orange/yellow** | +1 section |
|
||||
| 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.
|
||||
- Press the **reset button** (A1) at any time to reset all answers and return the thermometer to the midpoint.
|
||||
|
||||
---
|
||||
|
||||
## Folder Structure
|
||||
|
||||
```
|
||||
ExploringUncertanties/
|
||||
├── README.md ← this file
|
||||
├── Wiring.md ← full wiring diagrams and pin assignments
|
||||
├── game.md ← game design notes (Ilo's spec)
|
||||
├── hardware.md ← hardware specs and purchase links
|
||||
├── einkaufsliste.md ← shopping list
|
||||
├── ExploringUncertanties.ino ← MAIN SKETCH — upload this to the Arduino
|
||||
├── LEDStrip/
|
||||
│ ├── test/
|
||||
│ │ └── test.ino ← LED strip test / setMirroredLED reference implementation
|
||||
│ └── betterwinanimation-only.ino ← standalone win animation prototype
|
||||
└── button_test/
|
||||
└── button_test.ino ← standalone button wiring test
|
||||
```
|
||||
|
||||
**The sketch to upload is `ExploringUncertanties.ino` in the project root.**
|
||||
|
||||
---
|
||||
|
||||
## For Claude
|
||||
|
||||
This section captures the full technical context so future conversations can pick up without re-reading every file.
|
||||
|
||||
### LED strip physical layout
|
||||
|
||||
Four 60-LED WS2812B strips are connected end-to-end, alternating direction (snake pattern), giving 240 physical LEDs total. The data pin is A0 (Arduino digital pin 14).
|
||||
|
||||
The strips form two mirrored pairs:
|
||||
|
||||
```
|
||||
Strip A leds[0..59] → forward (bottom to top)
|
||||
Strip B leds[119..60] → reversed (top to bottom) — mirrors strip A
|
||||
Strip C leds[120..179] → forward (bottom to top) — mirrors strip A
|
||||
Strip D leds[239..180] → reversed (top to bottom) — mirrors strip D
|
||||
```
|
||||
|
||||
Because of this layout, `setMirroredLED(i, color)` is the **only** function you should call to set LEDs. It writes to all four physical positions at once so all strips show the same image:
|
||||
|
||||
```cpp
|
||||
void setMirroredLED(int i, CRGB color) {
|
||||
// i ranges 0..59 (BASE_LEDS - 1)
|
||||
leds[i] = color; // Strip A
|
||||
leds[119 - i] = color; // Strip B (reversed)
|
||||
leds[120 + i] = color; // Strip C
|
||||
leds[239 - i] = color; // Strip D (reversed)
|
||||
}
|
||||
```
|
||||
|
||||
Never write to `leds[]` directly — always go through `setMirroredLED`.
|
||||
|
||||
### Thermometer model
|
||||
|
||||
| Constant | Value | Meaning |
|
||||
|---|---|---|
|
||||
| `BASE_LEDS` | 60 | Total logical positions (0–59) |
|
||||
| `NUM_SECTIONS` | 8 | Thermometer sections |
|
||||
| `LEDS_PER_SECTION` | 7 | LEDs per section |
|
||||
| `ACTIVE_LEDS` | 56 | 56 thermometer positions; i=0–3 always off (physical bottom), i=4–59 active |
|
||||
| `LED_BOTTOM_OFFSET` | 4 | Bottom 4 LEDs always off; thermometer starts at i=4 |
|
||||
| `BASELINE_SECTIONS` | 4 | Starting state (midpoint) |
|
||||
|
||||
Color gradient: **blue** (i = 4, cold) → **red** (i = 59, hot) via `blend(CRGB(0,0,180), CRGB(180,0,0), pos)`. Positions 0–3 are always black (physical bottom of the strip).
|
||||
|
||||
Active LEDs (below the current temperature level) are rendered at `BRIGHT_BRIGHTNESS`. Inactive LEDs (above) are rendered at `DIM_BRIGHTNESS` — still colored, so the full gradient is always visible.
|
||||
|
||||
### Game state
|
||||
|
||||
```cpp
|
||||
int selectedAnswer[4]; // per question: -1 = no answer, 0/1/2 = button index within group
|
||||
int tempSections; // current lit sections (0–8)
|
||||
bool gameWon;
|
||||
```
|
||||
|
||||
`tempSections = BASELINE_SECTIONS + wrong_count - correct_count`, clamped to [0, 8].
|
||||
|
||||
Win = all 4 `selectedAnswer[q] == CORRECT_ANSWER[q]`.
|
||||
|
||||
### Button mapping
|
||||
|
||||
```
|
||||
buttonIdx = question * 3 + answerWithinGroup
|
||||
question = buttonIdx / 3 (0..3)
|
||||
answer = buttonIdx % 3 (0..2)
|
||||
```
|
||||
|
||||
Correct answers are configured in `CORRECT_ANSWER[4]` — update these when the actual questions are finalised.
|
||||
|
||||
### Answer type system
|
||||
|
||||
Each question has three button types: `ANSWER_CORRECT`, `ANSWER_HALF`, `ANSWER_WRONG`.
|
||||
|
||||
In `ExploringUncertanties.ino`, find:
|
||||
|
||||
```cpp
|
||||
const int CORRECT_BUTTON[NUM_QUESTIONS] = {0, 1, 2, 0};
|
||||
const int HALF_BUTTON[NUM_QUESTIONS] = {1, 2, 0, 1};
|
||||
```
|
||||
|
||||
Each value (0, 1, or 2) is the button index within that question's group. The remaining button is automatically `ANSWER_WRONG`. Update both arrays when the content is finalised.
|
||||
|
||||
`getAnswerType(question, button)` returns the `AnswerType` enum value for any button press.
|
||||
|
||||
### Feedback animations
|
||||
|
||||
| Type | Animation | Temp effect |
|
||||
|---|---|---|
|
||||
| `ANSWER_CORRECT` | Green blink × 3 | −1 section |
|
||||
| `ANSWER_HALF` | Orange/yellow steady glow (700 ms) | +1 section |
|
||||
| `ANSWER_WRONG` | Red blink × 3 | +1 section |
|
||||
|
||||
After any feedback animation, the thermometer is immediately redrawn before the slide animation runs.
|
||||
|
||||
### Win animation
|
||||
|
||||
Rainbow HSV sweep forward then backward across all 60 logical positions (using `setMirroredLED`). Plays in a loop until the reset button is pressed.
|
||||
|
||||
### FastLED setup
|
||||
|
||||
```cpp
|
||||
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`.
|
||||
+122779
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
Binary file not shown.
Reference in new issue
Block a user