Final Code
Final Code
h>
// Constants
const int SERVO_CENTER = 90; // Servo center position
const int SERVO_LEFT = 45; // Servo left position
const int SERVO_RIGHT = 135; // Servo right position
const int FLAME_THRESHOLD = 500; // Flame detection threshold
void setup() {
// Motor pins setup
pinMode(LM1_FORWARD, OUTPUT);
pinMode(LM1_BACKWARD, OUTPUT);
pinMode(LM2_FORWARD, OUTPUT);
pinMode(LM2_BACKWARD, OUTPUT);
pinMode(RM1_FORWARD, OUTPUT);
pinMode(RM1_BACKWARD, OUTPUT);
pinMode(RM2_FORWARD, OUTPUT);
pinMode(RM2_BACKWARD, OUTPUT);
void loop() {
// Continuously check for flame detection
if (detectFlame()) {
extinguishFire();
} else {
moveForward(); // Move forward if no flame is detected
}
}
while (millis() - pumpStartTime < 5000) { // Run the pump for 5 seconds
digitalWrite(PUMP_PIN, HIGH); // Activate water pump
void moveBackward() {
setMotors(LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH);
}
void turnLeft() {
setMotors(LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW);
}
void turnRight() {
setMotors(HIGH, LOW, LOW, HIGH, HIGH, LOW, LOW, HIGH);
}
void stopMovement() {
setMotors(LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW);
}
PROGRAM EXOLANATION
The detailed and descriptive explanation of the program, broken down into each section,
line by line. This explanation will help you and others understand exactly what the program
does, how it works, and why each part is important.
Constants
const int SERVO_CENTER = 90; // Servo center position
const int SERVO_LEFT = 45; // Servo left position
const int SERVO_RIGHT = 135; // Servo right position
const int FLAME_THRESHOLD = 500; // Flame detection threshold
Purpose: These constants define positions and limits:
o SERVO_CENTER: Center angle for the servo (90 degrees).
o SERVO_LEFT: Leftmost angle (45 degrees).
o SERVO_RIGHT: Rightmost angle (135 degrees).
o FLAME_THRESHOLD: The threshold value for flame detection. If a sensor value is
below this threshold, it means a flame is detected.
2. Setup Function
void setup() {
// Motor pins setup
pinMode(LM1_FORWARD, OUTPUT);
pinMode(LM1_BACKWARD, OUTPUT);
pinMode(LM2_FORWARD, OUTPUT);
pinMode(LM2_BACKWARD, OUTPUT);
pinMode(RM1_FORWARD, OUTPUT);
pinMode(RM1_BACKWARD, OUTPUT);
pinMode(RM2_FORWARD, OUTPUT);
pinMode(RM2_BACKWARD, OUTPUT);
Purpose: Configures all motor control pins as OUTPUT, so they can send signals to the
motors.
// Flame sensor pins setup
pinMode(FLAME_CENTER, INPUT);
pinMode(FLAME_LEFT, INPUT);
pinMode(FLAME_RIGHT, INPUT);
Purpose: Configures the flame sensor pins as INPUT, so they can read data from the
sensors.
// Pump and servo setup
pinMode(PUMP_PIN, OUTPUT);
myservo.attach(SERVO_PIN);
myservo.write(SERVO_CENTER); // Start with servo at center position
Purpose:
o Configures the PUMP_PIN as OUTPUT to control the water pump.
o Attaches the servo motor to SERVO_PIN and sets it to the center position.
Serial.begin(9600);
Serial.println("Firefighting robot initialized and ready!");
Purpose: Initializes serial communication for debugging. It prints status messages to the
Serial Monitor.
3. Main Loop
void loop() {
if (detectFlame()) {
extinguishFire();
} else {
moveForward();
}
}
Purpose:
o detectFlame(): Checks if any flame is detected.
If true (flame detected): It calls extinguishFire() to start extinguishing.
If false (no flame detected): The robot continues moving forward.
Summary of Functionality
1. The robot moves forward continuously.
2. If any flame is detected:
o The robot stops.
o It activates the water pump and sweeps the water pipe using a servo motor.
3. The robot runs the pump for 5 seconds or until the flame is extinguished.
4. After extinguishing, the robot resumes forward movement.