Skip to content

Issue while reading RFID and detect removal #28

@ScreamZ

Description

@ScreamZ

Step 1: Describe your environment

  • OS version: N/A
  • Arduino IDE version: 2.2.1
  • MFRC522 Library version: 2.0.4
  • Arduino device: UNO
  • MFRC522 device: Standard blue card?

The Arduino was tested with VIN on 5V and then VIN on 10V. It's powered by VIN.

Step 2: Describe the problem

Affected file(s) or example(s):

#include <MFRC522v2.h>
#include <MFRC522DriverSPI.h>
#include <MFRC522DriverPinSimple.h>
#include <MFRC522Debug.h>

#define ENABLE_SERIAL
#define SS_PIN 10
#define RST_PIN 9
#define SIGNAL_PIN 7 // To propagate information to firmata arduino High is Valid medal detected Low otherwise
#define REFRESH_DELAY 500

byte MedalID[4] = {0x13, 0x76, 0x8E, 0x31}; // The ID to check for the medal
bool cardRemoved = false;
int counter = 0;
bool current, previous;

MFRC522DriverPinSimple ss_pin(10);
MFRC522DriverSPI driver{ss_pin};
MFRC522 mfrc522{driver};

void setup()
{
#ifdef ENABLE_SERIAL
  // Init RS232
  Serial.begin(115200);
  Serial.println("Initialization");
#endif

  // Init SPI bus
  SPI.begin();

  // Init MFRC522
  mfrc522.PCD_Init();

  // Init signal gate
  pinMode(SIGNAL_PIN, OUTPUT);
  pinMode(A0, OUTPUT);
  digitalWrite(SIGNAL_PIN, LOW);
}

void loop()
{
  blinkLED();
  // Look for new cards
  if (!mfrc522.PICC_IsNewCardPresent())
  {
#ifdef ENABLE_SERIAL
    Serial.println("Skipping, no new card present.");
#endif
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial())
  {
#ifdef ENABLE_SERIAL
    Serial.println("Skipping, unable to read card.");
#endif
    return;
  }

#ifdef ENABLE_SERIAL
  Serial.print("New RFID detected : ");
  PrintHex(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println("");
#endif

  digitalWrite(SIGNAL_PIN, VerifyRFID(MedalID, mfrc522.uid.uidByte) ? HIGH : LOW);

  // Check if Card was removed
  cardRemoved = false;
  counter = 0;

  previous = !mfrc522.PICC_IsNewCardPresent();

  while (!cardRemoved)
  {
    current = !mfrc522.PICC_IsNewCardPresent();

    if (current && previous)
      counter++;

    previous = current;
    cardRemoved = (counter > 2);
  }

#ifdef ENABLE_SERIAL
  Serial.println("RFID was removed");
#endif
  digitalWrite(SIGNAL_PIN, LOW);
  mfrc522.PICC_HaltA();
}

bool VerifyRFID(byte *RequiredID, byte *CheckedID)
{
  if ((RequiredID[0] == CheckedID[0]) && (RequiredID[1] == CheckedID[1]) && (RequiredID[2] == CheckedID[2]) && (RequiredID[3] == CheckedID[3]))
    return true;
  else
    return false;
}

#ifdef ENABLE_SERIAL
void PrintHex(uint8_t *data, uint8_t length) // prints 8-bit data in hex with leading zeroes
{
  char tmp[16];
  for (int i = 0; i < length; i++)
  {
    sprintf(tmp, "0x%.2X", data[i]);
    Serial.print(tmp);
    Serial.print(" ");
  }
}
#endif

const int ledPin = A0;            // The pin the LED is connected to
int ledState = LOW;               // Tracks the state of the LED
unsigned long previousMillis = 0; // Will store last time LED was updated
const long interval = 1000;       // Interval at which the LED should blink (milliseconds
void blinkLED()
{
  unsigned long currentMillis = millis(); // Get the current time

  if (currentMillis - previousMillis >= interval)
  {
    // Save the last time the LED blinked
    previousMillis = currentMillis;

    if (ledState == LOW)
    {
      ledState = HIGH; // If the LED is off, turn it on
    }
    else
    {
      ledState = LOW; // If the LED is on, turn it off
    }

    digitalWrite(ledPin, ledState); // Apply the LED state
  }
}

Observed Results:

The above code is working well but after some time (in minutes, hard to guess) seems to always return a LOW signal on SIGNAL_PIN. I added some test code with a blinking LED to detect whether it's stuck on the while clause or elsewhere. It seems that it's stuck after some time on if (!mfrc522.PICC_IsNewCardPresent()) as the LED keeps blinking, no matter if I move or not the RFID close to the sensor. When I restart the Arduino, things work again.

EDIT:
After some investigation, it looks like if it stays too long in the while loop, it goes back to the start of the loop function and never quits isNewCardPresent condition resulting in LOW signal
EDIT2: : Even out of the while loop (when a card is present waiting for removal), it goes into LOW signal

Expected Results:

When an RFID is read and as long it's not removed, I want to put my SIGNAL_PIN to HIGH. When it's removed or when the ID doesn't match my MedalID I want the SIGNAL_PIN to be low.

I don't like the way I'm forced to check card removal, and this random isNewCardPresent returning value is really annoying.

Am I supposed to use PICC_HaltA somewhere else ? Adding some pause time like 150 ms at begin of the loop doesn't seem to change the problem

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions