Dear people,
I'm trying to build a alarm that uses nfc tags. The nfc tags need to be introduced to the nfc card reader within 10 seconds after the door has opened. if not, the relays will switch and the alarm/lights will go crazy.
I have been trying for a good 3 hours to get the 10 seconds timer to work, but I just can't get it to work. I did my research on millis() and the example codes. I need to be able to use the card reader and let the timer count up to 10 at the same time (for obvious reasons).
Whatever I do, the relays always switch after 1 second after I opened the door (sensor).
please, can someone help me?
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3)
// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
const int sensor = 8; // door sensor input pin
int doorstate; // 0 close - 1 open wwitch
int state; // statemachine
//this is for the blinking led:
const int ledPin = LED_BUILTIN; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
//this is for the alarm timer:
// I have no idea...
void setup(void)
{
timeStart = 0;
pinMode(4, OUTPUT); // sets the digital pin 4 as output for relay 1
pinMode(5, OUTPUT); // sets the digital pin 5 as output for relay 2
pinMode(6, OUTPUT); // sets the digital pin 6 as output RED LED
pinMode(7, OUTPUT); // sets the digital pin 7 as output GREEN LED
pinMode(13, OUTPUT); // sets the digital pin 13 as output TEST LED
pinMode(sensor, INPUT_PULLUP); // sets pullup resistor on for sensor
state = 1; // starts machine with first state
}
void loop (void)
{
if (state == 1)
{
digitalWrite(4, HIGH); // turn relay 1 off
digitalWrite(5, HIGH); // turn relay 2 off
Serial.begin(115200);
Serial.println("Hello!");
Serial.println("Alarmsystem booting up");
Serial.println("Alarm status: UNARMED");
Serial.println("Alarm will be armed when door is closed");
for (int i = 0; i <= 1; i++) // 10 seconds bootup (when set to 9)
{
digitalWrite(7, HIGH); // turn green light on
delay(500);
digitalWrite(7, LOW); // turn green light off
digitalWrite(6, HIGH); // turn red light on
delay(500);
digitalWrite(6, LOW); // turn red light off
}
doorstate = digitalRead(sensor); // check the status of the door (open or closed)
while (doorstate == HIGH) // door OPENED
{
doorstate = digitalRead(sensor); // check the status of the door (open or closed)
if (doorstate == HIGH) // door OPENED
{
Serial.println("Door is opened");
delay(1000);
}
}
Serial.println("Door is closed");
Serial.println("Alarmsystem booted");
state = 2;
}
else if (state == 2)
{
unsigned long currentMillis = millis(); //fancy light flashing while idle (nothing is happening and alarm is armed)
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite(6, ledState);
}
doorstate = digitalRead(sensor); // check the status of the door (open or closed)
if (doorstate == HIGH) // door OPENED
{
Serial.println("Door is opened");
digitalWrite(6, LOW); // turn red light off
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
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();
Serial.println("Waiting for an ISO14443A Card ...");
//----------the countdown (or countup) for the NFC scanner----------
if (?????????? ) //if the NFC tag is not scanned in time (10 sec.)
{
Serial.println("NFC not detected or too late");
Serial.println("ALARM TRIGGERD");
Serial.println("Activating light and horn");
digitalWrite(4, LOW); // turn relay 1 on
digitalWrite(5, LOW); // turn relay 2 on
}
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)
uint8_t mifareclassic_ReadDataBlock;
// 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) {
// Display some basic information about the card
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print(" UID Value: ");
nfc.PrintHex(uid, uidLength);
if (uidLength == 4)
{
// We probably have a Mifare Classic card ...
uint32_t cardid = uid[0];
cardid <<= 8;
cardid |= uid[1];
cardid <<= 8;
cardid |= uid[2];
cardid <<= 8;
cardid |= uid[3];
Serial.print("Seems to be a Mifare Classic card #");
Serial.println(cardid);
//------------------Checking if the nfc card is matching with data base entrys------------------//
if (cardid == 2861708310)
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
Serial.println("Detected card id:");
Serial.println(cardid);
Serial.println("NFC card: 1");
Serial.println("Alarm has been Dissarmed");
state = 3;
}
else if (cardid == 2864376086)
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
Serial.println("Detected card id:");
Serial.println(cardid);
Serial.println("NFC card: 2");
Serial.println("Alarm has been Dissarmed");
state = 3;
}
else if (cardid == 3403313686)
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
Serial.println("Detected card id:");
Serial.println(cardid);
Serial.println("NFC card: 3");
Serial.println("Alarm has been Dissarmed");
state = 3;
}
else if (cardid == 2863228950)
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
Serial.println("Detected card id:");
Serial.println(cardid);
Serial.println("NFC card: 4");
Serial.println("Alarm has been Dissarmed");
state = 3;
}
else if (cardid == 3392821014)
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
Serial.println("Detected card id:");
Serial.println(cardid);
Serial.println("NFC card: 5");
Serial.println("Alarm has been Dissarmed");
state = 3;
}
}
}
}
}
else if (state == 3)
{
digitalWrite(7, HIGH); // turn green light on
delay(500);
Serial.println("Waiting for door to be closed again...");
doorstate = digitalRead(sensor);
if (doorstate == LOW) // door CLOSED
{
Serial.println("Door is closed");
Serial.println("Alarm status: ARMED");
state = 2;
digitalWrite(7, LOW); // turn green light off
}
}
else if (state == 4)
{
}
}