Hi, I'm trying to make the RFID be able to turn on an LED upon tapping the key, and then turning it off by tapping the same key again. In the event that the 2nd tap never happens, I want the LED to turn off by itself after 2 minutes. However, I'm having issue with the 2nd part.
Right now, I'm only able to get the LED to turn off after tapping it a 2nd time. The LED does not turn off no matter how long I wait.
Could someone provide some guidance? I'm pretty new to Arduino as well so there might be some redundant code around somewhere.
#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
unsigned long LEDMillis = 0;
void setup(void) {
pinMode(LED, OUTPUT); // Declare the LED as an output
Serial.begin(115200); // Baud rate
while (!Serial) delay(10);
Serial.println("Hello!");
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.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)
// 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);
LEDMillis = millis();
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) == LOW) && (uid[0] == 0x33) && (uid[1] == 0x06) && (uid[2] == 0x64) && (uid[3] == 0x97)) { // ID: 0x33 0x06 0x64 0x97
previousMillis = currentMillis;
digitalWrite(LED, HIGH); // Turn the LED on
}
else if ((digitalRead(LED) == HIGH) && (currentMillis - previousMillis >= 1000) && (currentMillis - previousMillis < 120000) && (uid[0] == 0x33) && (uid[1] == 0x06) && (uid[2] == 0x64) && (uid[3] == 0x97)) {
digitalWrite(LED, LOW); // Tap again within 2 minutes to off
}
else if ((digitalRead(LED) == HIGH) && (currentMillis - previousMillis >= 120000)) {
digitalWrite(LED, LOW); // OR Off after 2 minutes
}
// 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);
}
}
}
}
}
}