Your code is overly complicated. I've tried to simplify. Note the turn off delay is 15 seconds to make it easier to test.
#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 ...");
}
boolean onLED = false;
unsigned long onTime = 0;
uint32_t MAX_ON_TIME = 15000;
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)
// Turn off LED if it has been on for more the maximum time.
if (onLED && millis() > onTime + MAX_ON_TIME)
{
onLED = false;
onTime = 0;
digitalWrite(LED, LOW);
}
// 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};
// Is this the correct card?
if (uid[0] == 0x33 && uid[1] == 0x06 && uid[2] == 0x64 && uid[3] == 0x97)
{
if (onLED) // Already on, so turn off.
{
onLED = false;
onTime = 0;
digitalWrite(LED, LOW);
}
else // Already off, so turn on.
{
onLED = true;
onTime = millis();
digitalWrite(LED, HIGH);
}
}
delay(500); // Just in case the card keeps getting read again & again.
// // 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);
// }
// }
}
}
}