I'm currently working on a school project for which I need to detect when a RFID card is present on a reader and when it is removed. To do this, I bought several MFRC522 readers and some cards.
I already achieved to detect when a single tag is present and when it is removed from the reader, but now I want to detect when the user place or remove a tag on multiple readers, at the same time.
I don't know if it's physically possible with this reader, but I found the below code on another forum, and it looks correct.
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
constexpr uint8_t SS_1_PIN = 10; // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
constexpr uint8_t SS_2_PIN = 8; // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
constexpr uint8_t NR_OF_READERS = 2;
byte ssPins[] = {SS_1_PIN, SS_2_PIN};
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
bool cardPresent[NR_OF_READERS]; // true => card is present on reader
//*****************************************************************************************//
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
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
Serial.print(F("Reader "));
Serial.print(reader);
Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();
// init card present flag
cardPresent[reader] = false;
}
}
uint8_t control = 0x00;
//*****************************************************************************************//
void loop() {
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
// Look for new card on this reader
bool newCard = mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial();
// if a new card is present
if ( newCard && !cardPresent[reader] )
{
// mark this reader as having a card present
cardPresent[reader] == true;
Serial.println(reader);
Serial.println("NewCard ");
} else if ( cardPresent[reader] ) {
// there was a card on the reader, is it still there?
if ( !checkReader(reader) )
{
// no, print message and mark as not present
Serial.print(reader);
Serial.println(" CardRemoved");
delay(500); //change value if you want to read cards faster
// clear card present flag
cardPresent[reader] = false;
} // if
} // else
mfrc522[reader].PICC_HaltA();
mfrc522[reader].PCD_StopCrypto1();
}
}
// Return true if there is a card on the passed reader
//
bool checkReader(int reader)
{
control = 0;
for (int i = 0; i < 3; i++) {
if (!mfrc522[reader].PICC_IsNewCardPresent()) {
if (mfrc522[reader].PICC_ReadCardSerial()) {
//Serial.print('a');
control |= 0x16;
}
if (mfrc522[reader].PICC_ReadCardSerial()) {
//Serial.print('b');
control |= 0x16;
}
//Serial.print('c');
control += 0x1;
}
//Serial.print('d');
control += 0x4;
}
//Serial.println(control);
return (control == 13 || control == 14);
//card is still there
}
I run it and it correcly detect when a card is detected, but for some reasons it doesn't detect when i remove the tag. Looks like it never goes into the "else if ( cardPresent[reader] )" condition.
I don't understand why.