1#define ledPin D4
2#define sensorPin D1
3#define relayPin D2
4
5unsigned
6 long previousMillis = 0;
7const long interval = 1000;
8int serialAlert = 0;
9
10void
11 setup() {
12
13
14 Serial.begin(9600);
15
16 pinMode(ledPin, OUTPUT);
17 pinMode(sensorPin, INPUT);
18 pinMode(relayPin,
19 OUTPUT);
20}
21
22void loop() {
23
24
25
26 unsigned long currentMillis = millis();
27
28 if (currentMillis - previousMillis
29 >= interval) {
30 previousMillis = currentMillis;
31
32 if (digitalRead(sensorPin)
33 == LOW) {
34 if (serialAlert == 0) {
35 Serial.println("Objected
36 Detected");
37 serialAlert = 1;
38 }
39 digitalWrite(ledPin,
40 HIGH);
41 digitalWrite(relayPin, LOW);
42 }
43
44 else {
45 if
46 (serialAlert == 1) {
47 Serial.println("No Objected Detected");
48 serialAlert
49 = 0;
50 }
51 digitalWrite(ledPin, LOW);
52 digitalWrite(relayPin,
53 HIGH);
54 }
55
56 }
57
58}