-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_code.cpp
204 lines (181 loc) · 4.49 KB
/
Final_code.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//The complete code for SIP project:
// definition of digital pins as pin to control the LED
int led1 = 13; // output of Ultrasonic 01
int led2 = 12; // output of Ultrasonic 02
int led3 = 11; // output of IR Sensor
int led4 = 10;
//output of Water Sensor
int led5 = 7;
//output of PIR
// connect Ultrasonic sensor to arduino pins
int trigPin1 = 1;
int echoPin1 = 2;
int trigPin2 = 3;
int echoPin2 = 4;
float Speed = 0.0343; // sound speed in cm/us
long duration, dist1, dist2;
// connect ir sensor to arduino pin 9
int IRSensor = 9;
// connects Water Sensor to aurdino pins
int waterSens = A5;
int waterVal;
// connects PIR sensor
int PIRsensor = 5;
// the pin that the PIR sensor is atteched to
int state = LOW;
// by default, no motion detected
int val = 0;
// variable to store the sensor status (value)
void
setup()
{
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT); // Ultrasonic1 sensor pin INPUT
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT); // Ultrasonic2 sensor pin INPUT
pinMode(IRSensor, INPUT); // IR sensor pin INPUT
//led Output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(waterSens, INPUT); // Water sensor Input
pinMode(PIRsensor, INPUT);
// initialize PIRsensor as an input
}
void loop()
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
//Ultrasonic sensor
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
dist1 = Speed * duration / 2;
if (dist1 < 200)
{
digitalWrite(led1, HIGH);
}
else
{
digitalWrite(led1, LOW);
}
digitalWrite(trigPin2, LOW);
delayMicroseconds(200);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(300);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
dist2 = Speed * duration / 2;
if (dist2 < 10)
{
digitalWrite(led2, HIGH);
}
else
{
digitalWrite(led2, LOW);
}
//IR Sensor Code
int statusSensor = digitalRead(IRSensor);
if (statusSensor == 1)
{
digitalWrite(led3, HIGH);
delay(100);
//Setting the delay time, 1 sec
digitalWrite(led3, LOW);
delay(100);
//Setting the delay time, 1 sec
}
else
{
digitalWrite(led3, LOW);
}
//Water Sensor
waterVal = analogRead(waterSens);
if (waterVal <= 600)
{
digitalWrite(led4, HIGH);
delay(100);
//Setting the delay time, 1 sec
digitalWrite(led4, LOW);
delay(100);
//Setting the delay time, 1 sec
}
else
{
digitalWrite(led4, LOW);
}
//PIR Sensor
val = digitalRead(PIRsensor);
// read sensor value
if (val == HIGH)
{
// check if the sensor is HIGH
digitalWrite(led5, HIGH);
// turn LED5 ON
delay(500);
// delay 100 milliseconds
if (state == LOW)
{
Serial.println("Motion detected!");
state = HIGH;
// update variable state to HIGH
}
}
else
{
digitalWrite(led5, LOW); // turn LED5 OFF
delay(500);
// delay 200 milliseconds
if (state == HIGH)
{
Serial.println("Motion stopped!");
state = LOW;
// update variable state to LOW
}
}
}