From ac45124271c060729760b542b6dc33d7d39e430c Mon Sep 17 00:00:00 2001 From: macduyhai Date: Thu, 21 Apr 2022 16:32:52 +0700 Subject: [PATCH] Add code test time open door from enigne --- .../Lock_Timer_21042022_Test_TimeOpenDoor.ino | 322 ++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor.ino diff --git a/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor.ino b/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor.ino new file mode 100644 index 0000000..f00e2bf --- /dev/null +++ b/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor/Lock_Timer_21042022_Test_TimeOpenDoor.ino @@ -0,0 +1,322 @@ +//===============> Version Timer 22/03/2022 Suported by Haimd <=====================// + +#include // We are going to read and write PICC's UIDs from/to EEPROM +#include // RC522 Module uses SPI protocol +#include +#include "TimerOne.h" +#include +#define ledView 13 + + +//Ver 3 +// LED 12V - A1 +//Relay 1 - D5 +//Button 1 - D8 +// Relay 2 - D6 +// Button 2 - D7 + +long Time_openRelay = 3000 ; // ms - Thoi gian giu mo khoa + +#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 +//------------------------ + +int stt; +int stt_old; +String input; +/* Địa chỉ của DS1307 */ +const byte DS1307 = 0x68; +/* Số byte dữ liệu sẽ đọc từ DS1307 */ +const byte NumberOfFields = 7; + +/* khai báo các biến thời gian */ +int second, minute, hour, day, wday, month, year; +//------------------------ +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; + +//--------------------------------------------------------- + +unsigned long countFlagRelay_1 = 0; +unsigned long countFlagRelay_2 = 0; + +bool kickFlag_1 = false; +bool kickFlag_2 = false; + + +///////////////////////////////////////// Setup /////////////////////////////////// +void setup() +{ + Wire.begin();//i2c + /* cài đặt thời gian cho module */ + setTime(15, 06, 00, 3, 22, 3, 22); // 12:30:45 CN 08-02-2015 + //Arduino Pin Configuration + pinMode(13, OUTPUT); + pinMode(Light, OUTPUT); + digitalWrite(13, LOW); + + pinMode(ledView, OUTPUT); + + 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); + // Init timer + Timer1.initialize(1000); // initialize timer1, and set a 1/2 second period + // Timer1.pwm(9, 512); // setup pwm on pin 9, 50% duty cycle + Timer1.attachInterrupt(funcHandler); // Every x_us + //Protocol Configuration + Serial.begin(9600); // Initialize serial communications with PC +} + +///////////////////////////////////////// Main Loop /////////////////////////////////// +void loop() +{ + ReadButton(); + SerialRead(); + CheckButton(); +} + +//----------- Full Function ----------------// + +void funcHandler() { + // Relay 1 + if ( kickFlag_1 == true ) { + countFlagRelay_1 ++; + if ( countFlagRelay_1 >= Time_openRelay ) { + countFlagRelay_1 = 0; + kickFlag_1 = false; + digitalWrite(relay, HIGH); // Unlock relay 1 + //Serial.println("Lock 1"); + } + } + // Relay 2 + if ( kickFlag_2 == true ) { + countFlagRelay_2 ++; + if ( countFlagRelay_2 >= Time_openRelay ) { + countFlagRelay_2 = 0; + kickFlag_2 = false; + digitalWrite(relay_2, HIGH); // Unlock relay 1 + //Serial.println("Lock 2"); + } + } + + +} +// 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; +} + + +void SerialRead() +{ + while (Serial.available() > 0) + { + input = Serial.readStringUntil('\r'); + //Serial.println(input); + input.trim(); + if(input.indexOf("tdoor")!= -1){ // msg = tdoor_6 +// Serial.println(Time_openRelay); + long t = input.substring(6)).toInt(); + if (t !=0){ + Time_openRelay = t*1000; + } +// Serial.println(Time_openRelay); + } + else 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; + } + Serial.println("open_1"); + ClearSerialdata(); + } else if (input == "2") + { + + if (relay_stt_2 == false) + { + relay_stt_2 = true; + } + Serial.println("open_2"); + ClearSerialdata(); + }else if (input == "5") { + // starttime = millis(); + readDS1307(); + digitalClockDisplay(); + // longtime=millis()-starttime; + + + // Serial.println(longtime); +// Serial.println("open_5"); + ClearSerialdata(); + } else + { + digitalWrite(13, LOW); + } + } + ClearSerialdata(); +} + + +void CheckButton() +{ + if (relay_stt == true) + { + digitalWrite(relay, LOW); // Unlock relay 1' + noInterrupts(); //Hủy các ngắt trước đó. + kickFlag_1 = true; + interrupts(); //Cho phép ngắt + relay_stt = false; + ClearSerialdata(); + } + + //------------------------- + if (relay_stt_2 == true) + { + digitalWrite(relay_2, LOW); // Unlock relay 1 + kickFlag_2 = true; + relay_stt_2 = false; + ClearSerialdata(); + } +} +// Turn on Light camera +void TurnOn_Light() { + digitalWrite(Light, HIGH); + Light_stt = true; +} +void TurnOff_Light() { + digitalWrite(Light, LOW); + Light_stt = false; +} + + +void ClearSerialdata() +{ + while (Serial.available() > 0) + { + char c = Serial.read(); + } +} +//get time từ ds1307 +void readDS1307() +{ + Wire.beginTransmission(DS1307); + Wire.write((byte)0x00); + Wire.endTransmission(); + Wire.requestFrom(DS1307, NumberOfFields); + + second = bcd2dec(Wire.read() & 0x7f); + minute = bcd2dec(Wire.read() ); + hour = bcd2dec(Wire.read() & 0x3f); // chế độ 24h. + wday = bcd2dec(Wire.read() ); + day = bcd2dec(Wire.read() ); + month = bcd2dec(Wire.read() ); + year = bcd2dec(Wire.read() ); + year += 2000; +} +/* Chuyển từ format BCD (Binary-Coded Decimal) sang Decimal */ +int bcd2dec(byte num) +{ + return ((num / 16 * 10) + (num % 16)); +} +/* Chuyển từ Decimal sang BCD */ +int dec2bcd(byte num) +{ + return ((num / 10 * 16) + (num % 10)); +} +void digitalClockDisplay() { + // digital clock display of the time + Serial.print(hour); + printDigits(minute); + printDigits(second); + Serial.print(" "); + Serial.print(day); + Serial.print(" "); + Serial.print(month); + Serial.print(" "); + Serial.print(year); + Serial.println(); +} +void printDigits(int digits) { + // các thành phần thời gian được ngăn chách bằng dấu : + Serial.print(":"); + + if (digits < 10) + Serial.print('0'); + Serial.print(digits); +} +void setTime(byte hr, byte min, byte sec, byte wd, byte d, byte mth, byte yr) +{ + Wire.beginTransmission(DS1307); + Wire.write(byte(0x00)); // đặt lại pointer + Wire.write(dec2bcd(sec)); + Wire.write(dec2bcd(min)); + Wire.write(dec2bcd(hr)); + Wire.write(dec2bcd(wd)); // day of week: Sunday = 1, Saturday = 7 + Wire.write(dec2bcd(d)); + Wire.write(dec2bcd(mth)); + Wire.write(dec2bcd(yr)); + Wire.endTransmission(); +} +//////////////////////////////////////