Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
8856306ae4 | ||
|
7f1821622a | ||
|
975d22d1fe | ||
|
ac45124271 | ||
|
9b8192faba |
326
Lock/Lock.ino
326
Lock/Lock.ino
|
@ -1,326 +0,0 @@
|
|||
/* +---------+
|
||||
+----------------------------------->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 <EEPROM.h> // We are going to read and write PICC's UIDs from/to EEPROM
|
||||
|
||||
|
||||
#include <avr/wdt.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
|
||||
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
|
||||
}
|
||||
}
|
|
@ -1,22 +1,31 @@
|
|||
//===============> Version Timer 22/03/2022 Suported by Thaonv <=====================//
|
||||
//===============> Version Timer 22/03/2022 Suported by Haimd <=====================//
|
||||
|
||||
#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 <avr/wdt.h>
|
||||
#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;
|
||||
|
@ -31,7 +40,7 @@ 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;
|
||||
|
@ -40,17 +49,20 @@ 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
|
||||
/* 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);
|
||||
|
@ -68,7 +80,9 @@ void setup()
|
|||
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
|
||||
}
|
||||
|
||||
|
@ -81,6 +95,7 @@ void loop()
|
|||
}
|
||||
|
||||
//----------- Full Function ----------------//
|
||||
|
||||
void funcHandler() {
|
||||
// Relay 1
|
||||
if ( kickFlag_1 == true ) {
|
||||
|
@ -106,6 +121,7 @@ void funcHandler() {
|
|||
|
||||
}
|
||||
// ADD Code
|
||||
|
||||
void ReadButton()
|
||||
{
|
||||
stt = digitalRead(button);
|
||||
|
@ -143,34 +159,15 @@ void SerialRead()
|
|||
//Serial.println(input);
|
||||
input.trim();
|
||||
if(input.indexOf("tdoor")!= -1){ // msg = tdoor_6
|
||||
// Serial.println(Time_openRelay);
|
||||
int t = input.substring(6).toInt();
|
||||
if (t !=0 && t <=30){
|
||||
Time_openRelay = t*1000;
|
||||
}else{
|
||||
// Give time old get
|
||||
}
|
||||
// Serial.println(Time_openRelay);
|
||||
}
|
||||
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();
|
||||
|
@ -206,8 +203,14 @@ void SerialRead()
|
|||
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
|
||||
{
|
||||
|
@ -305,7 +308,7 @@ void printDigits(int digits) {
|
|||
Serial.print('0');
|
||||
Serial.print(digits);
|
||||
}
|
||||
void setTime(byte hr, byte min, byte sec, byte wd, byte d, byte mth, byte yr) // byte wd,
|
||||
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
|
|
@ -0,0 +1,314 @@
|
|||
//===============> Version Timer 22/03/2022 Suported by Haimd <=====================//
|
||||
|
||||
#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 <avr/wdt.h>
|
||||
#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
|
||||
|
||||
int 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 == "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();
|
||||
}
|
||||
//////////////////////////////////////
|
Loading…
Reference in New Issue
Block a user