#include "SPI.h"
#include "MFRC522.h"
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
#define RFID_NR 2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
delay(4); // Optional delay. Some board do need more time after init to be ready, see Readme
mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}
byte nuidPICC[RFID_NR][4];
int rfid_nr = 0;
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
bool compareBytes(byte current[4] , byte last[4])
{
for (byte i = 0; i < 4; i++) {
if( current[i] != last[i])
return false;
}
return true;
}
void addNewCard(MFRC522 mfrc522)
{
for (byte i = 0; i < 4; i++) {
nuidPICC[rfid_nr][i] = mfrc522.uid.uidByte[i];
}
Serial.println(F("The NUID tag is:"));
Serial.print(F("In hex: "));
printHex(nuidPICC[rfid_nr], mfrc522.uid.size);
if(rfid_nr > 0 )
{
if (!compareBytes( nuidPICC[rfid_nr], nuidPICC[0]))
rfid_nr++;
}else
{
rfid_nr++;
}
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
if(rfid_nr < RFID_NR)
{
addNewCard(mfrc522);
}else{
byte currentCard[4];
for (byte i = 0; i < 4; i++) {
currentCard[i] = mfrc522.uid.uidByte[i];
}
if (compareBytes(currentCard, nuidPICC[0]) || compareBytes(currentCard, nuidPICC[1]))
{
Serial.println("Komnata tajemnic stoi otworem!!!");
}
else
{
Serial.println("Komnata tajemnic jest dalej zamknieta!!!");
}
}
}
/*
* Po przylozeniu klucza do czytnika program wyswietla na monitorze portuszeregowego informacje o kluczu PICC.
* Podaje UID, SAK, typ i blok danych
*/
// https://github.com/miguelbalboa/rfid/blob/master/examples/DumpInfo/DumpInfo.ino