I keep getting this error, but not sure why. Can anyone figure it out? I am trying to compare an RFID input to an array of allowed tag IDs, and then reset the sensed ID each iteration. But resetting the uid value seems to be causing the error, but I don't know why. Any thoughts?
// This project is to use an RFID reader (PN532 chipset) with a 16x2 LCD display to control "something"
// What that something is... well that is up to you.
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <PN532_SWHSU.h>
#include <PN532.h>
SoftwareSerial SWSerial( 3, 2 ); // RX, TX
PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );
String tagId = "None", dispTag = "None"; // global variable to store tag ID as a string
byte nuidPICC[4]; // global variable to store tag ID as an array of 4 bytes
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define authorized tags. Change the Allowed ID Count based on the number needed, and the code will adapt.
// If ARRAYSIZE is larger than the permitted number of tags, then the system will be more prone to hacking.
#define ALLOWEDIDCOUNT 2
byte allowedId[ALLOWEDIDCOUNT][4] =
{
{0xEE, 0xAD, 0xEF, 0xF1}, // 238.173.239.241
{0xCE, 0x2F, 0xF3, 0xF1}, // 206.47.243.241
};
bool allowed = false;
void setup(void)
{
Serial.begin(115200);
Serial.println("Hello Maker!");
// Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata)
{
Serial.print("Didn't Find PN53x Module");
while (1); // Halt
}
// Got valid data, print it out!
Serial.print("Found chip PN5");
Serial.println((versiondata >> 24) & 0xFF, HEX);
Serial.print("Firmware ver. ");
Serial.print((versiondata >> 16) & 0xFF, DEC);
Serial.print('.');
Serial.println((versiondata >> 8) & 0xFF, DEC);
// Configure board to read RFID tags
nfc.SAMConfig();
//Serial.println("Waiting for an ISO14443A Card ...");
// initialize the LCD
lcd.init();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello Ross,");
lcd.setCursor(0,1);
lcd.print("Setup Complete.");
delay(2000); // 2 second wait
lcd.noBacklight();
}
void loop()
{
// Reset variables in case last iteration read an allowed ID
nuidPICC = [0x00, 0x00, 0x00, 0x00];
// Read RFID tag if present
readNFC();
// Check to see if read tag (or default value) match an allowed ID
// Sets global "allowed" boolean. LEFT OFF HERE. NOT YET WORKING
checkNFC();
if (allowed)
// do something for allowed tag
Serial.println("Arrays Match");
else
// output denial message
Serial.println("No Match");
}
}
void readNFC()
{
boolean success = false; //init variable to start the WHILE loop below
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
while !success {
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (!success)
{
Serial.println("Waiting for RFID Tag...");
delay(500); // .5 second delay
}
}
// Loop has exited, which means a card was read
Serial.print("UID Length: ");
Serial.print(uidLength, DEC);
Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i = 0; i < uidLength; i++)
{
nuidPICC[i] = uid[i];
Serial.print(" "); Serial.print(uid[i], DEC);
}
Serial.println();
tagId = tagToString(nuidPICC);
dispTag = tagId;
Serial.print(F("tagId is : "));
Serial.println(tagId);
Serial.println("");
lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Tag ID: ");
lcd.setCursor(0,1);
lcd.print(tagId);
delay(2000); // 2 second wait
lcd.noBacklight();
}
void checkNFC()
{
allowed = false;
for (int i = 0; i < ALLOWEDIDCOUNT; i++) {
Serial.print("Testing Match with allowedID ");
Serial.println(i);
if (memcmp(nuidPICC, allowedID[i], 4) == 0)
Serial.println("Arrays Match");
allowed = true;
else
Serial.println("No Match");
}
}
String tagToString(byte id[4])
{
String tagId = "";
for (byte i = 0; i < 4; i++)
{
if (i < 3) tagId += String(id[i]) + ".";
else tagId += String(id[i]);
}
return tagId;
}
Thanks.