-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDR_PIR_Kamar_Mandi_Millis.ino
42 lines (37 loc) · 1.42 KB
/
LDR_PIR_Kamar_Mandi_Millis.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define ldr A0
#define ledPin D6
#define pir D5
#define relay D7
unsigned long previousPIR_Millis = 0; //save time-millis after executing the currentMillis
const long intervalPIR = 2.5*60000; //time for the lamp in ON state
int pirState = LOW; //State of PIR for the first time
int ledState = LOW; //State of LED for the first time
int relayValue = LOW; //Value for executing relay
bool BlinkTime = false;
void setup() {
Serial.begin(9600); //begin the serial
pinMode(pir, INPUT); //declare sensor as input
pinMode(ledPin, OUTPUT); //declare LED as output
pinMode(relay, OUTPUT); //declare relay as output
}
void loop() {
int valueLDR = analogRead(ldr); //Reading the illumination value from LDR
long state = digitalRead(pir); //Reading the illumination value from PIR
pirState = state; //Stating the PIR to monitor in the serial
Serial.println(state);
unsigned long currentMillis = millis();
//The Blink Time will be activated and put the LED and lamp (via relay) to HIGH
//after the object is detected and LDR value >620
if(digitalRead(pir) && valueLDR >= 620){
BlinkTime = true;
previousPIR_Millis = millis();
digitalWrite(ledPin, HIGH);
digitalWrite(relay, HIGH);
}
//The Blink Time will be removed and put the LED and lamp (via relay) to LOW
if(BlinkTime && millis()-previousPIR_Millis >= intervalPIR){
BlinkTime = false;
digitalWrite(ledPin, LOW);
digitalWrite(relay,LOW);
}
}