I currently using the Arduino Uno and PN532 RFID/NFC Shield by Adafruit. I am looking to read a RFID tag, store the ID (and later some data), and ignore tags that are previously stored. I know I am communicating with the RFID tags correctly and retrieving the IDs correctly.
I am using this global variable to store the tag IDs: uint8_t tagIDs[MAX_NUM_TAGS][TAG_SIZE];
I later plan to store the data in EEPROM, but just want to get it working using the global variable first.
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
const uint8_t MAX_NUM_TAGS = 10;
const uint8_t TAG_SIZE = 4;
uint8_t tagIDs[MAX_NUM_TAGS][TAG_SIZE];
uint8_t tagCount = 0;
I am using the following function to store only new tags. Each tag ID is an array of 4 unsigned 8-bit integers. To simplify my task, I am looking at only the first byte of each ID I scan. The first byte of the ID of the tag I am scanning is 0x04.
bool store_data(uint8_t* id)
{
bool newTag = false;
uint8_t i;
Serial.println(id[0]); // Prints value of 4 in my example (as expected)
Serial.println(tagIDs[0][0]); // Prints value of 0 in my example the first time I scan the tag, then 4 every other time. (as expected)
for (i=0; i<=tagCount; i++);
{
Serial.println(tagIDs[i][0]); //Prints value of 0 in my example after every scan (Why?) (not as expected)
if (id[0] == tagIDs[i][0])
{
Serial.println("old");
return newTag;
}
}
tagIDs[tagCount][0] = id[0];
Serial.print("id: ");Serial.println(id[0]); //Prints value of 4 (as expected)
Serial.print("tag: ");Serial.println(tagIDs[0][0]); //Prints value of 4 (as expected)
tagCount++;
newTag = true;
return newTag;
}
The line: if (id[0] == tagIDs[i][0])
is always false even though i = 0 for the first iteration.
If I change the code to: if (id[0] == tagIDs[0][0])
then it will be true after a scan a tag for a second time and return newTag = false as I expect.
Any help with fixing this would be much appreciated. Thanks