Fire car with obstacle avoidance

I'm working on a fire extinguisher robot.it will be used to detect and extinguish fire :fire: in a defined environment. It comprise of a 2wd robot car controlled by a l298n motor driver. It has 3 flame sensor mounted in front and the robot can avoid obstacle using and ultrasonic sensor (that is mounted on a servo) . Pls I need help with the code.

The car remains static in its position , but the moment, it senses fire , it Will move to the direction of the fire and extinguish it.

Pls I need help with the code

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This is a textbook case of breaking your project down into separate elements…

  1. Get the car and obstacle avoidance working…
  2. Get the heat sensors working…
  3. Get the fire suppression trigger working…

Stitch them all together.

We can help,at each step along the way, but you have to make the effort!

Cheers.

I have implement each part , except the obstacle avoidance. Also even if :thinking:, I implement each , I don't know how to integrate the whole part together

I'm sorry, I was desperate

ok, now it’s time to post the three chunks you have, and we can suggest how to stitch it together.

Code tags </> will make everyone happy to help you.

Remember millis() if you want the code to be responsive and flexible, no delay() if you can help it !

Okay, I'm coming

#include <Servo.h>
#include <NewPing.h>

#define TRIGGER_PIN  12  
#define ECHO_PIN     11  
#define MAX_DISTANCE 200 
#define MAX_SPEED 150  // set the speed of the dc motors
int distance= 100;

Servo flameservo;
Servo Sonarservo;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// L298 pins
const int ENa = 9;
const int ENb = 10;
const int IN1 = 2;
const int IN2 = 7;
const int IN3 = 4;
const int IN4 = 6;

// flame sensor pins
const int Dflame = 6;
const int Aflame = A0;

//flame extinguisher
const int fan =A5; 
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);

  pinMode(ENa, OUTPUT);
  pinMode(ENb, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
//set the pwm duty cycle for the Enable pin
 // analogWrite(ENa ,MAX_SPEED);
 // analogWrite(ENb , MAX_SPEED);

  flameservo.attach(5);  // attach flame servo to pin 5
  Sonarservo.attach(3); // attach sonar servo to pin 3
  Sonarservo.write(100); // set sonar servo to 100 degree
  delay(2000);     // wait for 2 secs

  // check the distance
distance = readPing();
delay(100);
distance = readPing();
delay(100);


}

void loop() {
  // put your main code here, to run repeatedly:
 analogWrite(ENa ,MAX_SPEED);
  analogWrite(ENb , MAX_SPEED);
int distanceR = 0;
int distanceL = 0;
delay(40);

if(distance <=15){
move_stop();
delay(100);
move_backward();
delay(300);
move_stop();
delay(200);

distanceR = look_right();
delay(300);
distanceL = look_left();
delay(300);

if (distanceR >= distanceL){
  turn_right();
  move_stop();
  }
  else {
  turn_left();
  move_stop();
  }
  }
  else {
  move_forward();
  }

  distance = readPing();




}

void move_forward(){
  digitalWrite(IN1 , HIGH);
  digitalWrite(IN2 , LOW);
  digitalWrite(IN3 , HIGH);
  digitalWrite(IN4 , LOW);
}

void move_backward(){
  digitalWrite(IN1 , LOW);
  digitalWrite(IN2 , HIGH);
  digitalWrite(IN3 , LOW);
  digitalWrite(IN4 , HIGH); 
}

void move_stop(){
  digitalWrite(IN1 , HIGH);
  digitalWrite(IN2 , HIGH);
  digitalWrite(IN3 , HIGH);
  digitalWrite(IN4 , HIGH);  
}

void turn_left(){
   digitalWrite(IN1 , HIGH);
  digitalWrite(IN2 , HIGH);
  digitalWrite(IN3 , HIGH);
  digitalWrite(IN4 , LOW); 
  delay(10);
}

void turn_right(){
   digitalWrite(IN1 , HIGH);
  digitalWrite(IN2 , LOW);
  digitalWrite(IN3 , HIGH);
  digitalWrite(IN4 , HIGH); 
  delay(10);
}
// calculate distance of the ultrasonic sensor
int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0){
    cm = 250;
  }
  return cm;
  }
