Compare commits

...

3 Commits
V3.1 ... master

Author SHA1 Message Date
quynhnm
342e88148d new version 2024-08-15 11:10:26 +07:00
quynhnm
c2e38e7de3 mv file 2024-08-15 10:58:35 +07:00
thaonguyen
a9c357a482 them settime cho ds1307 2022-09-15 10:57:42 +07:00
3 changed files with 413 additions and 2 deletions

View File

@ -35,8 +35,8 @@
*/
#include <EEPROM.h> // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h> // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices
#include <avr/wdt.h>
#define ledView 13

90
Lock/lock_main_202407.ino Normal file
View File

@ -0,0 +1,90 @@
const int ledPin1 = 5; // Chân kết nối LED 1
const int ledPin2 = 6; // Chân kết nối LED 2
const int buttonPin1 = 7; // Chân kết nối nút nhấn 1
const int buttonPin2 = 8; // Chân kết nối nút nhấn 2
bool ledState1 = false; // Trạng thái của LED 1
bool ledState2 = false; // Trạng thái của LED 2
#define Light A1 // Led tro sang camera
unsigned long previousMillis1 = 0; // Thời điểm lưu trữ cho LED 1
unsigned long previousMillis2 = 0; // Thời điểm lưu trữ cho LED 2
int timedelay= 5000;
void setup() {
pinMode(ledPin1, OUTPUT); // Thiết lập chân là OUTPUT cho LED 1
pinMode(ledPin2, OUTPUT); // Thiết lập chân là OUTPUT cho LED 2
pinMode(buttonPin1, INPUT_PULLUP); // Thiết lập chân là INPUT_PULLUP cho nút nhấn 1
pinMode(buttonPin2, INPUT_PULLUP); // Thiết lập chân là INPUT_PULLUP cho nút nhấn 2
digitalWrite(ledPin1, HIGH); // Make sure door is locked
digitalWrite(ledPin2, HIGH); // Make sure door is locked
digitalWrite(Light, LOW);
Serial.begin(9600); // Bắt đầu kết nối Serial
}
void loop() {
checkserial();
readbutton();
checkrelay();
}
void readbutton(){
// Kiểm tra nếu nút nhấn 1 được nhấn
if (digitalRead(buttonPin1) == LOW) {
// Bật LED 1 và ghi lại thời điểm hiện tại
digitalWrite(ledPin1, LOW);
ledState1 = true;
previousMillis1 = millis();
}
// Kiểm tra nếu nút nhấn 2 được nhấn
if (digitalRead(buttonPin2) == LOW) {
// Bật LED 2 và ghi lại thời điểm hiện tại
digitalWrite(ledPin2, LOW);
ledState2 = true;
previousMillis2 = millis();
}
}
void checkserial(){
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n'); // Đọc từ Serial cho đến khi gặp ký tự xuống dòng
input.trim(); // Loại bỏ các khoảng trắng và ký tự xuống dòng từ đầu và cuối chuỗi
if (input == "1") {
// Bật LED 1 và ghi lại thời điểm hiện tại
digitalWrite(ledPin1, LOW);
Serial.println("open_1");
ledState1 = true;
previousMillis1 = millis();
} else if (input == "2") {
// Bật LED 2 và ghi lại thời điểm hiện tại
digitalWrite(ledPin2, LOW);
Serial.println("open_2");
ledState2 = true;
previousMillis2 = millis();
}else if (input == "3"){
digitalWrite(Light, HIGH);
Serial.println("open_3");
}else if (input == "4"){
digitalWrite(Light, LOW);
Serial.println("open_4");
}else if (input.indexOf("tdoor")!= -1){
int t = input.substring(5,7).toInt();
Serial.println(t);
if (t !=0 && t <=30){
timedelay = t*1000;
}
}
}
}
void checkrelay(){
// Kiểm tra nếu LED 1 đã được bật và thời gian đã trôi qua 1000 ms (1 giây)
if (ledState1 && (millis() - previousMillis1 >= timedelay)) {
digitalWrite(ledPin1, HIGH); // Tắt LED 1
ledState1 = false; // Đặt trạng thái LED 1 về false
}
// Kiểm tra nếu LED 2 đã được bật và thời gian đã trôi qua 1000 ms (1 giây)
if (ledState2 && (millis() - previousMillis2 >= timedelay)) {
digitalWrite(ledPin2, HIGH); // Tắt LED 2
ledState2 = false; // Đặt trạng thái LED 2 về false
}
}

View File

@ -0,0 +1,321 @@
//===============> 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();
}
//////////////////////////////////////