miguelbalboa / rfid

Arduino RFID Library for MFRC522
The Unlicense
2.74k stars 1.43k forks source link

card removal for multiple readers #534

Closed Herodotmaximus94 closed 3 years ago

Herodotmaximus94 commented 3 years ago

Step 1: My environment

Step 2: My problem

I try to use the MFRC522 to read NTag216 tags and print serial, if the read tags are equal to my known tags. It works right, if I just try to track, when new tags are read. If I try to track on missing tags, a problem is occurring. Am I missing a check function for the reader or is something else wrong with my coding?

Relevant Code:


#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9          
#define SS_1_PIN        10         //SS pin for reader 1
#define SS_2_PIN        8          //SS pin for reader 2

#define NR_OF_READERS   2
#define SIZE(array) sizeof(array)/sizeof(array[0])

struct Tag {
  byte uuid[7];
  char name[16];
  unsigned int battery_size;
};

struct Tag database[ ] { //my known id´s
  {{0x04, 0x41,0xC1,0xD2,0x2F,0x66,0x81}, "5", 0},{{0x04,0x30,0xDB,0x02,0x2B,0x5E,0x80},"6",1},{{0x04, 0x3A,0xD6,0x02,0x2B,0x5E,0x80},"7",2}
  };

struct Tag* lookup(byte* uuid) {
  for(unsigned int i=0; i<SIZE(database); i++) {
    if(memcmp(database[i].uuid, uuid, 7) == 0) {
      return &database[i];
    }
  }
  return NULL;
}
char buffer[32];
byte ssPins[ ] = {SS_1_PIN, SS_2_PIN};

MFRC522 mfrc522[NR_OF_READERS];   // Create MFRC522 instance.

void setup() {

  Serial.begin(9600); // Initialize serial communications with the PC
  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();
  }
}

void loop() {

  bool cardremoved;

      for(uint8_t reader=0;reader<NR_OF_READERS;reader++){

          if(mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()){    
             struct Tag* match = lookup(mfrc522[reader].uid.uidByte);

             if(match == NULL) {
              Serial.println ("no matching ID");
              return;
              }else {
              Serial.print(match->name);
              Serial.print("#1#1\n");
                }

/****************************** my problem occures down here***********************************************/
          }else if(!mfrc522[reader].PICC_IsNewCardPresent()){
            struct Tag* match = lookup(mfrc522[reader].uid.uidByte);

            if(match == NULL) {
              cardremoved=true; 
            }else {
             cardremoved=false;
            }
            if(cardremoved){
               Serial.println(cardremoved +" "+reader);
            }
          }
            // Halt PICC
          mfrc522[reader].PICC_HaltA();
            // Stop encryption on PCD
          mfrc522[reader].PCD_StopCrypto1();
     }

}
Rotzbua commented 3 years ago

}else if(!mfrc522[reader].PICC_IsNewCardPresent()){

Seems redundant. Just else should do it.

            if(match == NULL) {
              cardremoved=true; 
            }else {
             cardremoved=false;
            }
            if(cardremoved){

Seems redundant. Could be simplified to one if.

return NULL; For pointers use nullptr.

If tag removed the mfrc522[reader].uid is not deleted or cleaned. So your second part does not work.