Add function on/off light support camera
This commit is contained in:
parent
d702b4018a
commit
732bd382ce
727
Lock/Lock.ino
727
Lock/Lock.ino
|
@ -46,6 +46,27 @@
|
||||||
// #include <Servo.h>
|
// #include <Servo.h>
|
||||||
|
|
||||||
//#define COMMON_ANODE
|
//#define COMMON_ANODE
|
||||||
|
// Ver 1
|
||||||
|
//#ifdef COMMON_ANODE
|
||||||
|
//#define LED_ON LOW
|
||||||
|
//#define LED_OFF HIGH
|
||||||
|
//#else
|
||||||
|
//#define LED_ON HIGH
|
||||||
|
//#define LED_OFF LOW
|
||||||
|
//#endif
|
||||||
|
//
|
||||||
|
//#define Light 7
|
||||||
|
//#define ledView 13
|
||||||
|
//
|
||||||
|
//#define redLed 3 // Set Led Pins
|
||||||
|
//#define greenLed 6
|
||||||
|
//#define blueLed 5
|
||||||
|
//
|
||||||
|
//#define relay A2 //8 // Set Relay Pin
|
||||||
|
//#define wipeB 2 // Button pin for WipeMode
|
||||||
|
//#define button 7 // button exit
|
||||||
|
/ --------------------------------------------
|
||||||
|
//Ver 2
|
||||||
|
|
||||||
#ifdef COMMON_ANODE
|
#ifdef COMMON_ANODE
|
||||||
#define LED_ON LOW
|
#define LED_ON LOW
|
||||||
|
@ -55,15 +76,19 @@
|
||||||
#define LED_OFF LOW
|
#define LED_OFF LOW
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define ledView 13
|
#define redLed 4 // Set Led Pins
|
||||||
|
#define greenLed 2
|
||||||
|
#define blueLed 3
|
||||||
|
|
||||||
#define redLed 3 // Set Led Pins
|
#define relay 5 //6 // Set Relay Pin
|
||||||
#define greenLed 6
|
#define wipeB A0 // Button pin for WipeMode
|
||||||
#define blueLed 5
|
#define button A1 // button exit
|
||||||
|
#define Light 7 // Led tro sang camera
|
||||||
|
|
||||||
#define relay A2 //8 // Set Relay Pin
|
|
||||||
#define wipeB 2 // Button pin for WipeMode
|
|
||||||
#define button 7 // button exit
|
|
||||||
|
bool Light_stt = false;
|
||||||
bool relay_stt = false;
|
bool relay_stt = false;
|
||||||
bool sttRun = false;
|
bool sttRun = false;
|
||||||
bool programMode = false; // initialize programming mode to false
|
bool programMode = false; // initialize programming mode to false
|
||||||
|
@ -85,6 +110,7 @@ bool Debug = false;
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(13, OUTPUT);
|
pinMode(13, OUTPUT);
|
||||||
|
pinMode(Light, OUTPUT);
|
||||||
digitalWrite(13, LOW);
|
digitalWrite(13, LOW);
|
||||||
//Arduino Pin Configuration
|
//Arduino Pin Configuration
|
||||||
pinMode(ledView, OUTPUT);
|
pinMode(ledView, OUTPUT);
|
||||||
|
@ -108,7 +134,7 @@ void setup()
|
||||||
//Protocol Configuration
|
//Protocol Configuration
|
||||||
Serial.begin(9600); // Initialize serial communications with PC
|
Serial.begin(9600); // Initialize serial communications with PC
|
||||||
|
|
||||||
/*
|
|
||||||
SPI.begin(); // MFRC522 Hardware uses SPI protocol
|
SPI.begin(); // MFRC522 Hardware uses SPI protocol
|
||||||
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
|
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
|
||||||
if (Debug == true)
|
if (Debug == true)
|
||||||
|
@ -122,7 +148,7 @@ void setup()
|
||||||
|
|
||||||
//Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
|
//Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM
|
||||||
if (digitalRead(wipeB) == LOW)
|
if (digitalRead(wipeB) == LOW)
|
||||||
{ // when button pressed pin should get low, button connected to ground
|
{ // when button pressed pin should get low, button connected to ground
|
||||||
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
|
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
|
||||||
if (Debug == 1)
|
if (Debug == 1)
|
||||||
{
|
{
|
||||||
|
@ -183,7 +209,7 @@ void setup()
|
||||||
delay(200);
|
delay(200);
|
||||||
} while (!successRead); // Program will not go further while you not get a successful read
|
} while (!successRead); // Program will not go further while you not get a successful read
|
||||||
for (uint8_t j = 0; j < 4; j++)
|
for (uint8_t j = 0; j < 4; j++)
|
||||||
{ // Loop 4 times
|
{ // Loop 4 times
|
||||||
EEPROM.write(2 + j, readCard[j]); // Write scanned PICC's UID to EEPROM, start from address 3
|
EEPROM.write(2 + j, readCard[j]); // Write scanned PICC's UID to EEPROM, start from address 3
|
||||||
}
|
}
|
||||||
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
|
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
|
||||||
|
@ -199,7 +225,7 @@ void setup()
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint8_t i = 0; i < 4; i++)
|
for (uint8_t i = 0; i < 4; i++)
|
||||||
{ // Read Master Card's UID from EEPROM
|
{ // Read Master Card's UID from EEPROM
|
||||||
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
|
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
|
||||||
if (Debug == 1)
|
if (Debug == 1)
|
||||||
{
|
{
|
||||||
|
@ -217,9 +243,10 @@ void setup()
|
||||||
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
|
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
|
||||||
// }
|
// }
|
||||||
// wdt_enable(WDTO_500MS);
|
// wdt_enable(WDTO_500MS);
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
//void SerialRead();
|
||||||
|
//void ReadButton();
|
||||||
///////////////////////////////////////// Main Loop ///////////////////////////////////
|
///////////////////////////////////////// Main Loop ///////////////////////////////////
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
@ -227,7 +254,7 @@ void loop()
|
||||||
{
|
{
|
||||||
ReadButton();
|
ReadButton();
|
||||||
SerialRead();
|
SerialRead();
|
||||||
/*
|
|
||||||
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
|
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
|
||||||
// When device is in use if wipe button pressed for 10 seconds initialize Master Card wiping
|
// When device is in use if wipe button pressed for 10 seconds initialize Master Card wiping
|
||||||
if (digitalRead(wipeB) == LOW)
|
if (digitalRead(wipeB) == LOW)
|
||||||
|
@ -245,7 +272,7 @@ void loop()
|
||||||
|
|
||||||
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
|
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
|
||||||
if (buttonState == true && digitalRead(wipeB) == LOW)
|
if (buttonState == true && digitalRead(wipeB) == LOW)
|
||||||
{ // If button still be pressed, wipe EEPROM
|
{ // If button still be pressed, wipe EEPROM
|
||||||
EEPROM.write(1, 0); // Reset Magic Number.
|
EEPROM.write(1, 0); // Reset Magic Number.
|
||||||
if (Debug == 1)
|
if (Debug == 1)
|
||||||
{
|
{
|
||||||
|
@ -327,77 +354,87 @@ void loop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
|
||||||
// wdt_reset();
|
|
||||||
//}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Access Granted ///////////////////////////////////
|
}
|
||||||
void granted(uint16_t setDelay)
|
// wdt_reset();
|
||||||
{
|
//}
|
||||||
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
|
|
||||||
digitalWrite(redLed, LED_OFF); // Turn off red LED
|
|
||||||
digitalWrite(greenLed, LED_ON); // Turn on green LED
|
|
||||||
digitalWrite(relay, LOW); // Unlock door!
|
|
||||||
digitalWrite(ledView, LOW);
|
|
||||||
delay(setDelay); // Hold door lock open for given seconds
|
|
||||||
digitalWrite(relay, HIGH); // Relock door
|
|
||||||
digitalWrite(ledView, HIGH);
|
|
||||||
delay(200); // Hold green LED on for a second
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Access Denied ///////////////////////////////////
|
///////////////////////////////////////// Access Granted ///////////////////////////////////
|
||||||
void denied()
|
// Turn on Light camera
|
||||||
{
|
void TurnOn_Light() {
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(Light, LOW);
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
Light_stt = true;
|
||||||
digitalWrite(redLed, LED_ON); // Turn on red LED
|
}
|
||||||
delay(1000);
|
void TurnOff_Light() {
|
||||||
}
|
digitalWrite(Light, HIGH);
|
||||||
|
Light_stt = false;
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
|
void granted(uint16_t setDelay)
|
||||||
uint8_t getID()
|
{
|
||||||
{
|
digitalWrite(blueLed, LED_OFF); // Turn off blue LED
|
||||||
// Getting ready for Reading PICCs
|
digitalWrite(redLed, LED_OFF); // Turn off red LED
|
||||||
if (!mfrc522.PICC_IsNewCardPresent())
|
digitalWrite(greenLed, LED_ON); // Turn on green LED
|
||||||
{ //If a new PICC placed to RFID reader continue
|
digitalWrite(relay, LOW); // Unlock door!
|
||||||
return 0;
|
digitalWrite(ledView, LOW);
|
||||||
}
|
delay(setDelay); // Hold door lock open for given seconds
|
||||||
if (!mfrc522.PICC_ReadCardSerial())
|
digitalWrite(relay, HIGH); // Relock door
|
||||||
{ //Since a PICC placed get Serial and continue
|
digitalWrite(ledView, HIGH);
|
||||||
return 0;
|
delay(200); // Hold green LED on for a second
|
||||||
}
|
}
|
||||||
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
|
|
||||||
// I think we should assume every PICC as they have 4 byte UID
|
///////////////////////////////////////// Access Denied ///////////////////////////////////
|
||||||
// Until we support 7 byte PICCs
|
void denied()
|
||||||
// Serial.println(F("Scanned PICC's UID:"));
|
{
|
||||||
for (uint8_t i = 0; i < 4; i++)
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
{ //
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
readCard[i] = mfrc522.uid.uidByte[i];
|
digitalWrite(redLed, LED_ON); // Turn on red LED
|
||||||
// Serial.print(readCard[i], HEX);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
|
||||||
|
uint8_t getID()
|
||||||
|
{
|
||||||
|
// Getting ready for Reading PICCs
|
||||||
|
if (!mfrc522.PICC_IsNewCardPresent())
|
||||||
|
{ //If a new PICC placed to RFID reader continue
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (!mfrc522.PICC_ReadCardSerial())
|
||||||
|
{ //Since a PICC placed get Serial and continue
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
|
||||||
|
// I think we should assume every PICC as they have 4 byte UID
|
||||||
|
// Until we support 7 byte PICCs
|
||||||
|
// Serial.println(F("Scanned PICC's UID:"));
|
||||||
|
for (uint8_t i = 0; i < 4; i++)
|
||||||
|
{ //
|
||||||
|
readCard[i] = mfrc522.uid.uidByte[i];
|
||||||
|
// Serial.print(readCard[i], HEX);
|
||||||
|
}
|
||||||
|
// Serial.println("");
|
||||||
|
mfrc522.PICC_HaltA(); // Stop reading
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int k;
|
||||||
|
void ShowReaderDetails()
|
||||||
|
{
|
||||||
|
// Get the MFRC522 software version
|
||||||
|
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
||||||
|
// Serial.print(F("MFRC522 Software Version: 0x"));
|
||||||
|
// Serial.print(v, HEX);
|
||||||
|
if (v == 0x91)
|
||||||
|
// Serial.print(F(" = v1.0"));
|
||||||
|
k = 0;
|
||||||
|
else if (v == 0x92)
|
||||||
|
// Serial.print(F(" = v2.0"));
|
||||||
|
k = 1;
|
||||||
|
else
|
||||||
|
// Serial.print(F(" (unknown),probably a chinese clone?"));
|
||||||
// Serial.println("");
|
// Serial.println("");
|
||||||
mfrc522.PICC_HaltA(); // Stop reading
|
// When 0x00 or 0xFF is returned, communication probably failed
|
||||||
return 1;
|
if ((v == 0x00) || (v == 0xFF))
|
||||||
}
|
|
||||||
int k;
|
|
||||||
void ShowReaderDetails()
|
|
||||||
{
|
|
||||||
// Get the MFRC522 software version
|
|
||||||
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
|
|
||||||
// Serial.print(F("MFRC522 Software Version: 0x"));
|
|
||||||
// Serial.print(v, HEX);
|
|
||||||
if (v == 0x91)
|
|
||||||
// Serial.print(F(" = v1.0"));
|
|
||||||
k = 0;
|
|
||||||
else if (v == 0x92)
|
|
||||||
// Serial.print(F(" = v2.0"));
|
|
||||||
k = 1;
|
|
||||||
else
|
|
||||||
// Serial.print(F(" (unknown),probably a chinese clone?"));
|
|
||||||
// Serial.println("");
|
|
||||||
// When 0x00 or 0xFF is returned, communication probably failed
|
|
||||||
if ((v == 0x00) || (v == 0xFF))
|
|
||||||
{
|
{
|
||||||
// Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
// Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
|
||||||
// Serial.println(F("SYSTEM HALTED: Check connections."));
|
// Serial.println(F("SYSTEM HALTED: Check connections."));
|
||||||
|
@ -408,293 +445,303 @@ void loop()
|
||||||
while (true)
|
while (true)
|
||||||
; // do not go further
|
; // do not go further
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
|
///////////////////////////////////////// Cycle Leds (Program Mode) ///////////////////////////////////
|
||||||
void cycleLeds()
|
void cycleLeds()
|
||||||
{
|
{
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
|
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
|
||||||
void normalModeOn()
|
void normalModeOn()
|
||||||
{
|
{
|
||||||
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
|
digitalWrite(blueLed, LED_ON); // Blue LED ON and ready to read card
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
|
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
|
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
|
||||||
digitalWrite(relay, HIGH); // Make sure Door is Locked
|
digitalWrite(relay, HIGH); // Make sure Door is Locked
|
||||||
digitalWrite(ledView, HIGH);
|
digitalWrite(ledView, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
|
//////////////////////////////////////// Read an ID from EEPROM //////////////////////////////
|
||||||
void readID(uint8_t number)
|
void readID(uint8_t number)
|
||||||
{
|
{
|
||||||
uint8_t start = (number * 4) + 2; // Figure out starting position
|
uint8_t start = (number * 4) + 2; // Figure out starting position
|
||||||
for (uint8_t i = 0; i < 4; i++)
|
for (uint8_t i = 0; i < 4; i++)
|
||||||
{ // Loop 4 times to get the 4 Bytes
|
{ // Loop 4 times to get the 4 Bytes
|
||||||
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
|
storedCard[i] = EEPROM.read(start + i); // Assign values read from EEPROM to array
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Add ID to EEPROM ///////////////////////////////////
|
||||||
void writeID(byte a[])
|
void writeID(byte a[])
|
||||||
{
|
{
|
||||||
if (!findID(a))
|
if (!findID(a))
|
||||||
{ // Before we write to the EEPROM, check to see if we have seen this card before!
|
{ // Before we write to the EEPROM, check to see if we have seen this card before!
|
||||||
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
||||||
uint8_t start = (num * 4) + 6; // Figure out where the next slot starts
|
uint8_t start = (num * 4) + 6; // Figure out where the next slot starts
|
||||||
num++; // Increment the counter by one
|
num++; // Increment the counter by one
|
||||||
EEPROM.write(0, num); // Write the new count to the counter
|
EEPROM.write(0, num); // Write the new count to the counter
|
||||||
for (uint8_t j = 0; j < 4; j++)
|
for (uint8_t j = 0; j < 4; j++)
|
||||||
{ // Loop 4 times
|
|
||||||
EEPROM.write(start + j, a[j]); // Write the array values to EEPROM in the right position
|
|
||||||
}
|
|
||||||
successWrite();
|
|
||||||
// Serial.println(F("Succesfully added ID record to EEPROM"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
failedWrite();
|
|
||||||
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
|
|
||||||
void deleteID(byte a[])
|
|
||||||
{
|
|
||||||
if (!findID(a))
|
|
||||||
{ // Before we delete from the EEPROM, check to see if we have this card!
|
|
||||||
failedWrite(); // If not
|
|
||||||
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
|
||||||
uint8_t slot; // Figure out the slot number of the card
|
|
||||||
uint8_t start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
|
|
||||||
uint8_t looping; // The number of times the loop repeats
|
|
||||||
uint8_t j;
|
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
|
|
||||||
slot = findIDSLOT(a); // Figure out the slot number of the card to delete
|
|
||||||
start = (slot * 4) + 2;
|
|
||||||
looping = ((num - slot) * 4);
|
|
||||||
num--; // Decrement the counter by one
|
|
||||||
EEPROM.write(0, num); // Write the new count to the counter
|
|
||||||
for (j = 0; j < looping; j++)
|
|
||||||
{ // Loop the card shift times
|
|
||||||
EEPROM.write(start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
|
|
||||||
}
|
|
||||||
for (uint8_t k = 0; k < 4; k++)
|
|
||||||
{ // Shifting loop
|
|
||||||
EEPROM.write(start + j + k, 0);
|
|
||||||
}
|
|
||||||
successDelete();
|
|
||||||
// Serial.println(F("Succesfully removed ID record from EEPROM"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Check Bytes ///////////////////////////////////
|
|
||||||
bool checkTwo(byte a[], byte b[])
|
|
||||||
{
|
|
||||||
for (uint8_t k = 0; k < 4; k++)
|
|
||||||
{ // Loop 4 times
|
{ // Loop 4 times
|
||||||
if (a[k] != b[k])
|
EEPROM.write(start + j, a[j]); // Write the array values to EEPROM in the right position
|
||||||
{ // IF a != b then false, because: one fails, all fail
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
successWrite();
|
||||||
|
// Serial.println(F("Succesfully added ID record to EEPROM"));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
///////////////////////////////////////// Find Slot ///////////////////////////////////
|
|
||||||
uint8_t findIDSLOT(byte find[])
|
|
||||||
{
|
{
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
failedWrite();
|
||||||
for (uint8_t i = 1; i <= count; i++)
|
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
||||||
{ // Loop once for each EEPROM entry
|
}
|
||||||
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
}
|
||||||
if (checkTwo(find, storedCard))
|
|
||||||
{ // Check to see if the storedCard read from EEPROM
|
///////////////////////////////////////// Remove ID from EEPROM ///////////////////////////////////
|
||||||
// is the same as the find[] ID card passed
|
void deleteID(byte a[])
|
||||||
return i; // The slot number of the card
|
{
|
||||||
}
|
if (!findID(a))
|
||||||
|
{ // Before we delete from the EEPROM, check to see if we have this card!
|
||||||
|
failedWrite(); // If not
|
||||||
|
// Serial.println(F("Failed! There is something wrong with ID or bad EEPROM"));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uint8_t num = EEPROM.read(0); // Get the numer of used spaces, position 0 stores the number of ID cards
|
||||||
|
uint8_t slot; // Figure out the slot number of the card
|
||||||
|
uint8_t start; // = ( num * 4 ) + 6; // Figure out where the next slot starts
|
||||||
|
uint8_t looping; // The number of times the loop repeats
|
||||||
|
uint8_t j;
|
||||||
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that stores number of cards
|
||||||
|
slot = findIDSLOT(a); // Figure out the slot number of the card to delete
|
||||||
|
start = (slot * 4) + 2;
|
||||||
|
looping = ((num - slot) * 4);
|
||||||
|
num--; // Decrement the counter by one
|
||||||
|
EEPROM.write(0, num); // Write the new count to the counter
|
||||||
|
for (j = 0; j < looping; j++)
|
||||||
|
{ // Loop the card shift times
|
||||||
|
EEPROM.write(start + j, EEPROM.read(start + 4 + j)); // Shift the array values to 4 places earlier in the EEPROM
|
||||||
|
}
|
||||||
|
for (uint8_t k = 0; k < 4; k++)
|
||||||
|
{ // Shifting loop
|
||||||
|
EEPROM.write(start + j + k, 0);
|
||||||
|
}
|
||||||
|
successDelete();
|
||||||
|
// Serial.println(F("Succesfully removed ID record from EEPROM"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////// Check Bytes ///////////////////////////////////
|
||||||
|
bool checkTwo(byte a[], byte b[])
|
||||||
|
{
|
||||||
|
for (uint8_t k = 0; k < 4; k++)
|
||||||
|
{ // Loop 4 times
|
||||||
|
if (a[k] != b[k])
|
||||||
|
{ // IF a != b then false, because: one fails, all fail
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Find Slot ///////////////////////////////////
|
||||||
bool findID(byte find[])
|
uint8_t findIDSLOT(byte find[])
|
||||||
{
|
{
|
||||||
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
||||||
for (uint8_t i = 1; i < count; i++)
|
for (uint8_t i = 1; i <= count; i++)
|
||||||
{ // Loop once for each EEPROM entry
|
{ // Loop once for each EEPROM entry
|
||||||
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
||||||
if (checkTwo(find, storedCard))
|
if (checkTwo(find, storedCard))
|
||||||
{ // Check to see if the storedCard read from EEPROM
|
{ // Check to see if the storedCard read from EEPROM
|
||||||
return true;
|
// is the same as the find[] ID card passed
|
||||||
}
|
return i; // The slot number of the card
|
||||||
else
|
|
||||||
{ // If not, return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
|
///////////////////////////////////////// Find ID From EEPROM ///////////////////////////////////
|
||||||
// Flashes the green LED 3 times to indicate a successful write to EEPROM
|
bool findID(byte find[])
|
||||||
void successWrite()
|
{
|
||||||
{
|
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
for (uint8_t i = 1; i < count; i++)
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
{ // Loop once for each EEPROM entry
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
|
readID(i); // Read an ID from EEPROM, it is stored in storedCard[4]
|
||||||
delay(200);
|
if (checkTwo(find, storedCard))
|
||||||
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
{ // Check to see if the storedCard read from EEPROM
|
||||||
delay(200);
|
return true;
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
|
|
||||||
// Flashes the red LED 3 times to indicate a failed write to EEPROM
|
|
||||||
void failedWrite()
|
|
||||||
{
|
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
|
|
||||||
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
|
|
||||||
void successDelete()
|
|
||||||
{
|
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
|
||||||
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
|
||||||
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
|
||||||
delay(200);
|
|
||||||
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
|
|
||||||
// Check to see if the ID passed is the master programing card
|
|
||||||
bool isMaster(byte test[])
|
|
||||||
{
|
|
||||||
return checkTwo(test, masterCard);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool monitorWipeButton(uint32_t interval)
|
|
||||||
{
|
|
||||||
uint32_t now = (uint32_t)millis();
|
|
||||||
while ((uint32_t)millis() - now < interval)
|
|
||||||
{
|
|
||||||
// check on every half a second
|
|
||||||
if (((uint32_t)millis() % 500) == 0)
|
|
||||||
{
|
|
||||||
if (digitalRead(wipeB) != LOW)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// ADD Code
|
|
||||||
|
|
||||||
int stt;
|
|
||||||
int stt_old;
|
|
||||||
String input;
|
|
||||||
void ReadButton()
|
|
||||||
{
|
|
||||||
stt = digitalRead(button);
|
|
||||||
Serial.println(stt);
|
|
||||||
if (stt == 0 && stt_old == 1)
|
|
||||||
{
|
|
||||||
relay_stt = true;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{ // If not, return false
|
||||||
relay_stt = false;
|
|
||||||
}
|
}
|
||||||
stt_old = stt;
|
|
||||||
}
|
}
|
||||||
// SD MASTER: 2B429622
|
return false;
|
||||||
// SD TEST: C9B09B6E
|
}
|
||||||
|
|
||||||
void SerialRead()
|
///////////////////////////////////////// Write Success to EEPROM ///////////////////////////////////
|
||||||
|
// Flashes the green LED 3 times to indicate a successful write to EEPROM
|
||||||
|
void successWrite()
|
||||||
|
{
|
||||||
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(greenLed, LED_ON); // Make sure green LED is on
|
||||||
|
delay(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////// Write Failed to EEPROM ///////////////////////////////////
|
||||||
|
// Flashes the red LED 3 times to indicate a failed write to EEPROM
|
||||||
|
void failedWrite()
|
||||||
|
{
|
||||||
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(redLed, LED_ON); // Make sure red LED is on
|
||||||
|
delay(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////// Success Remove UID From EEPROM ///////////////////////////////////
|
||||||
|
// Flashes the blue LED 3 times to indicate a success delete to EEPROM
|
||||||
|
void successDelete()
|
||||||
|
{
|
||||||
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
|
digitalWrite(redLed, LED_OFF); // Make sure red LED is off
|
||||||
|
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(blueLed, LED_OFF); // Make sure blue LED is off
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(blueLed, LED_ON); // Make sure blue LED is on
|
||||||
|
delay(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////// Check readCard IF is masterCard ///////////////////////////////////
|
||||||
|
// Check to see if the ID passed is the master programing card
|
||||||
|
bool isMaster(byte test[])
|
||||||
|
{
|
||||||
|
return checkTwo(test, masterCard);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool monitorWipeButton(uint32_t interval)
|
||||||
|
{
|
||||||
|
uint32_t now = (uint32_t)millis();
|
||||||
|
while ((uint32_t)millis() - now < interval)
|
||||||
{
|
{
|
||||||
while (Serial.available() > 0)
|
// check on every half a second
|
||||||
|
if (((uint32_t)millis() % 500) == 0)
|
||||||
{
|
{
|
||||||
input = Serial.readStringUntil('\r');
|
if (digitalRead(wipeB) != LOW)
|
||||||
Serial.println(input);
|
return false;
|
||||||
if (input == "1")
|
|
||||||
{
|
|
||||||
Serial.println("open");
|
|
||||||
if (relay_stt == false)
|
|
||||||
{
|
|
||||||
relay_stt = true;
|
|
||||||
digitalWrite(13, HIGH);
|
|
||||||
}
|
|
||||||
ClearSerialdata();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
digitalWrite(13, LOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClearSerialdata();
|
|
||||||
}
|
|
||||||
void ClearSerialdata()
|
|
||||||
{
|
|
||||||
while (Serial.available() > 0)
|
|
||||||
{
|
|
||||||
char c = Serial.read();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void CheckButton()
|
return true;
|
||||||
|
}
|
||||||
|
// ADD Code
|
||||||
|
|
||||||
|
int stt;
|
||||||
|
int stt_old;
|
||||||
|
String input;
|
||||||
|
void ReadButton()
|
||||||
|
{
|
||||||
|
stt = digitalRead(button);
|
||||||
|
Serial.println(stt);
|
||||||
|
if (stt == 0 && stt_old == 1)
|
||||||
{
|
{
|
||||||
if (relay_stt == true)
|
relay_stt = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
relay_stt = false;
|
||||||
|
}
|
||||||
|
stt_old = stt;
|
||||||
|
}
|
||||||
|
// SD MASTER: 2B429622
|
||||||
|
// SD TEST: C9B09B6E
|
||||||
|
|
||||||
|
void SerialRead()
|
||||||
|
{
|
||||||
|
while (Serial.available() > 0)
|
||||||
|
{
|
||||||
|
input = Serial.readStringUntil('\r');
|
||||||
|
Serial.println(input);
|
||||||
|
if (input == "2") {
|
||||||
|
if (Light_stt == false) {
|
||||||
|
TurnOn_Light();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (input == "3") {
|
||||||
|
if (Light_stt == true) {
|
||||||
|
TurnOff_Light();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (input == "1")
|
||||||
{
|
{
|
||||||
granted(4000); // Open the door lock for 300 ms
|
Serial.println("open");
|
||||||
relay_stt = false;
|
if (relay_stt == false)
|
||||||
|
{
|
||||||
|
relay_stt = true;
|
||||||
|
digitalWrite(13, HIGH);
|
||||||
|
}
|
||||||
ClearSerialdata();
|
ClearSerialdata();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
digitalWrite(relay, HIGH); // Unlock door!
|
digitalWrite(13, LOW);
|
||||||
digitalWrite(ledView, HIGH);
|
|
||||||
relay_stt = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ClearSerialdata();
|
||||||
|
}
|
||||||
|
void ClearSerialdata()
|
||||||
|
{
|
||||||
|
while (Serial.available() > 0)
|
||||||
|
{
|
||||||
|
char c = Serial.read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void CheckButton()
|
||||||
|
{
|
||||||
|
if (relay_stt == true)
|
||||||
|
{
|
||||||
|
granted(4000); // Open the door lock for 300 ms
|
||||||
|
relay_stt = false;
|
||||||
|
ClearSerialdata();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
digitalWrite(relay, HIGH); // Unlock door!
|
||||||
|
digitalWrite(ledView, HIGH);
|
||||||
|
relay_stt = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user