Mesh networked computer terminals with RFID logon - part 6

Going back a few weeks I was having trouble with stability of the RFID readers. Which I've now fixed with a far less complicated solution.

The library has an option to turn the RFID antenna on and off. Simply shutting it off and turning back on to check the card periodically seems a 100% stable solution.

Obviously you should also check the card hasn't been swapped by checking the ID hasn't changed but here is a a minimal sketch to do this. As checking for a card stops it appearing as new it's easy to get the logic messed up but this is a tested and working example.

Note to self: start putting stuff on GutHub.


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

const uint8_t SS_PIN = D8;    //My example code is for a WeMos D1 mini, change these to match your setup
const uint8_t RST_PIN = D0;   //My example code is for a WeMos D1 mini, change these to match your setup
 
MFRC522 rfid(SS_PIN, RST_PIN);

bool cardPresentWhenLastChecked = false;
bool antennaEnabled = true;
uint32_t cardCheckTimer = 0;

void setup()
  Serial.begin(115200);
  SPI.begin();
  rfid.PCD_Init();
  Serial.println(F("Checking for RFID card removal"));
}
 
void loop()
{
  if(millis() > cardCheckTimer)
  {
    //Start a check of the card
    if(antennaEnabled == false)
    {
        //Turn the antenna back on
        rfid.PCD_AntennaOn();
        antennaEnabled = true;
        //It takes time to wake up the RFID card so the sketch needs to wait before checking for it
        cardCheckTimer = millis() + 20ul;
    }
    else if(antennaEnabled == true)
    {
      if(millis() > cardCheckTimer)
      {
        //Check for a card after a delay for it to power up
        if(rfid.PICC_IsNewCardPresent() == true)
        {
          if(cardPresentWhenLastChecked == false)
          {
            //Card was absent but has been presented
            Serial.println(F("Card presented"));
            cardPresentWhenLastChecked = true;
          }
        }
        else if(rfid.PICC_IsNewCardPresent() == false && cardPresentWhenLastChecked == true)
        {
          //The card was present but has been removed
          Serial.println("Card removed");
          cardPresentWhenLastChecked = false;
        }
        //Switch off the antenna, otherwise the card will not show as 'new' when checked again
        rfid.PCD_AntennaOff();
        antennaEnabled = false;
        //Wait before checking the card again
        cardCheckTimer = millis() + 100ul;
      }
    }
  }
}

No comments: