That code will never do what you want it to because each time it enters one of those “while true” loops it totally ignores the other reader (and anything else).
Each time through loop(), for each reader, you need to determine whether there is a new card present. If there is, print a message and record that a card is present.
If there is not, check whether there was a card on it last time through loop(). If so and the card is not still there print a message and record that a card is no longer present.
I do not have that library and so cannot compile code. There are no comments in your code and I don’t know why values of 13 or 14 mean that a card is still present. The code below might give you an ideas how to better structure your code to pay attention to more than one reader.
#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 )
{
// 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
}