0% found this document useful (0 votes)
8 views

Final Code

Very good for programming fire extinguishing robot

Uploaded by

abhijt Maitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Final Code

Very good for programming fire extinguishing robot

Uploaded by

abhijt Maitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <Servo.

h>

// Flame sensor pins


#define FLAME_CENTER A0
#define FLAME_LEFT A1
#define FLAME_RIGHT A2

// Motor control pins for 4 motors


#define LM1_FORWARD 2
#define LM1_BACKWARD 3
#define LM2_FORWARD 4
#define LM2_BACKWARD 5
#define RM1_FORWARD 6
#define RM1_BACKWARD 7
#define RM2_FORWARD 8
#define RM2_BACKWARD 9

// Servo and pump control pins


#define PUMP_PIN 10
#define SERVO_PIN 11

// 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

Servo myservo; // Servo object for pipe movement


bool isExtinguishing = false; // Flag to prevent multiple activations

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);

// Flame sensor pins setup


pinMode(FLAME_CENTER, INPUT);
pinMode(FLAME_LEFT, INPUT);
pinMode(FLAME_RIGHT, INPUT);

// Pump and servo setup


pinMode(PUMP_PIN, OUTPUT);
myservo.attach(SERVO_PIN);
myservo.write(SERVO_CENTER); // Start with servo at center position

// Start Serial Monitor for debugging


Serial.begin(9600);
Serial.println("Firefighting robot initialized and ready!");
}

void loop() {
// Continuously check for flame detection
if (detectFlame()) {
extinguishFire();
} else {
moveForward(); // Move forward if no flame is detected
}
}

// Flame detection function


bool detectFlame() {
int center = analogRead(FLAME_CENTER);
int left = analogRead(FLAME_LEFT);
int right = analogRead(FLAME_RIGHT);

// Debugging output for sensor readings


Serial.print("Flame Readings - Center: ");
Serial.print(center);
Serial.print(", Left: ");
Serial.print(left);
Serial.print(", Right: ");
Serial.println(right);

// Return true if any sensor detects a flame


return (center < FLAME_THRESHOLD || left < FLAME_THRESHOLD || right <
FLAME_THRESHOLD);
}

// Fire extinguishing function


void extinguishFire() {
if (isExtinguishing) return; // Prevent re-activation if already extinguishing

isExtinguishing = true; // Set extinguishing flag


stopMovement(); // Stop robot movement
Serial.println("Fire detected! Activating extinguisher...");

unsigned long pumpStartTime = millis(); // Track time for water dispensing

while (millis() - pumpStartTime < 5000) { // Run the pump for 5 seconds
digitalWrite(PUMP_PIN, HIGH); // Activate water pump

// Sweeping motion for the servo


for (int angle = SERVO_LEFT; angle <= SERVO_RIGHT; angle += 5) {
myservo.write(angle);
delay(50); // Smooth sweeping motion
}
for (int angle = SERVO_RIGHT; angle >= SERVO_LEFT; angle -= 5) {
myservo.write(angle);
delay(50);
}

// Re-check flame during extinguishing


if (!detectFlame()) break; // Stop if flame is extinguished
}

digitalWrite(PUMP_PIN, LOW); // Turn off the pump


myservo.write(SERVO_CENTER); // Reset servo position
Serial.println("Fire extinguished. Re-checking for flame...");

delay(2000); // Wait 2 seconds before re-checking for flame


if (detectFlame()) { // If flame reignites, re-activate extinguisher
Serial.println("Flame reignited! Re-engaging extinguisher...");
extinguishFire();
}

isExtinguishing = false; // Reset flag


}

// Motor movement functions


void moveForward() {
setMotors(HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW);
}

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);
}

// Set motor states for 4 motors


void setMotors(int lm1Forward, int lm1Backward, int lm2Forward, int lm2Backward,
int rm1Forward, int rm1Backward, int rm2Forward, int rm2Backward) {
digitalWrite(LM1_FORWARD, lm1Forward);
digitalWrite(LM1_BACKWARD, lm1Backward);
digitalWrite(LM2_FORWARD, lm2Forward);
digitalWrite(LM2_BACKWARD, lm2Backward);
digitalWrite(RM1_FORWARD, rm1Forward);
digitalWrite(RM1_BACKWARD, rm1Backward);
digitalWrite(RM2_FORWARD, rm2Forward);
digitalWrite(RM2_BACKWARD, rm2Backward);
}

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.

1. Library and Pin Definitions


