add biface_v3

This commit is contained in:
HaiMD 2020-10-24 13:06:29 +07:00
parent 732bd382ce
commit b1620b28c8

View File

@ -56,7 +56,7 @@
//#endif //#endif
// //
//#define Light 7 //#define Light 7
//#define ledView 13 #define ledView 13
// //
//#define redLed 3 // Set Led Pins //#define redLed 3 // Set Led Pins
//#define greenLed 6 //#define greenLed 6
@ -65,8 +65,13 @@
//#define relay A2 //8 // Set Relay Pin //#define relay A2 //8 // Set Relay Pin
//#define wipeB 2 // Button pin for WipeMode //#define wipeB 2 // Button pin for WipeMode
//#define button 7 // button exit //#define button 7 // button exit
/ -------------------------------------------- // --------------------------------------------
//Ver 2 //Ver 3
// LED 12V - A1
//Relay 1 - D5
//Button 1 - D8
// Relay 2 - D6
// Button 2 - D7
#ifdef COMMON_ANODE #ifdef COMMON_ANODE
#define LED_ON LOW #define LED_ON LOW
@ -80,16 +85,23 @@
#define greenLed 2 #define greenLed 2
#define blueLed 3 #define blueLed 3
#define relay 5 //6 // Set Relay Pin #define relay 5 //6 // Set Relay 1 Pin
#define button 7 // button exit
#define relay_2 6 //6 // Set Relay 1 Pin
#define button_2 8 // button exit
#define wipeB A0 // Button pin for WipeMode #define wipeB A0 // Button pin for WipeMode
#define button A1 // button exit
#define Light 7 // Led tro sang camera #define Light A1 // Led tro sang camera
bool Light_stt = false; bool Light_stt = false;
bool relay_stt = false; bool relay_stt = false;
bool relay_stt_2 = false;
bool sttRun = false; bool sttRun = false;
bool programMode = false; // initialize programming mode to false bool programMode = false; // initialize programming mode to false
@ -119,130 +131,134 @@ void setup()
pinMode(blueLed, OUTPUT); pinMode(blueLed, OUTPUT);
pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
pinMode(relay, OUTPUT); pinMode(relay, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(button,INPUT);
pinMode(button_2,INPUT);
//Be careful how relay circuit behave on while resetting or power-cycling your Arduino //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
digitalWrite(relay, HIGH); // Make sure door is locked digitalWrite(relay, HIGH); // Make sure door is locked
delay(2000); digitalWrite(relay_2, HIGH); // Make sure door is locked
digitalWrite(relay, LOW); // delay(2000);
delay(2000); // digitalWrite(relay, LOW);
digitalWrite(relay, HIGH); // Make sure door is locked // delay(2000);
delay(2000); // digitalWrite(relay_2, HIGH); // Make sure door is locked
// delay(2000);
digitalWrite(redLed, LED_OFF); // Make sure led is off digitalWrite(redLed, LED_OFF); // Make sure led is off
digitalWrite(greenLed, LED_OFF); // Make sure led is off digitalWrite(greenLed, LED_OFF); // Make sure led is off
digitalWrite(blueLed, LED_OFF); // Make sure led is off digitalWrite(blueLed, LED_OFF); // Make sure led is off
digitalWrite(ledView, LOW); digitalWrite(Light, LOW);
//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)
{ // {
//If you set Antenna Gain to Max it will increase reading distance // //If you set Antenna Gain to Max it will increase reading distance
mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); // mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
Serial.println(F("Access Control Example v0.1")); // For debugging purposes // Serial.println(F("Access Control Example v0.1")); // For debugging purposes
} // }
//
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details // ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
//
//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)
{ // {
Serial.println(F("Wipe Button Pressed")); // Serial.println(F("Wipe Button Pressed"));
Serial.println(F("You have 10 seconds to Cancel")); // Serial.println(F("You have 10 seconds to Cancel"));
Serial.println(F("This will be remove all records and cannot be undone")); // Serial.println(F("This will be remove all records and cannot be undone"));
} // }
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
// Serial.println(F("Starting Wiping EEPROM")); // // Serial.println(F("Starting Wiping EEPROM"));
for (uint16_t x = 0; x < EEPROM.length(); x = x + 1) // for (uint16_t x = 0; x < EEPROM.length(); x = x + 1)
{ //Loop end of EEPROM address // { //Loop end of EEPROM address
if (EEPROM.read(x) == 0) // if (EEPROM.read(x) == 0)
{ //If EEPROM address 0 // { //If EEPROM address 0
// do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM // // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
} // }
else // else
{ // {
EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS // EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
} // }
} // }
// Serial.println(F("EEPROM Successfully Wiped")); // // Serial.println(F("EEPROM Successfully Wiped"));
digitalWrite(redLed, LED_OFF); // visualize a successful wipe // digitalWrite(redLed, LED_OFF); // visualize a successful wipe
delay(200); // delay(200);
digitalWrite(redLed, LED_ON); // digitalWrite(redLed, LED_ON);
delay(200); // delay(200);
digitalWrite(redLed, LED_OFF); // digitalWrite(redLed, LED_OFF);
delay(200); // delay(200);
digitalWrite(redLed, LED_ON); // digitalWrite(redLed, LED_ON);
delay(200); // delay(200);
digitalWrite(redLed, LED_OFF); // digitalWrite(redLed, LED_OFF);
} // }
else // else
{ // {
// Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds // // Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds
digitalWrite(redLed, LED_OFF); // digitalWrite(redLed, LED_OFF);
} // }
} // }
// Check if master card defined, if not let user choose a master card // // Check if master card defined, if not let user choose a master card
// This also useful to just redefine the Master Card // // This also useful to just redefine the Master Card
// You can keep other EEPROM records just write other than 143 to EEPROM address 1 // // You can keep other EEPROM records just write other than 143 to EEPROM address 1
// EEPROM address 1 should hold magical number which is '143' // // EEPROM address 1 should hold magical number which is '143'
if (EEPROM.read(1) != 143) // if (EEPROM.read(1) != 143)
{ // {
if (Debug == 1) // if (Debug == 1)
{ // {
Serial.println(F("No Master Card Defined")); // Serial.println(F("No Master Card Defined"));
Serial.println(F("Scan A PICC to Define as Master Card")); // Serial.println(F("Scan A PICC to Define as Master Card"));
} // }
//
do // do
{ // {
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
digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined // digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
delay(200); // delay(200);
digitalWrite(blueLed, LED_OFF); // digitalWrite(blueLed, LED_OFF);
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.
if (Debug == 1) // if (Debug == 1)
{ // {
Serial.println(F("Master Card Defined")); // Serial.println(F("Master Card Defined"));
} // }
} // }
if (Debug == 1) // if (Debug == 1)
{ // {
Serial.println(F("-------------------")); // Serial.println(F("-------------------"));
Serial.println(F("Master Card's UID")); // Serial.println(F("Master Card's UID"));
} // }
//
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)
{ // {
Serial.print(masterCard[i], HEX); // Serial.print(masterCard[i], HEX);
} // }
} // }
if (Debug == 1) // if (Debug == 1)
{ // {
Serial.println(""); // Serial.println("");
Serial.println(F("-------------------")); // Serial.println(F("-------------------"));
Serial.println(F("Everything is ready")); // Serial.println(F("Everything is ready"));
Serial.println(F("Waiting PICCs to be scanned")); // Serial.println(F("Waiting PICCs to be scanned"));
} // }
//
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 SerialRead();
@ -250,123 +266,131 @@ void setup()
///////////////////////////////////////// Main Loop /////////////////////////////////// ///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop() void loop()
{ {
do
{
ReadButton(); ReadButton();
SerialRead(); SerialRead();
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
if (digitalRead(wipeB) == LOW)
{ // Check if button is pressed
// Visualize normal operation is iterrupted by pressing wipe button Red is like more Warning to user
digitalWrite(redLed, LED_ON); // Make sure led is off
digitalWrite(greenLed, LED_OFF); // Make sure led is off
digitalWrite(blueLed, LED_OFF); // Make sure led is off
// Give some feedback
if (Debug == 1)
{
Serial.println(F("Wipe Button Pressed"));
Serial.println(F("Master Card will be Erased! in 10 seconds"));
}
bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
if (buttonState == true && digitalRead(wipeB) == LOW)
{ // If button still be pressed, wipe EEPROM
EEPROM.write(1, 0); // Reset Magic Number.
if (Debug == 1)
{
Serial.println(F("Master Card Erased from device"));
Serial.println(F("Please reset to re-program Master Card"));
}
while (1)
;
}
// Serial.println(F("Master Card Erase Cancelled"));
}
if (programMode)
{
cycleLeds(); // Program Mode cycles through Red Green Blue waiting to read a new card
}
else
{
normalModeOn(); // Normal mode, blue Power LED is on, all others are off
}
CheckButton(); CheckButton();
} while (!successRead); //the program will not go further while you are not getting a successful read
if (programMode)
{
if (isMaster(readCard))
{ //When in program mode check First If master card scanned again to exit program mode
// Serial.println(F("Master Card Scanned"));
// Serial.println(F("Exiting Program Mode"));
// Serial.println(F("-----------------------------"));
programMode = false;
return;
}
else
{
if (findID(readCard))
{ // If scanned card is known delete it
// Serial.println(F("I know this PICC, removing..."));
deleteID(readCard);
// Serial.println("-----------------------------");
// Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
}
else
{ // If scanned card is not known add it
// Serial.println(F("I do not know this PICC, adding..."));
writeID(readCard);
// Serial.println(F("-----------------------------"));
// Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
}
}
}
else
{
if (isMaster(readCard))
{ // If scanned card's ID matches Master Card's ID - enter program mode
programMode = true;
// Serial.println(F("Hello Master - Entered Program Mode"));
uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
// Serial.print(F("I have ")); // stores the number of ID's in EEPROM
// Serial.print(count);
// Serial.print(F(" record(s) on EEPROM"));
// Serial.println("");
// Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
// Serial.println(F("Scan Master Card again to Exit Program Mode"));
// Serial.println(F("-----------------------------"));
}
else
{
if (findID(readCard))
{ // If not, see if the card is in the EEPROM
// Serial.println(F("Welcome, You shall pass"));
relay_stt = true;
granted(4000); // Open the door lock for 300 ms
ClearSerialdata();
}
else
{ // If not, show that the ID was not valid
// Serial.println(F("You shall not pass"));
denied();
}
}
}
} }
// do
// {
// ReadButton();
// SerialRead();
//
// 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
// if (digitalRead(wipeB) == LOW)
// { // Check if button is pressed
// // Visualize normal operation is iterrupted by pressing wipe button Red is like more Warning to user
// digitalWrite(redLed, LED_ON); // Make sure led is off
// digitalWrite(greenLed, LED_OFF); // Make sure led is off
// digitalWrite(blueLed, LED_OFF); // Make sure led is off
// // Give some feedback
// if (Debug == 1)
// {
// Serial.println(F("Wipe Button Pressed"));
// Serial.println(F("Master Card will be Erased! in 10 seconds"));
// }
//
// bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
// if (buttonState == true && digitalRead(wipeB) == LOW)
// { // If button still be pressed, wipe EEPROM
// EEPROM.write(1, 0); // Reset Magic Number.
// if (Debug == 1)
// {
// Serial.println(F("Master Card Erased from device"));
// Serial.println(F("Please reset to re-program Master Card"));
// }
// while (1)
// ;
// }
// // Serial.println(F("Master Card Erase Cancelled"));
// }
// if (programMode)
// {
// cycleLeds(); // Program Mode cycles through Red Green Blue waiting to read a new card
// }
// else
// {
// normalModeOn(); // Normal mode, blue Power LED is on, all others are off
// }
// CheckButton();
// } while (!successRead); //the program will not go further while you are not getting a successful read
//
// if (programMode)
// {
// if (isMaster(readCard))
// { //When in program mode check First If master card scanned again to exit program mode
// // Serial.println(F("Master Card Scanned"));
// // Serial.println(F("Exiting Program Mode"));
// // Serial.println(F("-----------------------------"));
// programMode = false;
// return;
// }
// else
// {
// if (findID(readCard))
// { // If scanned card is known delete it
// // Serial.println(F("I know this PICC, removing..."));
// deleteID(readCard);
// // Serial.println("-----------------------------");
// // Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
// }
// else
// { // If scanned card is not known add it
// // Serial.println(F("I do not know this PICC, adding..."));
// writeID(readCard);
// // Serial.println(F("-----------------------------"));
// // Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
// }
// }
// }
// else
// {
// if (isMaster(readCard))
// { // If scanned card's ID matches Master Card's ID - enter program mode
// programMode = true;
// // Serial.println(F("Hello Master - Entered Program Mode"));
// uint8_t count = EEPROM.read(0); // Read the first Byte of EEPROM that
// // Serial.print(F("I have ")); // stores the number of ID's in EEPROM
// // Serial.print(count);
// // Serial.print(F(" record(s) on EEPROM"));
// // Serial.println("");
// // Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
// // Serial.println(F("Scan Master Card again to Exit Program Mode"));
// // Serial.println(F("-----------------------------"));
// }
// else
// {
// if (findID(readCard))
// { // If not, see if the card is in the EEPROM
// // Serial.println(F("Welcome, You shall pass"));
// relay_stt = true;
// granted(4000); // Open the door lock for 300 ms
// ClearSerialdata();
// }
// else
// { // If not, show that the ID was not valid
// // Serial.println(F("You shall not pass"));
// denied();
// }
// }
// }
//
//}
//----------------------------------------
// wdt_reset(); // wdt_reset();
//} //}
///////////////////////////////////////// Access Granted /////////////////////////////////// ///////////////////////////////////////// Access Granted ///////////////////////////////////
// Turn on Light camera // Turn on Light camera
void TurnOn_Light() { void TurnOn_Light() {
digitalWrite(Light, LOW); digitalWrite(Light, HIGH);
Light_stt = true; Light_stt = true;
} }
void TurnOff_Light() { void TurnOff_Light() {
digitalWrite(Light, HIGH); digitalWrite(Light, LOW);
Light_stt = false; Light_stt = false;
} }
@ -383,6 +407,21 @@ void granted(uint16_t setDelay)
delay(200); // Hold green LED on for a second delay(200); // Hold green LED on for a second
} }
void granted_2(uint16_t setDelay)
{
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_2, LOW); // Unlock door!
digitalWrite(ledView, LOW);
// digitalWrite(Light, HIGH);
delay(setDelay); // Hold door lock open for given seconds
digitalWrite(relay_2, HIGH); // Relock door
digitalWrite(ledView, HIGH);
// digitalWrite(Light, LOW);
delay(200); // Hold green LED on for a second
}
///////////////////////////////////////// Access Denied /////////////////////////////////// ///////////////////////////////////////// Access Denied ///////////////////////////////////
void denied() void denied()
{ {
@ -673,20 +712,45 @@ bool monitorWipeButton(uint32_t interval)
int stt; int stt;
int stt_old; int stt_old;
String input; String input;
int stt_2;
int stt_old_2;
String input_2;
void ReadButton() void ReadButton()
{ {
stt = digitalRead(button); stt = digitalRead(button);
Serial.println(stt); stt_2 = digitalRead(button_2);
// Serial.print(stt);
// Serial.print("\t");
// Serial.println(stt_2);
if (stt == 0 && stt_old == 1) if (stt == 0 && stt_old == 1)
{ {
relay_stt = true; relay_stt = true;
Serial.println("Button 1 press");
} }
else else
{ {
relay_stt = false; relay_stt = false;
} }
if (stt_2 == 0 && stt_old_2 == 1)
{
Serial.println("Button 2 press");
relay_stt_2 = true;
}
else
{
relay_stt_2 = false;
}
stt_old = stt; stt_old = stt;
stt_old_2 = stt_2;
} }
// SD MASTER: 2B429622 // SD MASTER: 2B429622
// SD TEST: C9B09B6E // SD TEST: C9B09B6E
@ -696,15 +760,20 @@ void SerialRead()
{ {
input = Serial.readStringUntil('\r'); input = Serial.readStringUntil('\r');
Serial.println(input); Serial.println(input);
if (input == "2") { input.trim();
if (input == "3") {
Serial.println(input);
if (Light_stt == false) { if (Light_stt == false) {
TurnOn_Light(); TurnOn_Light();
} }
ClearSerialdata();
} else if (input == "3") { } else if (input == "4") {
if (Light_stt == true) { if (Light_stt == true) {
TurnOff_Light(); TurnOff_Light();
} }
ClearSerialdata();
} }
else if (input == "1") else if (input == "1")
{ {
@ -715,6 +784,15 @@ void SerialRead()
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
} }
ClearSerialdata(); ClearSerialdata();
}else if (input == "2")
{
Serial.println("open");
if (relay_stt_2 == false)
{
relay_stt_2 = true;
digitalWrite(13, HIGH);
}
ClearSerialdata();
} }
else else
{ {
@ -744,4 +822,17 @@ void CheckButton()
digitalWrite(ledView, HIGH); digitalWrite(ledView, HIGH);
relay_stt = false; relay_stt = false;
} }
//-------------------------
if (relay_stt_2 == true)
{
granted_2(4000); // Open the door lock for 300 ms
relay_stt_2 = false;
ClearSerialdata();
}
else
{
digitalWrite(relay_2, HIGH); // Unlock door!
digitalWrite(ledView, HIGH);
relay_stt_2 = false;
}
} }