0

I am trying to build a mobile device that turns itself on when removed from its usual location. I am attempting to do this with an Adafruit PN532 RFID reader and a MiFare card. Using the sample code I can detect when the card is present

success= nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
Serial.println("Scanning");
if (success){
  Serial.println("FOUND");
}

However, when I try to detect the absence of the card using the following code it will tell me "Present" over and over (since its in the Loop) while the RFID card is present but when it is removed it stops printing "Present" but never prints "Card Gone"

success= nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
    Serial.println("Present");
    if (!success){
      Serial.println("Card Gone");
    }

Is this a function of how Im using ! or is there something else at play here? I have found very few suggestions regarding detecting the absence of an RFID card and none of them seem to provide any useful answer. Most suggest some other hardware methodology as the solution. That is not the type of answer Im interested in for the moment. I would simply like to figure out why my code is incorrect.

  • Post the whole function, at least, not this snippet. There is no context. – SoreDakeNoKoto Mar 9 '16 at 23:13
2

First, your code isn't structured properly. It should be:

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

if (success)
  Serial.println("Present");
else
  Serial.println("Card Gone");

From what I could find in the library, the reason you aren't getting either the 'Card Gone' or 'Present' message when there is no card is because nfc.readPassiveTargetID() is a blocking function. It will wait forever until a tag is detected, at which point it returns to the caller. When you bring a card within the reader's range, the function returns quickly and loop() runs smoothly, giving you the 'Present' message over and over again. When you remove the card however, on the next call of nfc.readPassiveTargetID(), the function tries to locate a card but finds none and then it keeps trying and does not return. So loop() never gets past that point, until you bring a card within range again.

To detect the presence or absence of something, an observer must observe for some pre-determined period of time before making a decision. So, I suggest you get the latest version of this library which gives you the option of adding a milliseconds timeout argument to nfc.readPassiveTargetID(). That way, you can specify how long you want the function to try detecting a card before concluding there is no card and returning.

Alternatively, you could use your existing function as is but in setup() you need to set the maximum number of re-tries before the nfc.readPassiveTargetID() returns. This is done with setPassiveActivationRetries(). Any argument from 0x00 to 0xFE indicates a finite number of tries, while 0xFF is to wait forever:

nfc.setPassiveActivationRetries(0x9A); // for example
nfc.SAMConfig();  // this comes next

Good luck.

| improve this answer | |
  • thank you for the great explanation and thoughts on approaches to address the issue. One question before I try this. From your explanation, it seems the "if (success)" line is redundant? – techkilljoy Mar 10 '16 at 16:13
  • @techkilljoy In your code, it is redundant because the function only ever returns if it finds a card. However, if you make these changes, it will become possible for the function to return 0 after trying a few times and finding no card. A card detected would result in a return value of 1 though – SoreDakeNoKoto Mar 10 '16 at 16:17
0

You can edit the .cpp file and add a timeout. I did this to remove the blocking scan. Search for TIMEOUT in the Adafruit_PN532 source code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.