#include <Servo.h>
 Purpose: The Servo.h library is included to control the servo motor. The servo motor is
responsible for aiming the water pipe in different directions during the extinguishing
process.

Flame Sensor Pin Definitions


#define FLAME_CENTER A0
#define FLAME_LEFT A1
#define FLAME_RIGHT A2
 Purpose: Defines the pins for the flame sensors connected to the Arduino's analog
input pins.
 Sensors:
o FLAME_CENTER: Sensor detecting flame directly in front.
o FLAME_LEFT: Sensor detecting flame to the left.
o FLAME_RIGHT: Sensor detecting flame to the right.
Motor Control Pins for 4 Motors
#define LM1_FORWARD 2
#define LM1_BACKWARD 3
#define LM2_FORWARD 4
#define LM2_BACKWARD 5
#define RM1_FORWARD 6
#define RM1_BACKWARD 7
#define RM2_FORWARD 8
#define RM2_BACKWARD 9
 Purpose: Defines the pins to control the four motors:
o LM1 and LM2: Left-side motors.
o RM1 and RM2: Right-side motors.
 Each motor has two control pins:
o Forward Pin: Controls forward movement.
o Backward Pin: Controls backward movement.

Servo and Pump Control Pins


#define PUMP_PIN 10
#define SERVO_PIN 11
 Purpose:
o PUMP_PIN: Controls the water pump for extinguishing the fire.
o SERVO_PIN: Controls the servo motor that moves the water pipe (left, right, or
center).

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.

4. Flame Detection Function


bool detectFlame() {
int center = analogRead(FLAME_CENTER);
int left = analogRead(FLAME_LEFT);
int right = analogRead(FLAME_RIGHT);
 Purpose: Reads analog values from the center, left, and right flame sensors.
Serial.print("Flame Readings - Center: ");
Serial.print(center);
Serial.print(", Left: ");
Serial.print(left);
Serial.print(", Right: ");
Serial.println(right);
 Purpose: Prints the sensor values for debugging.
return (center < FLAME_THRESHOLD || left < FLAME_THRESHOLD || right <
FLAME_THRESHOLD);
}
 Purpose: Checks if any sensor value is less than the threshold. If so, it returns true
(flame detected).

5. Fire Extinguishing Function


void extinguishFire() {
if (isExtinguishing) return;
isExtinguishing = true;
 Purpose: Prevents multiple activations of extinguishing while it's already running.
stopMovement();
Serial.println("Fire detected! Activating extinguisher...");
 Purpose: Stops the robot's movement and prints a status message.
unsigned long pumpStartTime = millis();

while (millis() - pumpStartTime < 5000) {


digitalWrite(PUMP_PIN, HIGH);
 Purpose: Runs the water pump for 5 seconds while the servo sweeps back and forth.
for (int angle = SERVO_LEFT; angle <= SERVO_RIGHT; angle += 5) {
myservo.write(angle);
delay(50);
}
for (int angle = SERVO_RIGHT; angle >= SERVO_LEFT; angle -= 5) {
myservo.write(angle);
delay(50);
}
 Purpose: Moves the servo motor smoothly from left to right and back.
if (!detectFlame()) break; // Stop sweeping if flame is extinguished
}
 Purpose: If the flame is no longer detected, stop extinguishing early.
digitalWrite(PUMP_PIN, LOW);
myservo.write(SERVO_CENTER);
Serial.println("Fire extinguished.");
isExtinguishing = false;
}
 Purpose: Turns off the water pump and resets the servo to the center position.

6. Motor Control Functions


Move Forward
void moveForward() {
setMotors(HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW);
}
 Purpose: Sets all motors to move forward.
Stop Movement
void stopMovement() {
setMotors(LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW);
}
 Purpose: Stops all motors.
Motor State Function
void setMotors(int lm1Forward, int lm1Backward, int lm2Forward, int lm2Backward,
int rm1Forward, int rm1Backward, int rm2Forward, int rm2Backward) {
digitalWrite(LM1_FORWARD, lm1Forward);
digitalWrite(LM1_BACKWARD, lm1Backward);
digitalWrite(LM2_FORWARD, lm2Forward);
digitalWrite(LM2_BACKWARD, lm2Backward);
digitalWrite(RM1_FORWARD, rm1Forward);
digitalWrite(RM1_BACKWARD, rm1Backward);
digitalWrite(RM2_FORWARD, rm2Forward);
digitalWrite(RM2_BACKWARD, rm2Backward);
}
 Purpose: Sets the direction for all four motors using digital signals.

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.

You might also like