add light sensor p2

This commit is contained in:
HaiMD 2021-06-09 10:24:59 +07:00
parent 65daad87ac
commit 05616e8868
2 changed files with 72 additions and 20 deletions

BIN
Lock.rar Normal file

Binary file not shown.

View File

@ -67,10 +67,19 @@
#define relay_2 6 //6 // Set Relay 1 Pin
#define button_2 8 // button exit
#define wipeB A0 // Button pin for WipeMode
#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;
@ -97,11 +106,11 @@ void setup()
digitalWrite(13, LOW);
pinMode(ledView, OUTPUT);
pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
pinMode(ss, INPUT);
pinMode(relay, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(button,INPUT);
pinMode(button_2,INPUT);
pinMode(button, INPUT);
pinMode(button_2, INPUT);
digitalWrite(relay, HIGH); // Make sure door is locked
digitalWrite(relay_2, HIGH); // Make sure door is locked
@ -113,9 +122,10 @@ void setup()
///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop()
{
ReadButton();
SerialRead();
CheckButton();
ReadLightSS();
ReadButton();
SerialRead();
CheckButton();
}
///////////////////////////////////////// Access Granted ///////////////////////////////////
@ -167,7 +177,7 @@ void ReadButton()
{
relay_stt = false;
}
if (stt_2 == 0 && stt_old_2 == 1)
if (stt_2 == 0 && stt_old_2 == 1)
{
//Serial.println("Button 2 press");
relay_stt_2 = true;
@ -180,6 +190,45 @@ void ReadButton()
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;
}
@ -206,7 +255,7 @@ void SerialRead()
TurnOff_Light();
}
Serial.println("open_4");
Serial.println("open_4");
ClearSerialdata();
}
else if (input == "1")
@ -219,7 +268,7 @@ void SerialRead()
}
Serial.println("open_1");
ClearSerialdata();
}else if (input == "2")
} else if (input == "2")
{
if (relay_stt_2 == false)
@ -229,8 +278,11 @@ void SerialRead()
}
Serial.println("open_2");
ClearSerialdata();
}
else
} else if (input == "recv_bf")
{
stt_msg = false;
ClearSerialdata();
} else
{
digitalWrite(13, LOW);
}
@ -259,7 +311,7 @@ void CheckButton()
relay_stt = false;
}
//-------------------------
if (relay_stt_2 == true)
if (relay_stt_2 == true)
{
granted_2(4000); // Open the door lock for 300 ms
relay_stt_2 = false;