diff --git a/ExploringUncertanties.ino b/ExploringUncertanties.ino index 6cdff7a..910fc56 100644 --- a/ExploringUncertanties.ino +++ b/ExploringUncertanties.ino @@ -92,97 +92,65 @@ boolean getCardID(Adafruit_PN532 &nfc, uint8_t *uid, uint8_t &uidLength) { return success; } +const unsigned long CARD_TIMEOUT_MS = 2000; + +unsigned long lastSeen1 = 0; +unsigned long lastSeen2 = 0; +unsigned long lastSeen3 = 0; - UIDResult result1 = UID_NONE; - UIDResult result2 = UID_NONE; - UIDResult result3 = UID_NONE; -/**************************************************************************/ void loop(void) { -delay(50); // Static + delay(50); + const int8_t DEFAULT_TEMP = 24; + int8_t temperature = DEFAULT_TEMP; - //RFID readers - boolean success1; - boolean success2; - boolean success3; - uint8_t uid1[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - uint8_t uid2[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - uint8_t uid3[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - uint8_t uidLength1 = 7; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) - uint8_t uidLength2 = 7; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) - uint8_t uidLength3 = 7; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) + unsigned long now = millis(); + uint8_t uid1[7], uid2[7], uid3[7]; + uint8_t uidLength1 = 0, uidLength2 = 0, uidLength3 = 0; - //Reader 01-------------- - success1 = getCardID(nfc, uid1, uidLength1); - success2 = getCardID(nfc2, uid2, uidLength2); - success3 = getCardID(nfc3, uid3, uidLength3); - - int8_t temperature = 24; - - if(result1 == UID_RIGHT){ - temperature = temperature - 6; - }else if (result1 == UID_WRONG) { - temperature = temperature + 6; - } - else { - } - - if(result2 == UID_RIGHT){ - temperature = temperature - 6; - }else if (result2 == UID_WRONG) { - temperature = temperature + 6; - } - else { - } - if(result3 == UID_RIGHT){ - temperature = temperature - 6; - }else if (result3 == UID_WRONG) { - temperature = temperature + 6; - } - else { - } - - if (success1) { - Serial.println("First reader found a card!"); - Serial.print("UID Length: ");Serial.print(uidLength1, DEC);Serial.println(" bytes"); - Serial.print("UID Value: "); - for (uint8_t i=0; i < uidLength1; i++) - { - Serial.print(" 0x");Serial.print(uid1[i], HEX); - } - Serial.println(""); - result1 = checkUID(uid1, uidLength1); + // -------- Reader 1 -------- + if (getCardID(nfc, uid1, uidLength1)) { + lastSeen1 = now; } - if (success2) { - Serial.println("Second reader found a card!"); - Serial.print("UID Length: ");Serial.print(uidLength2, DEC);Serial.println(" bytes"); - Serial.print("UID Value: "); - for (uint8_t i=0; i < uidLength2; i++) - { - Serial.print(" 0x");Serial.print(uid2[i], HEX); - } - Serial.println(""); - result2 = checkUID(uid2, uidLength2); + // -------- Reader 2 -------- + if (getCardID(nfc2, uid2, uidLength2)) { + lastSeen2 = now; } - if (success3) { - Serial.println("Third reader found a card!"); - Serial.print("UID Length: ");Serial.print(uidLength3, DEC);Serial.println(" bytes"); - Serial.print("UID Value: "); - for (uint8_t i=0; i < uidLength3; i++) - { - Serial.print(" 0x");Serial.print(uid3[i], HEX); - } - Serial.println(""); - result3 = checkUID(uid3, uidLength3); + // -------- 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 @@ -200,34 +168,17 @@ void displayTemp(uint8_t temp) { // 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 pos = map(i, 18, 53, 0, 255); uint8_t eased = ease8InOutQuad(pos); - CRGB color = blend(blue, red, eased); - - uint8_t scale; - if (i < brightEnd) { - scale = BRIGHT_SCALE; // temp-controlled brightness - } else { - scale = (i < 36) ? BRIGHT_SCALE : DIM_SCALE; // original behavior - } - - color.nscale8_video(scale); - leds[i] = color; - } - - // Top 6 LEDs solid RED - for (int i = 54; i < NUM_LEDS; i++) { - CRGB color = red; + CRGB color = blend(blue, red, eased); uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE; color.nscale8_video(scale); @@ -235,9 +186,18 @@ void displayTemp(uint8_t temp) { 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(); } + // ---- Known UIDs ---- // Right answers (4-byte UIDs)