Qwiic RFID wrong readings after 20 reads

I have taken some time to simulate the FIFO and the 2 pointers: NewestTag OldestTag as defined in the sketch :SparkFun_Qwiic_RFID_ID-XXLA/Firmware/ATtiny85_Firmware/Qwiic_RFID_IDXXLA/Qwiic_RFID_IDXXLA.ino at master · sparkfun/SparkFun_Qwiic_RFID_ID-XXLA · GitHub

It fails..
The problem related to the moment the pointers return to zero !

MAX_TAG_STORAGE is defined as 20 and thus positions 0 - 19.

The FIFO is defined as:

struct {
  byte tagID[6]; //The Uniqe identifier of the RFID tag, converted from ASCII to HEX will be six bytes in size.
  unsigned long tagTime; //When was the RFID tag sensed?
} tagEvent[MAX_TAG_STORAGE];

The turnaround pointer check is done AFTER the a new tag has been added:
if( newestTag++ == MAX_TAG_STORAGE ) newestTag = 0;

As newestTag is increment AFTER the compare it can contain position 20 to store the next TAG. This is beyond the FIFO size and a such will overwrite memory that is used for other variables. The result is undefined.. but not good…

Changing the turn around check to BEFORE doing the check, solves the issue
if( ++newestTag == MAX_TAG_STORAGE ) newestTag = 0;

btw the same should be done for OldestTag :
if (++oldestTag == MAX_TAG_STORAGE) oldestTag = 0;

Attached the sketch I used to simulate.
RFID_QWIIC_FIFO.txt (3.2 KB)

2 Likes