#include <avr/sleep.h>
#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>
#include <Servo.h>
#include <SPI.h>
#define IRQ (2)
#define SS_PIN (6) // Not connected by default on the NFC Shield
Adafruit_NFCShield_I2C nfc(IRQ, SS_PIN);
Servo deadbolt;
const int locked = 90;
const int unlocked = 180;
int pos;
int wakePin = 3;
int sleepStatus = 0;
//int count = 0; //delays do not work with timing when running a loop
/*blinkWithoutDelay*/
const int ledPin = 13;
int ledState = LOW;
long previousMillis = 0;
long interval = 1000;
void wakeUpNow() {
}
void setup(void) {
pinMode(wakePin, INPUT);
pinMode(ledPin, OUTPUT);
attachInterrupt(1, wakeUpNow, HIGH);
Serial.begin(9600); //originally set to 115200
Serial.println("Looking for PN532...");
deadbolt.attach(9);
deadbolt.write(locked);
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.println("Everything checks out."); //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 Identification Card ...");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void sleepNow() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(1, wakeUpNow, HIGH);
deadbolt.detach();
sleep_mode();
sleep_disable();
detachInterrupt(1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop(void) {
//Serial.println(count); //count prints, but only when a MiFare card is within range
pos = deadbolt.read();
byte j_card[4] = { 0xCD, 0xBF, 0xC1, 0x78 };
volatile boolean valid;
uint8_t success; // Flag to check if there was an error with the PN532
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 currentblock; // Counter to keep track of which block we're on
bool authenticated = false; // Flag to indicate if the sector is authenticated
uint8_t data[16]; // Array to store block data during reads
// Keyb on NDEF and Mifare Classic should be the same
const uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// 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) { //upon any 4k card within range
//const int locked = 90;
//const int unlocked = 180;
if ( pos == locked ) { // depends on deadbolt: switch 90 and 180 in the declartions of the poisitions above ^^^ before void setup(){}
deadbolt.write(unlocked);
delay(100);
return;//commenting these out so the loop runs continuously
}
else if ( pos == unlocked ) {
deadbolt.write(locked);
delay(100);
return; //commenting these out so the loop runs continuously
}
}
//following code is from the blinkWithoutDelay example from Arduino
///////////////////////////////////////////////////////////////////
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Serial.println(previousMillis);
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
//loop runs every 20ms for 500 times = 10 seconds
//if ( count >= 500) {
// Serial.println("Timer: Entering Sleep Mode");
// count = 0;
// sleepNow();
//}
}