-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoor_sensor_mtd2.ino
More file actions
101 lines (66 loc) · 2.06 KB
/
door_sensor_mtd2.ino
File metadata and controls
101 lines (66 loc) · 2.06 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
int ledPin = 13; //considering real light
int inPin = 3; //door open close reading from pin 3
int counter = 0; //counter variable
int door_status = 0; //for status of pin
int a = 0;
int ledPin1 = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
Serial.begin(9600);
}
void loop() {
door_status = digitalRead(inPin);
ledPin1 = digitalRead(ledPin);
digitalWrite(ledPin, LOW);
check();
if (counter == 0)
{
digitalWrite(ledPin, LOW);
Serial.println("ideal state");
}
else if (counter == 1)
{
digitalWrite(ledPin, HIGH);
Serial.println("LIGHT IS ON door opend for first time ");
}
else if (counter == 2)
{
digitalWrite(ledPin, HIGH);
Serial.println("light will remain on until door is opened from inside ");
}
else
{
digitalWrite(ledPin, LOW);
Serial.println("OFF");
delay(5000);
counter = 0;
}
}
void check() {
if (door_status == LOW && ledPin1 == LOW)
{
// Serial.println("ideal state"); //case 1 door is closed by default
// digitalWrite(ledPin,LOW); // led is also off
counter = 0;
}
else if (door_status == HIGH && ledPin1 == LOW && counter == 0 )
{
// CASE 2 sensor is active means door is open from outside
//light will remain on until i close the door and timeout concept goes here
//Serial.println("LIGHT IS ON door opend for first time ");
// Serial.println("ON");
// digitalWrite(ledPin,HIGH);
counter = 1;
}
else if (ledPin1 == HIGH && door_status == LOW) //CASE 3 door is closed from inside means someone is inside [door is closed ][light is ON]
{
// Serial.println("light will remain on until door is opened from inside ");//light will remain on until door is opened from inside
// digitalWrite(ledPin,HIGH);
counter = 2; //explecitly setting counter to 2
}
else if (door_status == HIGH && ledPin1 == HIGH && counter == 2 )
{
counter = 3; //explecitly setting counter to 3
}
}