I am trying to detect a RFID card using the NFC-PN532 module and Adafruit_PN532 library.
Initialization code:-
#include <Adafruit_PN532.h>
#include <Wire.h>
#define SCK (13)
#define MISO (12)
#define MOSI (11)
#define SS (10)
Adafruit_PN532 nfc(SCK, MISO, MOSI, SS);
void setup()
{
Serial.begin(9600);
Serial.println("Initializing please wait.......");
delay(3000);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata)
{
Serial.print("Didn't find PN53x board");
while (1); // halt
}
Serial.print("Device Found PN5 Chip");
Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware version > ");
Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.println((versiondata>>8) & 0xFF, DEC);
nfc.SAMConfig(); //Set to read RFID tags
Serial.println("Waiting for RFID Card ...");
}
Main loop:-
void loop()
{
uint8_t success = 0;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success)
{
Serial.println("Card Detected!");
}
else
{
Serial.println("Card Not Detected or Removed!");
}
}
Whenever i place a valid card on the PN532, Card Detected! is printed on the serial monitor.
However, when i remove the card, Card Not Detected or Removed! does not get printed.
How can i make the program to loop in such a way that it can detect when the card is present/removed?
loopafter card was present, ... try addingnfc.setPassiveActivationRetries(0x10);to your setup just beforenfc.SAMConfig();- this should stop the code waiting for a card :p