From b1620b28c89eef5a1bf5c84570f10f0e0cf41616 Mon Sep 17 00:00:00 2001 From: haimd Date: Sat, 24 Oct 2020 13:06:29 +0700 Subject: [PATCH] add biface_v3 --- Lock/Lock.ino | 547 +++++++++++++++++++++++++++++--------------------- 1 file changed, 319 insertions(+), 228 deletions(-) diff --git a/Lock/Lock.ino b/Lock/Lock.ino index 8bc0c2b..8e3dccd 100644 --- a/Lock/Lock.ino +++ b/Lock/Lock.ino @@ -56,7 +56,7 @@ //#endif // //#define Light 7 -//#define ledView 13 +#define ledView 13 // //#define redLed 3 // Set Led Pins //#define greenLed 6 @@ -65,8 +65,13 @@ //#define relay A2 //8 // Set Relay Pin //#define wipeB 2 // Button pin for WipeMode //#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 #define LED_ON LOW @@ -80,16 +85,23 @@ #define greenLed 2 #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 button A1 // button exit -#define Light 7 // Led tro sang camera + +#define Light A1 // Led tro sang camera bool Light_stt = false; bool relay_stt = false; +bool relay_stt_2 = false; + bool sttRun = false; bool programMode = false; // initialize programming mode to false @@ -119,130 +131,134 @@ void setup() pinMode(blueLed, OUTPUT); pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor 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 digitalWrite(relay, HIGH); // Make sure door is locked - delay(2000); - digitalWrite(relay, LOW); - delay(2000); - digitalWrite(relay, HIGH); // Make sure door is locked - delay(2000); + digitalWrite(relay_2, HIGH); // Make sure door is locked +// delay(2000); +// digitalWrite(relay, LOW); +// delay(2000); +// digitalWrite(relay_2, HIGH); // Make sure door is locked +// delay(2000); digitalWrite(redLed, 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(ledView, LOW); + digitalWrite(Light, LOW); //Protocol Configuration Serial.begin(9600); // Initialize serial communications with PC - - SPI.begin(); // MFRC522 Hardware uses SPI protocol - mfrc522.PCD_Init(); // Initialize MFRC522 Hardware - if (Debug == true) - { - //If you set Antenna Gain to Max it will increase reading distance - mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); - Serial.println(F("Access Control Example v0.1")); // For debugging purposes - } - - ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details - - //Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM - if (digitalRead(wipeB) == LOW) - { // 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 - if (Debug == 1) - { - Serial.println(F("Wipe Button Pressed")); - Serial.println(F("You have 10 seconds to Cancel")); - Serial.println(F("This will be remove all records and cannot be undone")); - } - bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation - if (buttonState == true && digitalRead(wipeB) == LOW) - { // If button still be pressed, wipe EEPROM - // Serial.println(F("Starting Wiping EEPROM")); - for (uint16_t x = 0; x < EEPROM.length(); x = x + 1) - { //Loop end of EEPROM address - if (EEPROM.read(x) == 0) - { //If EEPROM address 0 - // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM - } - else - { - EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS - } - } - // Serial.println(F("EEPROM Successfully Wiped")); - digitalWrite(redLed, LED_OFF); // visualize a successful wipe - delay(200); - digitalWrite(redLed, LED_ON); - delay(200); - digitalWrite(redLed, LED_OFF); - delay(200); - digitalWrite(redLed, LED_ON); - delay(200); - digitalWrite(redLed, LED_OFF); - } - else - { - // Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds - digitalWrite(redLed, LED_OFF); - } - } - // Check if master card defined, if not let user choose a 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 - // EEPROM address 1 should hold magical number which is '143' - if (EEPROM.read(1) != 143) - { - if (Debug == 1) - { - Serial.println(F("No Master Card Defined")); - Serial.println(F("Scan A PICC to Define as Master Card")); - } - - do - { - 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 - delay(200); - digitalWrite(blueLed, LED_OFF); - delay(200); - } while (!successRead); // Program will not go further while you not get a successful read - for (uint8_t j = 0; j < 4; j++) - { // Loop 4 times - 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. - if (Debug == 1) - { - Serial.println(F("Master Card Defined")); - } - } - if (Debug == 1) - { - Serial.println(F("-------------------")); - Serial.println(F("Master Card's UID")); - } - - for (uint8_t i = 0; i < 4; i++) - { // Read Master Card's UID from EEPROM - masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard - if (Debug == 1) - { - Serial.print(masterCard[i], HEX); - } - } - if (Debug == 1) - { - Serial.println(""); - Serial.println(F("-------------------")); - Serial.println(F("Everything is ready")); - Serial.println(F("Waiting PICCs to be scanned")); - } - - cycleLeds(); // Everything ready lets give user some feedback by cycling leds - // } - // wdt_enable(WDTO_500MS); +// +// SPI.begin(); // MFRC522 Hardware uses SPI protocol +// mfrc522.PCD_Init(); // Initialize MFRC522 Hardware +// if (Debug == true) +// { +// //If you set Antenna Gain to Max it will increase reading distance +// mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); +// Serial.println(F("Access Control Example v0.1")); // For debugging purposes +// } +// +// ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details +// +// //Wipe Code - If the Button (wipeB) Pressed while setup run (powered on) it wipes EEPROM +// if (digitalRead(wipeB) == LOW) +// { // 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 +// if (Debug == 1) +// { +// Serial.println(F("Wipe Button Pressed")); +// Serial.println(F("You have 10 seconds to Cancel")); +// Serial.println(F("This will be remove all records and cannot be undone")); +// } +// bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation +// if (buttonState == true && digitalRead(wipeB) == LOW) +// { // If button still be pressed, wipe EEPROM +// // Serial.println(F("Starting Wiping EEPROM")); +// for (uint16_t x = 0; x < EEPROM.length(); x = x + 1) +// { //Loop end of EEPROM address +// if (EEPROM.read(x) == 0) +// { //If EEPROM address 0 +// // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM +// } +// else +// { +// EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS +// } +// } +// // Serial.println(F("EEPROM Successfully Wiped")); +// digitalWrite(redLed, LED_OFF); // visualize a successful wipe +// delay(200); +// digitalWrite(redLed, LED_ON); +// delay(200); +// digitalWrite(redLed, LED_OFF); +// delay(200); +// digitalWrite(redLed, LED_ON); +// delay(200); +// digitalWrite(redLed, LED_OFF); +// } +// else +// { +// // Serial.println(F("Wiping Cancelled")); // Show some feedback that the wipe button did not pressed for 15 seconds +// digitalWrite(redLed, LED_OFF); +// } +// } +// // Check if master card defined, if not let user choose a 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 +// // EEPROM address 1 should hold magical number which is '143' +// if (EEPROM.read(1) != 143) +// { +// if (Debug == 1) +// { +// Serial.println(F("No Master Card Defined")); +// Serial.println(F("Scan A PICC to Define as Master Card")); +// } +// +// do +// { +// 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 +// delay(200); +// digitalWrite(blueLed, LED_OFF); +// delay(200); +// } while (!successRead); // Program will not go further while you not get a successful read +// for (uint8_t j = 0; j < 4; j++) +// { // Loop 4 times +// 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. +// if (Debug == 1) +// { +// Serial.println(F("Master Card Defined")); +// } +// } +// if (Debug == 1) +// { +// Serial.println(F("-------------------")); +// Serial.println(F("Master Card's UID")); +// } +// +// for (uint8_t i = 0; i < 4; i++) +// { // Read Master Card's UID from EEPROM +// masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard +// if (Debug == 1) +// { +// Serial.print(masterCard[i], HEX); +// } +// } +// if (Debug == 1) +// { +// Serial.println(""); +// Serial.println(F("-------------------")); +// Serial.println(F("Everything is ready")); +// Serial.println(F("Waiting PICCs to be scanned")); +// } +// +// cycleLeds(); // Everything ready lets give user some feedback by cycling leds +// // } +// // wdt_enable(WDTO_500MS); } //void SerialRead(); @@ -250,123 +266,131 @@ void setup() ///////////////////////////////////////// Main Loop /////////////////////////////////// void loop() { - 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(); - } - } - } - } +// 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(); //} ///////////////////////////////////////// Access Granted /////////////////////////////////// // Turn on Light camera void TurnOn_Light() { - digitalWrite(Light, LOW); + digitalWrite(Light, HIGH); Light_stt = true; } void TurnOff_Light() { - digitalWrite(Light, HIGH); + digitalWrite(Light, LOW); Light_stt = false; } @@ -383,6 +407,21 @@ void granted(uint16_t setDelay) 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 /////////////////////////////////// void denied() { @@ -673,20 +712,45 @@ bool monitorWipeButton(uint32_t interval) int stt; int stt_old; String input; + + +int stt_2; +int stt_old_2; +String input_2; + + void ReadButton() { 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) { relay_stt = true; + Serial.println("Button 1 press"); } else { 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_2 = stt_2; } + + + // SD MASTER: 2B429622 // SD TEST: C9B09B6E @@ -696,15 +760,20 @@ void SerialRead() { input = Serial.readStringUntil('\r'); Serial.println(input); - if (input == "2") { + input.trim(); + if (input == "3") { + Serial.println(input); if (Light_stt == false) { TurnOn_Light(); } + ClearSerialdata(); - } else if (input == "3") { + } else if (input == "4") { if (Light_stt == true) { TurnOff_Light(); + } + ClearSerialdata(); } else if (input == "1") { @@ -715,6 +784,15 @@ void SerialRead() digitalWrite(13, HIGH); } ClearSerialdata(); + }else if (input == "2") + { + Serial.println("open"); + if (relay_stt_2 == false) + { + relay_stt_2 = true; + digitalWrite(13, HIGH); + } + ClearSerialdata(); } else { @@ -744,4 +822,17 @@ void CheckButton() digitalWrite(ledView, HIGH); 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; + } }