Lock_Arduino/lock_settime_start/lock_settime_start.ino
2024-08-15 10:58:35 +07:00

322 lines
7.4 KiB
C++

//===============> Version Timer 22/03/2022 Suported by Thaonv <=====================//
#include <SPI.h> // RC522 Module uses SPI protocol
#include "TimerOne.h"
#include <Wire.h>
#define ledView 13
//Ver 3
// LED 12V - A1
//Relay 1 - D5
//Button 1 - D8
// Relay 2 - D6
// Button 2 - D7
long Time_openRelay = 4000 ; // 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;
String timestart;
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;
int day_st,month_st,year_st,second_st,minute_st,hour_st,thu_st;
///////////////////////////////////////// Setup ///////////////////////////////////
void setup()
{
Wire.begin();//i2c
//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.attachInterrupt(funcHandler); // Every x_us
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
int t = input.substring(6).toInt();
if (t !=0 && t <=30){
Time_openRelay = t*1000;
}else{
// Give time old get
}
}
else if (input.indexOf("time_set")!= -1){
timestart = input.substring(8);
hour_st=timestart.substring(0,2).toInt();
minute_st=timestart.substring(3,5).toInt();
second_st=timestart.substring(6,8).toInt();
day_st=timestart.substring(9,11).toInt();
String month_st=timestart.substring(12,14);
if(month_st.indexOf("0")!= -1){
int vitri0 = month_st.indexOf("0"); //5
String Str7 = month_st.substring(vitri0+1,vitri0+2);
month_st=Str7;
}else{
}
int month_st1=month_st.toInt();
year_st=timestart.substring(17,19).toInt();
thu_st=timestart.substring(20,22).toInt();
setTime(hour_st, minute_st, second_st, thu_st, day_st, month_st1, year_st); // 12:30:45 CN 08-02-2015
}
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") {
readDS1307();
digitalClockDisplay();
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) // byte wd,
{
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();
}
//////////////////////////////////////