//make the servo the ultrasonic sensor is mounted on look left and 
//return the distance of obstacle away from it
int look_left() {
  Sonarservo.write(170);
  int distance =readPing();
  delay(2000);
  Sonarservo.write(100);
  return distance;
  delay(100);
}

//make the servo the ultrasonic sensor is mounted on look left and 
//return the distance of obstacle away from it
int look_right(){
  Sonarservo.write(20);
  int distance =readPing();
  delay(2000);
  Sonarservo.write(100);
  return distance;
  delay(100);
}

Should there be a line of code in between each void forward or void backward to tell it the pwm speed (Ena and enb )

I uploaded the code but the car isn't moving :frowning:

As documented, Servo.h disables analogWrite() on pins 9 and 10. So you need to move ENa and ENb to different pins.

Steve

1 Like

Thanks a lot

But can pin 9 and 10 function as a digital pin with ouput High or low??

Yes.

1 Like

I'm building a fire Extinguishing robot. I having issues on how to structure the code in the void loop.

#include <Servo.h>
Servo myservo;
 
int pos = 0;    
boolean fire = false;

#define Left_S 9      // left sensor
#define Right_S 10      // right sensor
#define Forward_S 8 //forward sensor
 
#define LM1 2       // left motor
#define LM2 3       // left motor
#define RM1 4       // right motor
#define RM2 5       // right motor
#define pump 6
 
void setup()
{
  pinMode(Left_S, INPUT);
  pinMode(Right_S, INPUT);
  pinMode(Forward_S, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
  pinMode(pump, OUTPUT);
 
  myservo.attach(11);
  myservo.write(90); 
}
 
void put_off_fire()
{
    delay (500);
 
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    
   digitalWrite(pump, HIGH); delay(500);
    
    for (pos = 50; pos <= 130; pos += 1) { 
    myservo.write(pos); 
    delay(10);  
  }
  for (pos = 130; pos >= 50; pos -= 1) { 
    myservo.write(pos); 
    delay(10);
  }
  
  digitalWrite(pump,LOW);
  myservo.write(90);
  
  fire=false;
}
 
void loop()
{
   myservo.write(90); //Sweep_Servo();  
 
    if (digitalRead(Left_S) ==1 && digitalRead(Right_S)==1 && digitalRead(Forward_S) ==1) //If Fire not detected all sensors are zero
    {
    //Do not move the robot
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    }
    
    else if (digitalRead(Forward_S) ==0) //If Fire is straight ahead
    {
    //Move the robot forward
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
    fire = true;
    }
    
    else if (digitalRead(Left_S) ==0) //If Fire is to the left
    {
    //Move the robot left
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, HIGH);
    }
    
    else if (digitalRead(Right_S) ==0) //If Fire is to the right
    {
    //Move the robot right
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, HIGH);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
    }
    
delay(300); //Slow down the speed of robot
 
     while (fire == true)
     {
      put_off_fire();
     }
}

I don't know how to integrate the first code (obstacle avoidance) with this one that senses fire and goes to extinguish it..

I want it to move autonomously avoiding obstacle and also detecting and extinguishing fire

Hello
I´ve made a low level flight about the sketch and I ´ve found 16 times a call of the delay() function.

	Line 45:   delay(2000);     // wait for 2 secs
	Line 49: delay(100);
	Line 51: delay(100);
	Line 62: delay(40);
	Line 66: delay(100);
	Line 68: delay(300);
	Line 70: delay(200);
	Line 73: delay(300);
	Line 75: delay(300);
	Line 123:   delay(10);
	Line 131:   delay(10);
	Line 135:   delay(70);
	Line 147:   delay(2000);
	Line 150:   delay(100);
	Line 158:   delay(2000);
	Line 161:   delay(100);

The delay() function isn´t delaying it is stopping the processing.
Lay back and think about it.
Have nice day and enjoy coding in C++.

Pls what must I do

Lay back and think about it.

I would think that your robot is going to need to operate in several modes: idle, driving around, moving towards fire and spraying water.

That kind of thing is often handled by a state machine q.v.

The other trick is to build the smallest possible part of the project and test it before adding more code.