diff --git a/ExploringUncertanties.ino b/ExploringUncertanties.ino index 910fc56..90661d8 100644 --- a/ExploringUncertanties.ino +++ b/ExploringUncertanties.ino @@ -64,6 +64,11 @@ void setupNFC(Adafruit_PN532 nfc){ } +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 + + /**************************************************************************/ void setup(void) { @@ -112,9 +117,26 @@ void loop(void) { uint8_t uidLength1 = 0, uidLength2 = 0, uidLength3 = 0; // -------- Reader 1 -------- - if (getCardID(nfc, uid1, uidLength1)) { - lastSeen1 = now; - } +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)) { @@ -251,3 +273,32 @@ UIDResult checkUID(const uint8_t *uid, uint8_t uidLength) { Serial.println("unknown"); return UID_NONE; } + +void playWinAnimation() { + const int waitMs = 10; // delay between each LED step + static uint8_t hue = 0; // hue counter + + // Forward sweep + for (int i = 0; i < NUM_LEDS; i++) { + leds[i] = CHSV(hue++, 255, 255); // light up the LED with current hue + FastLED.show(); + fadeAllSweep(); // fade all LEDs slightly + delay(waitMs); + } + + // Backward sweep + for (int i = NUM_LEDS - 1; i >= 0; i--) { + leds[i] = CHSV(hue++, 255, 255); // light up the LED with current hue + FastLED.show(); + fadeAllSweep(); // fade all LEDs slightly + delay(waitMs); + } +} + +// exactly like your previous 'fadeall()' +void fadeAllSweep() { + for (int i = 0; i < NUM_LEDS; i++) { + leds[i].nscale8(250); // dim LEDs gradually + } +} +