RFID Card Removal

Hey Guys,
I am designing a project on arduino and I am using RFID RC522 readers. For my programm I need to detect when a card has been removed and restart the process of vaidation when a card is removed from the reader.

void loop() {

  while (isreading0 == false || isreading1 == false || isreading2 == false || isreading3 == false) {
    //if a new tag is on the reader then read it
    if (rfidReader0.PICC_IsNewCardPresent() && rfidReader0.PICC_ReadCardSerial()) {
      isreading0 = true;
      Serial.println(isreading0);
      readOnReader0();
      Serial.println(rfidReader0.PICC_IsNewCardPresent() && rfidReader0.PICC_ReadCardSerial());
      Serial.println(rfidReader0.PICC_IsNewCardPresent());
       Serial.println(rfidReader0.PICC_ReadCardSerial());
    }

    if (rfidReader1.PICC_IsNewCardPresent() && rfidReader1.PICC_ReadCardSerial()) {
      isreading1 = true;
      readOnReader1();
    }

  giveFeedback();
}
}


/**
   There are 4 readers: 0,1,2,3
   If a new tag is present on this reader, read it and save its ID as strUID0
*/
void readOnReader0() {
  //strUID0={0,0,0,0};
  Serial.print(F("Reader 0 "));
  Serial.print(F(": Card UID:"));
  printDec(rfidReader0.uid.uidByte, rfidReader0.uid.size);
  Serial.println();
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfidReader0.PICC_GetType(rfidReader0.uid.sak);
  Serial.println(rfidReader0.PICC_GetTypeName(piccType));
  Serial.println("this is the byteid rean on reader 0");
 
  for (int i = 0; i < 4; i++) {
   strUID0[i] = rfidReader0.uid.uidByte[i];
   Serial.print(strUID0[i]);
  }
  stopReading(rfidReader0);

}


void readOnReader1() {
  Serial.print(F("Reader1 "));
  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F(": Card UID:"));
  printDec(rfidReader1.uid.uidByte, rfidReader1.uid.size);
  Serial.println();
  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfidReader1.PICC_GetType(rfidReader1.uid.sak);
  Serial.println(rfidReader1.PICC_GetTypeName(piccType));
  Serial.println("this is the byteid");
 
  for (int i = 0; i < 4; i++) {
    strUID1[i] = rfidReader1.uid.uidByte[i];
    Serial.print(strUID1[i]);
  }
  stopReading(rfidReader1);

}
/**
   stop reading the card after it is read one time on the reader.
*/
void stopReading(MFRC522 reader) {
  delay(200); //change value if you want to read cards faster
  reader.PICC_HaltA();
  reader.PCD_StopCrypto1();
}


void giveFeedback() {
  if (isreading0 && isreading1 && isreading2 && isreading3) {
    Serial.println("all are reading, so we can validate");
    if (validateTags(strUID0, strUID2, strUID3)) {
      blinkLedPos0();
    } else blinkLedNeg0();

    isreading0 = false;
    isreading1 = false;
    isreading2 = false;
  } else {
    Serial.print("There was a problem while reading");
  }
}

bool validateTags(byte fstNo[], byte sndNo[], byte result[]) {
//process of validation is done here
}

Right now if I place a card on each of the readers the validation starts. And only if I remove BOTH cards the validation proccess will stop. But I want the validation to be interrupted even when just One a card was removed. I think I might have to use these two functions in a smarter way:

PICC_IsNewCardPresent() 
 rfidReader1.PICC_ReadCardSerial()

But I am not sure how they work.
I really appreciate your help.
Thanks

That is not a complete sketch. Please post the entire sketch.

You mention 2 readers, but the comments and code suggest 4 - which is correct? Wrong comments are worse than no comments

You don't need a while() loop inside your entire loop() function - It will loop like it is designed to do.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.