#include #include #include // Define pins #define RST_PIN 1 // Reset pin for RC522 #define SS_RC522 13 // Chip select for RC522 #define SS_PN532 13 // Chip select for PN532 #define BUZZER_PIN 2 // Buzzer pin // RFID readers Adafruit_PN532 nfc(SS_PN532); MFRC522 mfrc522(SS_RC522, RST_PIN); // Function prototypes void playTone(int frequency, int duration, int times); bool detectRC522(); bool detectPN532(); void printUID(uint8_t *uid, uint8_t uidLength); //set the booleans for the readers so it only has to set once during setup and can use these later on bool rc522Connected = false; bool pn532Connected = false; void setup() { Serial.begin(115200); pinMode(BUZZER_PIN, OUTPUT); // Initialize SPI bus SPI.begin(); // Detect which reader is connected rc522Connected = detectRC522(); if(rc522Connected){ playTone(1000, 100, 3); // Three beeps for RC522 Serial.println("RC522 detected."); } else{ //no rc522 :( pn532Connected = detectPN532(); if(pn532Connected){ Serial.println("RC522 detected."); playTone(1000, 100, 2); // Two beeps for PN532 } else{ //no reader detected at all, long beep repeatedly every 3 seconds while (true){ playTone(1000, 500, 1); // Long beep if no reader is found Serial.println("No RFID reader detected."); delay(3000); //wait 3 seconds and do the beep/serial log again, in case it was missed the first time? might be too annoying so move outside the loop otherwise. }; // Halt execution if no reader is found } } } void loop() { // Check if PN532 is connected and try to read if (pn532Connected) { uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength)) { Serial.print("PN532 UID: "); printUID(uid, uidLength); playTone(1000, 100, 1); // Single beep for a successful read } } // Check if RC522 is connected and try to read else if (rc522Connected) { mfrc522.PCD_Init(); // Ensure RC522 is initialized for every cycle if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) { Serial.print("RC522 UID: "); printUID(mfrc522.uid.uidByte, mfrc522.uid.size); playTone(1000, 100, 1); // Single beep for a successful read mfrc522.PICC_HaltA(); // Halt PICC (Tag) } } delay(500); // Small delay to avoid continuous reading } // Function to detect RC522 bool detectRC522() { mfrc522.PCD_Init(); // Initialize the RC522 delay(100); // Check if the VersionReg is readable byte version = mfrc522.PCD_ReadRegister(MFRC522::VersionReg); return (version != 0 && version != 0xFF); // Return true if detected } // Function to detect PN532 bool detectPN532() { nfc.begin(); delay(100); // Try to get the firmware version uint32_t version = nfc.getFirmwareVersion(); return (version); // Return true if detected } // Function to print UID void printUID(uint8_t *uid, uint8_t uidLength) { for (uint8_t i = 0; i < uidLength; i++) { Serial.print(uid[i] < 0x10 ? " 0" : " "); Serial.print(uid[i], HEX); } Serial.println(); } // Function to make a tone sound with custom frequency, duration, and repeat count void playTone(int frequency, int duration, int times) { for (int i = 0; i < times; i++) { tone(BUZZER_PIN, frequency); // Play tone on buzzer delay(duration); // Tone duration noTone(BUZZER_PIN); // Stop tone delay(100); // Delay between tones } }