Getting notification when an RFID is removed from a reader

I’m doing a project using the sparkfun RFID shield SparkFun RFID Evaluation Shield - 13.56MHz - DEV-10406 - SparkFun Electronics and RFID module https://www.sparkfun.com/products/10126.

In the project, when a RIFD tag is placed on the reader and sends out a serial.println - this is pretty standard and already works in the sample code. What I want to do however is have it send a new Serial message when the tag is removed. I’m not to sure about how to go about doing this and any help would be really appreciated.

I’ve added the sparkfun sample code that I’m currently using below:

#include <SoftwareSerial.h>

int incomingByte = 0;

SoftwareSerial rfid(7, 8);
SoftwareSerial xbee(10, 9);

//Prototypes
void check_for_notag(void);
void halt(void);
void parse(void);
void print_serial(void);
void read_serial(void);
void seek(void);
void set_flag(void);

//Global var
int flag = 0;
int Str1[11];

//INIT
void setup()
{
  Serial.begin(9600);
  Serial.println("Start");

  // set the data rate for the SoftwareSerial ports
  xbee.begin(9600);
  rfid.begin(19200);
  delay(10);
  halt();
}

//MAIN
void loop()
{
  read_serial();
}

void check_for_notag()
{
  
  seek();
  delay(10);
  parse();
  set_flag();

  if(flag = 1){
    seek();
    delay(10);
    parse();
  }

}

void halt()
{
 //Halt tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)147);
  rfid.write((uint8_t)148);
}

void parse()
{
  while(rfid.available()){
    if(rfid.read() == 255){
      for(int i=1;i<11;i++){
        Str1[i]= rfid.read();
      }
    }
  }
}

void print_serial()
{
  if(flag == 1){
    //print to serial port
    Serial.print(Str1[8], HEX);
    Serial.print(Str1[7], HEX);
    Serial.print(Str1[6], HEX);
    Serial.print(Str1[5], HEX);
    Serial.println();
    
    check_for_notag();
  }

  
}

void read_serial()
{
  seek();
  delay(10);
  parse();
  set_flag();
  print_serial();
  delay(100);
}

void seek()
{
  //search for RFID tag
  rfid.write((uint8_t)255);
  rfid.write((uint8_t)0);
  rfid.write((uint8_t)1);
  rfid.write((uint8_t)130);
  rfid.write((uint8_t)131);
  delay(10);
}

void set_flag()
{
  if(Str1[2] == 6){
    flag++;
  }
  if(Str1[2] == 2){
    flag = 0;
  }
}

Check to see if pin 16 "OUTPUT OK" (Arduino Pin 12) stays high as long as the card is present.

So ended up solving this myself with some help from a friend of mine (I now know a TON about the HEX codes the board is using and the Str1 array in general).

But long story short in

void set_flag()
{
  if(Str1[2] == 6){ // returning tag
    flag++;
  }
  if(Str1[2] == 2){ // processing or error
    flag = 0;

the (Str1[2] == 2) is sent by the RFID reader if it is processing or there is an “error”. This includes the condition where there is no RFID tag on the reader.

To make it only alert me of this condition right after the RFID is removed I set up a boolean in the set_flag() and in the print_serial function, so that flipped when a tag is placed on the reader or immediately after.

I also converted the HEX array for the RFID serial # into a string so I could output it, which also might be handy

The individual pieces of code are below, but if anyone wants I can paste the whole thing:

boolean newTag = true;
char chArray[] = "";
String RFIDserial(chArray);
void print_serial()
{
  if(flag == 1){

    RFIDserial = ""; // This resets the string to empty each time you place a new RFID on the reader
    
    for(int i = 0; i < 4; i++){
      
      int p = 8 - i;
      
      char b[16];  // just sets up the buffer so you know you have enough space
      sprintf(b, "%X", Str1[p]); //converts the HEX to a string
      RFIDserial += b;  //adds the next piece in the RFID serial #
      newTag = false;  // Sets the boolean so that the when you remove the tag it triggers the if statement in set_flag()
    }
    Serial.print("RFID ");
    Serial.print(RFIDserial);
    Serial.println(" has connected!"); // outputs the RFID serial    
  }
}
void set_flag()
{
  if(Str1[2] == 6){         // returning tag
    flag++;
  }
  if(Str1[2] == 2){           // processing or error
    flag = 0;
    if (newTag == false){      // checks to make sure the RFID has placed on the reader
      Serial.print("The RFID ");
      Serial.print(RFIDserial);
      Serial.println(" has left the station!");
      newTag = true;            // flips the boolean so it won't output again until an RFID is placed on the reader
    }
  }
}