-2

I am reading RFID cards and trying to compare the ID's as once the card passes the reader, the reader actually reads it more than once so I need to discard the duplicates.

UPDATED : The following code gets the cards ID's

uint32_t IndentifyTag(Adafruit_PN532 rfidReader)
{
    uint8_t tagID[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
    uint8_t tagIDLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    uint32_t cardid = tagID[0];
    boolean success = rfidReader.readPassiveTargetID(PN532_MIFARE_ISO14443A, &tagID[0], &tagIDLength);
    if (success) {
        // Display some basic information about the card for testing
        Serial.println("Found an ISO14443A card");
        Serial.print("  UID Length: "); Serial.print(tagIDLength, DEC);     Serial.println(" bytes");
        Serial.print("  UID Value: "); rfidReader.PrintHex(tagID, tagIDLength);

        if (tagIDLength == 4)
        {
            // Mifare Classic card 
            cardid <<= 8;
            cardid |= tagID[1];
            cardid <<= 8;
            cardid |= tagID[2];
            cardid <<= 8;
            cardid |= tagID[3];
            Serial.print("Mifare Classic card #");
            Serial.println(cardid);
        }
        Serial.println("");
    }
    return cardid;
}

And in a switch statement's case, I am testing the tags for equality as follows:

uint32_t priorTag = 0000000;
tag = IndentifyTag(entryRFIDReader);

if (tag == priorTag)
{               
    Serial.println("These tags are the same!!!");
}
if (tag != priorTag)
{
    Serial.println("I got here!");
    tagCount += 1;
}
priorTag = tag;
SSerial.println("tagCount = "); Serial.println(tagCount);

The problem is even if the reader reads the same card 3 or 4 times in one pass, they are never equal and therefore the tagCounter is counting the dupes. Both tag and priorTag are of type uint32_t so should be comparable with ==, but it is not working in this context.

Found an ISO14443A card
UID Length: 4 bytes
UID Value: 0x92 0x77 0x88 0xBB
Mifare Classic card #7833787

I got here!
tagCount = 1
Found an ISO14443A card
UID Length: 4 bytes
UID Value: 0x92 0x77 0x88 0xBB
Mifare Classic card #7833787

I got here!
tagCount = 2
Found an ISO14443A card
Mifare Classic card #7833787

I got here!
tagCount = 3
Found an ISO14443A card
Mifare Classic card #7833787

I got here!
tagCount = 4
Disillusioned
  • 14,635
  • 3
  • 43
  • 77
dinotom
  • 4,990
  • 16
  • 71
  • 139
  • 1
    The relevant part of the code is missing. We don't see where priorTag is declared. Maybe it is declared inside the loop so that it always has the value 0 at the beginning of each loop iteration. – NineBerry May 03 '16 at 23:40
  • @NineBerry...its declared, you just missed it. – dinotom May 03 '16 at 23:47
  • 1
    No. You misunderstood. The question is where it is declared in the context of the code. – NineBerry May 03 '16 at 23:50
  • Recommend rerunning your test and updating the output now that you've made significant changes to how the output is generated. We don't see "These tags are the same!!!" or "I got here!" in the output. Is this accurate? – user4581301 May 03 '16 at 23:50
  • 1
    If you want assistance debugging your code, please have the courtesy to provide the ***actual code*** and sample output that ***needs debugging***. 1) Your sample output fails to show either "These tags are the same" or "I got here". 2) You moved the line `priorTag = tag` when the obvious was pointed out. – Disillusioned May 03 '16 at 23:51
  • @NineBerry you are correct. The declaration needed to be moved out of the switch statement. They are now comparing. Post as answer and Ill accept – dinotom May 03 '16 at 23:56
  • What this still doesn't show is how you get around `uint32_t priorTag = 0000000;` And never mind. Moot comment – user4581301 May 03 '16 at 23:57
  • @CraigYoung...the actual code was posted, and then updated as the debugging statements I put in PRIOR to posting were in wrong place as noted by Sam Varshavchik, thus the UPDATED in the post. NineBerry had the answer regardless – dinotom May 03 '16 at 23:59
  • 2
    ***TIP for future debugging:*** When you output "I got here" because `tag != priorTag`, it's useful to also output the values of `tag` and `priorTag`. It would have quickly revealed what was going wrong. – Disillusioned May 04 '16 at 00:03
  • @CraigYoung....duly noted – dinotom May 04 '16 at 01:10

2 Answers2

4
priorTag = tag;
Serial.println("******************************");
Serial.print("Prior tag: "); Serial.println(priorTag);
Serial.print("Current tag: "); Serial.println(tag); 

Can you explain how you could possibly ever see two different values printed here, when these four lines of code get executed?

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Good point, I added those serial statements to see whats going on and inadvertently put them in wrong place, regardless even though the tag numbers are the same they are NOT comparing with == – dinotom May 03 '16 at 23:44
  • 1
    Cosmic ray strike flipped a bit. Unlikely, but possible. – user4581301 May 03 '16 at 23:44
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/12234877) – James Youngman May 04 '16 at 07:12
  • @JamesYoungman This ***did provide*** an answer to the question, until the ***question was changed***. – Disillusioned May 04 '16 at 22:19
1

The variable priorTag is declared too local, inside the loop you have. So it always has the value 0 at the beginning of each iteration.

Move the declaration of the variable priorTag outside the loop.

NineBerry
  • 26,306
  • 3
  • 62
  • 93