/* +---------+ +----------------------------------->READ TAGS+^------------------------------------------+ | +--------------------+ | | | | | | | | | | +----v-----+ +-----v----+ | | |MASTER TAG| |OTHER TAGS| | | +--+-------+ ++-------------+ | | | | | | | | | | | | +-----v---+ +----v----+ +----v------+ | | +------------+READ TAGS+---+ |KNOWN TAG| |UNKNOWN TAG| | | | +-+-------+ | +-----------+ +------------------+ | | | | | | | | | +----v-----+ +----v----+ +--v--------+ +-v----------+ +------v----+ | | |MASTER TAG| |KNOWN TAG| |UNKNOWN TAG| |GRANT ACCESS| |DENY ACCESS| | | +----------+ +---+-----+ +-----+-----+ +-----+------+ +-----+-----+ | | | | | | | | +----+ +----v------+ +--v---+ | +---------------> +-------+EXIT| |DELETE FROM| |ADD TO| | | +----+ | EEPROM | |EEPROM| | | +-----------+ +------+ +-------------------------------+ Typical pin layout used: ----------------------------------------------------------------------------------------- MFRC522 Arduino Arduino Arduino Arduino Arduino Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro Signal Pin Pin Pin Pin Pin Pin ----------------------------------------------------------------------------------------- RST/Reset RST 9 5 D9 RESET/ICSP-5 RST SPI SS SDA(SS) 10 53 D10 10 10 SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 */ #include // We are going to read and write PICC's UIDs from/to EEPROM #include // RC522 Module uses SPI protocol #include // Library for Mifare RC522 Devices #include #define ledView 13 //Ver 3 // LED 12V - A1 //Relay 1 - D5 //Button 1 - D8 // Relay 2 - D6 // Button 2 - D7 #ifdef COMMON_ANODE #define LED_ON LOW #define LED_OFF HIGH #else #define LED_ON HIGH #define LED_OFF LOW #endif #define redLed 4 // Set Led Pins #define greenLed 2 #define blueLed 3 #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 Light A1 // Led tro sang camera //------------------------ #define ss A0 int stt_ss; int stt_old_ss; String input_ss; int th = 1023; // ngưỡng int time_get = 250; //------------------------ int stt; int stt_old; String input; int stt_2; int stt_old_2; String input_2; bool Light_stt = false; bool relay_stt = false; bool relay_stt_2 = false; bool sttRun = false; bool Debug = false; ///////////////////////////////////////// Setup /////////////////////////////////// void setup() { //Arduino Pin Configuration pinMode(13, OUTPUT); pinMode(Light, OUTPUT); digitalWrite(13, LOW); pinMode(ledView, OUTPUT); pinMode(ss, INPUT); pinMode(relay, OUTPUT); pinMode(relay_2, OUTPUT); pinMode(button, INPUT); pinMode(button_2, INPUT); digitalWrite(relay, HIGH); // Make sure door is locked digitalWrite(relay_2, HIGH); // Make sure door is locked digitalWrite(Light, LOW); //Protocol Configuration Serial.begin(9600); // Initialize serial communications with PC } ///////////////////////////////////////// Main Loop /////////////////////////////////// void loop() { ReadLightSS(); ReadButton(); SerialRead(); CheckButton(); } ///////////////////////////////////////// Access Granted /////////////////////////////////// // Turn on Light camera void TurnOn_Light() { digitalWrite(Light, HIGH); Light_stt = true; } void TurnOff_Light() { digitalWrite(Light, LOW); Light_stt = false; } void granted(uint16_t setDelay) { 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 } void granted_2(uint16_t setDelay) { digitalWrite(relay_2, LOW); // Unlock door! digitalWrite(ledView, LOW); delay(setDelay); // Hold door lock open for given seconds digitalWrite(relay_2, HIGH); // Relock door digitalWrite(ledView, HIGH); delay(200); // Hold green LED on for a second } // ADD Code void ReadButton() { stt = digitalRead(button); stt_2 = digitalRead(button_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; } //Read Light Sensor bool stt_msg = false; void ReadLightSS() { // stt_ss = digitalRead(ss); int val = analogRead(A0); if (val > th) { delay (time_get); int val = analogRead(A0); if (val > th) { stt_ss = 1; } else { stt_ss = 0; } } else { stt_ss = 0; } // if (stt_ss == 1 && stt_old_ss == 0) { stt_msg = true; // bật cờ gửi tín hiệu cho engine bật ads Light_stt = !Light_stt; if (Light_stt == 1) { TurnOn_Light(); // Serial.println("Light sensor press ON"); } else { TurnOff_Light(); // Serial.println("Light sensor press OFF"); } } if (stt_msg == true){ Serial.println("open_bf"); ClearSerialdata(); } stt_old_ss = stt_ss; } // SD MASTER: 2B429622 // SD TEST: C9B09B6E void SerialRead() { while (Serial.available() > 0) { input = Serial.readStringUntil('\r'); //Serial.println(input); input.trim(); if (input == "3") { if (Light_stt == false) { TurnOn_Light(); } Serial.println("open_3"); ClearSerialdata(); } else if (input == "4") { if (Light_stt == true) { TurnOff_Light(); } Serial.println("open_4"); ClearSerialdata(); } else if (input == "1") { if (relay_stt == false) { relay_stt = true; digitalWrite(13, HIGH); } Serial.println("open_1"); ClearSerialdata(); } else if (input == "2") { if (relay_stt_2 == false) { relay_stt_2 = true; digitalWrite(13, HIGH); } Serial.println("open_2"); ClearSerialdata(); } else if (input == "recv_bf") { stt_msg = false; ClearSerialdata(); } else { digitalWrite(13, LOW); } } 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; } //------------------------- 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; } }