FINAL PROGRAMMING
FINAL PROGRAMMING
FINAL PROGRAMMING
#include <Servo.h>
// Constants
const int SERVO_CENTER = 90;
const int SERVO_LEFT = 45;
const int SERVO_RIGHT = 135;
const int OBSTACLE_DISTANCE = 20; // cm
const int FLAME_THRESHOLD = 500; // Flame intensity threshold
Servo myservo;
bool isExtinguishing = false;
void setup() {
// Motor pins
pinMode(LM_FORWARD, OUTPUT);
pinMode(LM_BACKWARD, OUTPUT);
pinMode(RM_FORWARD, OUTPUT);
pinMode(RM_BACKWARD, OUTPUT);
// Other pins
pinMode(PUMP_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize components
myservo.attach(SERVO_PIN);
myservo.write(SERVO_CENTER);
Serial.begin(9600);
Serial.println("Firefighting robot initialized and ready!");
}
void loop() {
1
IGNIS
if (isObstacleAhead()) {
handleObstacle();
return;
}
if (detectFlame()) {
extinguishFire();
} else {
moveForward();
}
}
// Flame detection
bool detectFlame() {
int center = analogRead(FLAME_CENTER);
int left = analogRead(FLAME_LEFT);
int right = analogRead(FLAME_RIGHT);
isExtinguishing = true;
stopMovement();
Serial.println("Fire detected! Activating extinguisher...");
while (millis() - pumpStartTime < 5000) { // Keep spraying water for 5 seconds
digitalWrite(PUMP_PIN, HIGH);
digitalWrite(PUMP_PIN, LOW);
myservo.write(SERVO_CENTER); // Reset servo to center position
Serial.println("Fire extinguished. Re-checking for flame...");
2
IGNIS
delay(2000); // Re-check flame after 2 seconds
if (detectFlame()) {
Serial.println("Flame reignited! Re-engaging extinguisher...");
extinguishFire();
}
isExtinguishing = false;
}
// Movement functions
void moveForward() {
setMotors(HIGH, LOW, HIGH, LOW);
}
void moveBackward() {
setMotors(LOW, HIGH, LOW, HIGH);
}
void turnLeft() {
setMotors(LOW, HIGH, HIGH, LOW);
}
void turnRight() {
setMotors(HIGH, LOW, LOW, HIGH);
}
void stopMovement() {
setMotors(LOW, LOW, LOW, LOW);
}
// Obstacle detection
bool isObstacleAhead() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Handle obstacles
void handleObstacle() {
stopMovement();
Serial.println("Obstacle detected. Navigating...");
turnLeft();
3
IGNIS
delay(700); // Turn left
if (isObstacleAhead()) {
turnRight();
delay(1400); // Turn right longer if left is blocked
}
moveForward();
}