I've placed print commands at every digitalWrite command to print numbers from 1 to 4.
I've also changed the timer to 3000ms for testing purposes.
The output is as follows:
The number 2 is printed whenever I scan the key for the 1st time. This part is working as intended.
However, for the 2nd scan to off the LED, the output prints the numbers 1 and 3, depending on the duration between the 1st and 2nd scan. The number 1 appears when the duration between both scans are over 3 seconds, while the number 3 appears when the duration is less than 3 seconds.
The LED does not off by itself at all, only when I've scanned it a 2nd time.
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#define PN532_SCK (2)
#define PN532_MOSI (3)
#define PN532_SS (4)
#define PN532_MISO (5)
#define LED 13 // LED connected to pin 13
// Defining pins for I2C for shield
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
// For breakout/shield with I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
// Declaring last recorded time (previousMillis) and latest recorded time (currentMillis)
unsigned long previousMillis = 0;
unsigned long currentMillis = millis(); // millis() is a timer function
void setup(void) {
pinMode(LED, OUTPUT); // Declare the LED as an output
Serial.begin(115200); // Baud rate
while (!Serial) delay(10);
nfc.begin();
// Shield detection
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) { // If board not connected or wrong board
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// If board connected
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.setPassiveActivationRetries(0x9A);
nfc.SAMConfig();
// Standby mode
Serial.println("Waiting for an ISO14443A Card ...");
}
void loop(void) {
uint8_t success;
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)
if ((digitalRead(LED) == 1) && (currentMillis - previousMillis >= 3000)) {
digitalWrite(LED, 0); // OR Off after 2 minutes
Serial.print("1");
}
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
nfc.PrintHex(uid, uidLength); // Displays ID of card/tag
Serial.println("");
if (uidLength == 4) {
// Card ID
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
currentMillis = millis(); // Defining current time. Timer starts here
// Turn LED on for 1st RFID key
if ((digitalRead(LED) == 0) && (uid[0] == 0x33) && (uid[1] == 0x06) && (uid[2] == 0x64) && (uid[3] == 0x97)) { // ID: 0x33 0x06 0x64 0x97
previousMillis = currentMillis;
digitalWrite(LED, 1); // Turn the LED on
Serial.print("2");
}
else if ((digitalRead(LED) == 1) && (currentMillis - previousMillis >= 1000) && (currentMillis - previousMillis < 3000) && (uid[0] == 0x33) && (uid[1] == 0x06) && (uid[2] == 0x64) && (uid[3] == 0x97)) {
digitalWrite(LED, 0); // Tap again within 2 minutes to off
Serial.print("3");
}
// Start with block 4 (the first block of sector 1) cos sector 0 contains manufacturer data
success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
if (success) {
uint8_t data[16];
// Reading the contents of block 4
success = nfc.mifareclassic_ReadDataBlock(4, data);
if (success)
{
delay(1000);
}
}
}
}
}