RFID Checking

Hi all.
Im using the following code to read an RFID tag. I want to make sure that I print only one message when a new card is present, and not send the same message constantly. Then, when the tag is detached, the system will present another message.
Do you think the code is efficient enough or maybe there could be another better way to do this checking?

#include <SPI.h>
#include <RFID.h>
#define IRQ 6
#define SS_PIN 10
#define RST_PIN 9
int serNum[5];

int counterOn, counterOff, counter;
String idString = "";
String deviceNum = "1, ";
String deviceOn = deviceNum + "1, ";
String deviceOff = deviceNum + "0, ";

void setup() {
  // start serial

  while (!Serial);



void loop() {

  if (rfid.isCard()) {
    if (rfid.readCardSerial()) {
      counterOff = 0;
      idString = "";
      for (int i = 0; i <= 4; i++) {
        idString += rfid.serNum[i];
      if (counterOn == 1) {
        //1 is the number of current sensor device
        String fullMessage = deviceOn + idString;
  } else  {
    if (idString != "") {
      counterOn = 0;
      if (counterOff == 1) {
        String fullMessage = deviceOff + idString;


RFID readers usually do not return the same ID continually, anyway.

But how can I detect the OFF event then? That is when the card is removed.

I would have to run in the loop() to make sure that the card is present there all times. Then, when it is removed to notify with another event.

Why do you want to?

I want to make sure that when the card remains attached specific things happen in my application.
When it is not, the application deactivates. Therefor, I need to constantly check the reader.
For this reason I made a counter, and on the first addition the code checks and verifies as a valid ON message. If the card is attached for longer, the counter will return false, so it will not send one more ON message. The same is done with the OFF event.
But I am not sure if this is the right or best way to validate this. That’s why I am asking if someone else knows a better tip.

I still haven't found a way to check the RFID card as the reader gives me unstable responses. When the card is attached it is read normally, but then suddenly gives me an OFF event. Is there a way to properly filter/configure the readings?

Someone with some RFID experience can share their thoughts?


Someone with some RFID experience can share their thoughts?

People with RFID readers usually know what kind of reader they have. They usually understand what the reader does. Typically, an RFID reader detects the presence of a card, and reads it ONCE. Nothing else happens until that card goes out of range and then comes back into range. Typically, the reader does NOT do anything when the card goes out of range.

You haven't bothered to tell us what RFID reader you have, so YOU need to read the datasheet to figure out exactly what you can, and can NOT do, with your particular reader.