add winning animation
This commit is contained in:
1 parent
b075f473f7
commit
b758784969
1 file changed
+52
-1
@@ -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) {
|
||||
|
||||
@@ -113,9 +118,26 @@ void loop(void) {
|
||||
|
||||
// -------- Reader 1 --------
|
||||
if (getCardID(nfc, uid1, uidLength1)) {
|
||||
lastSeen1 = now;
|
||||
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;
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user