-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceCode.ino
More file actions
247 lines (217 loc) · 5.91 KB
/
SourceCode.ino
File metadata and controls
247 lines (217 loc) · 5.91 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Include prebuilt library supports
#include <Servo.h> // Servo motor library header
// Initialize pins
//
// Wheel Motors
const int motorFrontL = 9; // Front Left Motor
const int motorFrontR = 8; // Front Right Motor
const int motorBackL = 10; // Back Left Motor
const int motorBackR = 11; // Back Right Motor
// UltraSonic Sensors
const int trigPinL = 30; // Ultrasonic Trig Pin
const int trigPinR = 31; // Ultrasonic Trig Pin
const int echoPinL = 32; // Left Ultrasonic Echo Pin
const int echoPinR = 33; // Right Ultrasonic Echo Pin
long durationL, durationR;
int distanceL, distanceR;
// buzzerer
const int buzzer = 22; // buzzerer Trigger Pin
// Water Pump
const int pump = 52; // Water Pump Trigger Pin
// Flame Sensors
const int flameMainAnalog = A11; // Middle Flame Analog Pin
const int flameLeftAnalog = A10; // Middle Flame Analog Pin
const int flameRightAnalog = A12; // Middle Flame Analog Pin
const int flameMain = 41; // Middle Flame Digital Pin
const int flameLeft = 40; // Left Flame Digital Pin
const int flameRight = 42; // Right Flame Digital Pin
// Servo Motor
Servo myservo; // Create servo object
int pos = 80; // Store servo position
void setup(){
// Serial COM
Serial.begin(9600);
Serial.println("System Booted!"); // Notify about Standby Boot
alertTone();
Serial.println("Setup Pin Modes"); // Setup Pin Modes
pinMode(motorFrontL, OUTPUT);
pinMode(motorFrontR, OUTPUT);
pinMode(motorBackL, OUTPUT);
pinMode(motorBackR, OUTPUT);
pinMode(trigPinL, OUTPUT);
pinMode(trigPinR, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(pump, OUTPUT);
pinMode(echoPinL, INPUT);
pinMode(echoPinR, INPUT);
pinMode(flameLeft, INPUT);
pinMode(flameMain, INPUT);
pinMode(flameRight, INPUT);
myservo.attach(9); // attach the servo on pin 9
}
void loop(){
// Set Devices to Standby
digitalWrite(buzzer, LOW);
digitalWrite(trigPinL, LOW);
digitalWrite(trigPinR, LOW);
digitalWrite(flameLeft, HIGH);
digitalWrite(flameMain, HIGH);
digitalWrite(flameRight, HIGH);
myservo.write(pos);
delay(500);
// Start Main Loop
switch ( checkfire() ){
case 1:
Serial.println("Fire detected in left sensor, turning left..");
alertTone();
moveForward();
moveLeft();
delay(200);
break;
case 2:
Serial.println("Fire Detected in Main Sensor, Checking obstacles..");
alertTone();
if( analogRead(flameMainAnalog) >= 35 ) {
switch( obstacle() ){
case 1:
Serial.println("Obstacle on left."); // Obstacle on left.
moveRight();
moveForward();
moveForward();
moveLeft();
delay(300);
break;
case 2:
Serial.println("Obstacle on right."); // Obstacle on right.
moveLeft();
moveForward();
moveForward();
moveRight();
delay(300);
break;
default:
Serial.println("No obstacles. Moving Forward!");
moveForward();
delay(300);
}
}
while( digitalRead(flameMain) == LOW ) {
Serial.println("Starting Water Spray");
pumpStart();
Serial.println("Starting Servo to spread water spray");
while( digitalRead(flameMain) == LOW ) {
servoControl();
}
pumpStop();
alertTone();
}
break;
case 3:
Serial.println("Fire detected in right sensor, turning right..");
alertTone();
moveForward();
moveRight();
delay(200);
break;
default:
Serial.println("No fire detected! Waiting in standby mode.");
}
}
// Additional Tasks
int moveForward() {
digitalWrite(motorFrontL, HIGH);
digitalWrite(motorFrontR, HIGH);
digitalWrite(motorBackL, HIGH);
digitalWrite(motorBackR, HIGH);
delay(500);
digitalWrite(motorFrontL, LOW);
digitalWrite(motorFrontR, LOW);
digitalWrite(motorBackL, LOW);
digitalWrite(motorBackR, LOW);
}
int moveLeft(){
digitalWrite(motorFrontR, HIGH);
digitalWrite(motorBackR, HIGH);
delay(500);
digitalWrite(motorFrontR, LOW);
digitalWrite(motorBackR, LOW);
}
int moveRight(){
digitalWrite(motorFrontL, HIGH);
digitalWrite(motorBackL, HIGH);
delay(500);
digitalWrite(motorFrontL, LOW);
digitalWrite(motorBackL, LOW);
}
int obstacle(){
// Trigger Ultrasonics
digitalWrite(trigPinL, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinL, LOW);
durationL = pulseIn(echoPinL, HIGH);
distanceL = durationL * 0.034 / 2;
digitalWrite(trigPinR, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinR, LOW);
durationR = pulseIn(echoPinR, HIGH);
distanceR = durationR * 0.034 / 2;
// Check for obstacles
if ( distanceL <= 5 ){
return 1;
}
else if ( distanceR <= 5 ){
return 2;
}
else{
return 0;
}
}
int checkfire(){
// Check Flame Sensor Data
if ( analogRead(flameLeftAnalog) <= 900 || analogRead(flameMainAnalog) <= 900 || analogRead(flameRightAnalog) <= 900 )
{
if ( analogRead(flameLeftAnalog) < analogRead(flameMainAnalog) && analogRead(flameLeftAnalog) < analogRead(flameRightAnalog) )
return 1;
else if ( analogRead(flameMainAnalog) < analogRead(flameLeftAnalog) && analogRead(flameMainAnalog) < analogRead(flameRightAnalog) )
return 2;
else if ( analogRead(flameRightAnalog) < analogRead(flameMainAnalog) && analogRead(flameRightAnalog) < analogRead(flameLeftAnalog) )
return 3;
}
else
return 0;
}
int pumpStart(){
digitalWrite(pump, HIGH);
delay(15);
}
int pumpStop(){
digitalWrite(pump, LOW);
delay(15);
}
int servoControl(){
for(pos = 80; pos > 35; pos--)
{
myservo.write(pos);
delay(20);
}
for(pos = 35; pos <= 80; pos++)
{
myservo.write(pos);
delay(20);
}
for(pos = 80; pos < 125; pos++)
{
myservo.write(pos);
delay(20);
}
for(pos = 125; pos >= 80; pos--)
{
myservo.write(pos);
delay(20);
}
}
int alertTone(){
tone(buzzer, 500, 100);
delay(200);
tone(buzzer, 1000, 250);
}