Milestone: Quiz 1 done. 3 NFC readers are connected. If reader 1 can read a UID the temperature decreases on all other readers it increases. Temperature is displayed on single LED strip

This commit is contained in:
Maira Bradler committed 2026-01-29 00:38:09 +01:00
1 parent a84ba0e60a
commit b075f473f7
1 file changed
+47 -87
+47 -87
View File
@@ -92,97 +92,65 @@ boolean getCardID(Adafruit_PN532 &nfc, uint8_t *uid, uint8_t &uidLength) {
return success; 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) { void loop(void) {
delay(50); // Static delay(50);
const int8_t DEFAULT_TEMP = 24;
int8_t temperature = DEFAULT_TEMP;
//RFID readers unsigned long now = millis();
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)
uint8_t uid1[7], uid2[7], uid3[7];
uint8_t uidLength1 = 0, uidLength2 = 0, uidLength3 = 0;
//Reader 01-------------- // -------- Reader 1 --------
success1 = getCardID(nfc, uid1, uidLength1); if (getCardID(nfc, uid1, uidLength1)) {
success2 = getCardID(nfc2, uid2, uidLength2); lastSeen1 = now;
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){ // -------- Reader 2 --------
temperature = temperature - 6; if (getCardID(nfc2, uid2, uidLength2)) {
}else if (result2 == UID_WRONG) { lastSeen2 = now;
temperature = temperature + 6;
}
else {
}
if(result3 == UID_RIGHT){
temperature = temperature - 6;
}else if (result3 == UID_WRONG) {
temperature = temperature + 6;
}
else {
} }
if (success1) { // -------- Reader 3 --------
Serial.println("First reader found a card!"); if (getCardID(nfc3, uid3, uidLength3)) {
Serial.print("UID Length: ");Serial.print(uidLength1, DEC);Serial.println(" bytes"); lastSeen3 = now;
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);
} }
if (success2) { // -------- Presence check (2s timeout) --------
Serial.println("Second reader found a card!"); bool reader1Active = (now - lastSeen1) < CARD_TIMEOUT_MS;
Serial.print("UID Length: ");Serial.print(uidLength2, DEC);Serial.println(" bytes"); bool reader2Active = (now - lastSeen2) < CARD_TIMEOUT_MS;
Serial.print("UID Value: "); bool reader3Active = (now - lastSeen3) < CARD_TIMEOUT_MS;
for (uint8_t i=0; i < uidLength2; i++)
{ if (reader1Active) {
Serial.print(" 0x");Serial.print(uid2[i], HEX); Serial.println("Reader 1 active");
} temperature -= 6;
Serial.println("");
result2 = checkUID(uid2, uidLength2);
} }
if (success3) { if (reader2Active) {
Serial.println("Third reader found a card!"); Serial.println("Reader 2 active");
Serial.print("UID Length: ");Serial.print(uidLength3, DEC);Serial.println(" bytes"); temperature += 6;
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); if (reader3Active) {
Serial.println("Reader 3 active");
temperature += 6;
} }
temperature = constrain(temperature, 0, NUM_LEDS - 12);
displayTemp(temperature); displayTemp(temperature);
} }
void displayTemp(uint8_t temp) { void displayTemp(uint8_t temp) {
// Clamp temp so we don’t overflow the strip // Clamp temp so we don’t overflow the strip
@@ -200,10 +168,8 @@ void displayTemp(uint8_t temp) {
// Bottom 6 LEDs solid BLUE // Bottom 6 LEDs solid BLUE
for (int i = 12; i < 18; i++) { for (int i = 12; i < 18; i++) {
CRGB color = blue; CRGB color = blue;
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE; uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
color.nscale8_video(scale); color.nscale8_video(scale);
leds[i] = color; leds[i] = color;
} }
@@ -214,30 +180,24 @@ void displayTemp(uint8_t temp) {
uint8_t eased = ease8InOutQuad(pos); uint8_t eased = ease8InOutQuad(pos);
CRGB color = blend(blue, red, eased); 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;
uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE; uint8_t scale = (i < brightEnd) ? BRIGHT_SCALE : DIM_SCALE;
color.nscale8_video(scale); color.nscale8_video(scale);
leds[i] = color; 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(); FastLED.show();
} }
// ---- Known UIDs ---- // ---- Known UIDs ----
// Right answers (4-byte UIDs) // Right answers (4-byte UIDs)