Adafruit PN532 NFC shield help.

Hey guys. i am working on a project with the adafruit nfc shield. but i have a problem. my program reads an nfc tag an places the read name from the tag into a queue. the problem is that if i remove a tag too quickly, before it has read it all, the arduino crashes and reboots.

to my knowledge, the nfc shield outputs high into digital pin 2 when there isn’t a readable card near it. so i think i can use this somehow, but i don’t know how to do it.

TL;DR
NFC shield crashes when i remove a tag, before it has read it all. how do i stop it from this?

byte namecheckerloadReady = 1;
byte namecheckerReady = 0;
byte writerReady = 0;
byte linecheckerReady = 0;
byte linenumbersetReady = 0;
byte clearerReady = 0;
byte linenumbersetdirty = 0;
byte readerReady = 1;
int freeline = 0;
int linetoclear = 0;
int nextlinenumber = 0;
int lastline = 0;
const byte entryCount = 17;
const byte entrySize = 19;
char inData[entryCount][entrySize]; 
char namecheck[entrySize];
char lineholder[entrySize];
byte namecheckindex = 0;
char inChar; 
byte index = 0; 
byte linenumber = 0;

#include <Mifare.h>
#include <NDEF.h>
#include <Wire.h>
#include <PN532_I2C.h>
#define IRQ   2
#define RESET 3
#define PAYLOAD_SIZE 224
PN532 * board = new PN532_I2C(IRQ, RESET);
Mifare mifare;
uint8_t Mifare::useKey = KEY_A;
uint8_t Mifare::keyA[6] = {
  0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
uint8_t Mifare::keyB[6] = {
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
uint32_t Mifare::cardType = 0;
uint8_t payload[PAYLOAD_SIZE] = {
};

void setup() {
  Serial.begin(115200);
  board->begin();
  uint32_t versiondata = board->getFirmwareVersion();
  if (! versiondata) {
    Serial.println("err");
    while (1);
  }
  if(mifare.SAMConfig()){
    Serial.println("ok");  
  }
  else{
    Serial.println("err");
  }
}


void writer()
{
  delay(1000);
  if ((linenumber<entryCount) && (index < entrySize))
  {
    for(byte i = 0; i < entrySize; i++)
    {
      inChar = namecheck[i];

      if (inChar == 0 ) 
      {
        inData[linenumber][index] = 0; 
        index = 0;
        writerReady = 0;
        namecheckerloadReady = 1;
        linenumber++;
        readerReady = 1;
        index = 0;
        linenumbersetdirty = 0;
        linenumberset();
        writerReady = 1;
        break;
      } 
      else
      {
        inData[linenumber][index] = inChar;
        index++; 
      }
    }
  }

}

void queuefix()
{
  delay(1000);
  for (byte i = linetoclear; i < entryCount; i++)
  {
    index = 0;
    do{
      inData[i][index] = 0;
      index++;
    } 
    while (index < entrySize);
    index = 0;
    if(inData[i+1][0] == 0)
    {
      break;
    }
    do
    {
      inData[i][index] = inData[i+1][index];
      index ++;
    } 
    while (inData[i+1][index] != 0);
    lastline = i;
  }    
  inData[lastline+1][0] = 0;
}


void linenumberset()
{
  delay(1000);
  for(byte i = 0; i < entryCount; i++)
  {

    if(strlen(inData[i]) == 0)  
    {

      if(linenumbersetdirty == 0)
      { 
        linenumber = i;
      }
      linenumbersetdirty = 1;
    }  

    if(i == (entryCount-1))
    {
      linenumbersetReady = 0;
      inChar = 0;
      printer();
      break;
    }
  }
  linenumbersetdirty = 1;
}

void namechecker()
{
  delay(1000);
  for(byte i = 0; i < entryCount; i++)
  {
    if(strcmp(inData[i], namecheck) == 0)
    {
      index = 0;
      linetoclear = i;
      do
      {
        inData[i][index] = 0;
        index++;
      } 
      while (index < entrySize);
      queuefix();
      linenumbersetdirty = 0;
      linenumberset();
      writerReady = 0;  
      namecheckerloadReady = 1;
      linecheckerReady = 1;
      readerReady = 1;
      break;
    }
    else
    {
      writerReady = 1;
      index = 0; 
    }
  }
  namecheckerReady = 0; 
  index = 0;
}


void printer()
{
  delay(1000);
  Serial.write(27);       // ESC command
  Serial.print("[2J");    // clear screen command
  Serial.write(27);
  Serial.print("[H");     // cursor to home command
  for(byte i = 0; i < entryCount; i++)
  { 
    if (inData[i][0] != 0)
    {
      Serial.print(i+1);
      Serial.print(". ");
      Serial.println(inData[i]);
    }
  }
  index = 0;
}


void reader()
{
  delay(20000);
  uint8_t * uid = mifare.readTarget();
  if(uid){    
    memset(payload, 0, PAYLOAD_SIZE);
    mifare.readPayload(payload, PAYLOAD_SIZE);
    FOUND_MESSAGE m = NDEF().decode_message(payload);     
    do
    {
      inChar = m.payload[index]; 
      namecheck[namecheckindex] = inChar; 
      namecheckindex++;
      index++;
      if(inChar == 13)
      {
        namecheck[namecheckindex] = 0;
        namecheckindex=0;
        namecheckerReady = 1;
        readerReady = 0; 
      }
      if(index > entrySize)
      {
        break;
      }
    }
    while (inChar != 13);              
    index = 0;  
  }
}


void clearer()
{
  delay(1000);
  do
  {
    namecheck[index] = 0;
    index++;
  } 
  while (index < entrySize);
  index = 0;
}

void loop() {

  if (readerReady == 1)
  {
    reader();
  }

  if (namecheckerReady == 1)
  {
    namechecker();
  }

  if (writerReady == 1)
  {
    writer(); 
  }

  if (linenumbersetReady == 1)
  {
    nextlinenumber = 30;
    index = 0;
    linenumberset(); 
    linenumbersetdirty = 1;
  }

  if (clearerReady == 1)
  {

  }
}

Output in putty:

1. Mark
2. Bert
3. franok

franok should be frank, but it gets interrupted, and then reboots, the “ok” comes from the serial print in the setup of the code.

any help? :slight_smile:

Thanks in advance.