Motor do not start after detecting fire

Hi everyone,

Thanking in advance for the help.

I have tried to mix ultrasonic and fire fighting robot programming to make my project. Problem which i am getting is that when it extinguish the fire it stops there and it do not move until i put some hurdle near ultrasonic.

Please help me out.

i want that when it extinguish fire it move forward. attached the sketch which i made

#include <Servo.h>          //standard library for the servo
#include <NewPing.h>        //for the Ultrasonic sensor function library.

//L298N motor control pins
const int LeftMotorForward = 2;
const int LeftMotorBackward = 3;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;
boolean fire = false;
//sensor pins
#define trig_pin 12 //analog input 1
#define echo_pin 13 //analog input 2
#define pump 6 
/*-------defining Inputs------*/
#define Left_S 9      // left sensor
#define Right_S 10      // right sensor
#define Forward_S 8 //forward sensor
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; 

void setup(){
  pinMode(pump, OUTPUT);
  pinMode(Left_S, INPUT);
  pinMode(Right_S, INPUT);
  pinMode(Forward_S, INPUT);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(7); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}
void put_off_fire()
{
    delay (500);
 
   moveStop();
    
   digitalWrite(pump, HIGH); delay(500);
      
  digitalWrite(pump,LOW);
   
  fire=false;
  
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
  

  if (digitalRead(Right_S) == 1 && digitalRead(Left_S) == 1 && digitalRead(Forward_S) == 1 && distance < 21)
  {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
   
  }
  else {moveForward(); 
    }

    
   if (digitalRead(Right_S) == 0)

   {
     delay(500);
    fire = true;
           
    }
   if (digitalRead(Left_S) == 0)
    {
     delay(500);
    fire = true;
           
    }
   if (digitalRead(Forward_S) == 0)  
    {
     delay(500);
    fire = true;
           
    }
      
  delay(300); //Slow down the speed of robot


     while (fire == true)
     {
      put_off_fire();      
     }
   
    distance = readPing();

}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
 
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH); //2
  digitalWrite(RightMotorBackward, HIGH); //5
  
  digitalWrite(LeftMotorBackward, LOW); //3
  digitalWrite(RightMotorForward, LOW); //4
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

Best_at_1_30.ino (4.37 KB)

When fire is put out You don't reset the fire flag so You end up in this part of code again:

  delay(300); //Slow down the speed of robot


     while (fire == true)
     {
      put_off_fire();      
     }
   
    distance = readPing();

Add a "fire = false" after the "put_off_fire()"

Use Serial print to tell what is happening inside the sketch. That's a technic that's very useful!

I am still getting same problem it stops after detecting fire and it do not move after extinguishing

Post your new code.

#include <Servo.h>          //standard library for the servo
#include <NewPing.h>        //for the Ultrasonic sensor function library.

//L298N motor control pins
const int LeftMotorForward = 2;
const int LeftMotorBackward = 3;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;
boolean fire = false;
//sensor pins
#define trig_pin 12 //analog input 1
#define echo_pin 13 //analog input 2
#define pump 6
/*-------defining Inputs------*/
#define Left_S 9      // left sensor
#define Right_S 10      // right sensor
#define Forward_S 8 //forward sensor
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor;

void setup(){
  pinMode(pump, OUTPUT);
  pinMode(Left_S, INPUT);
  pinMode(Right_S, INPUT);
  pinMode(Forward_S, INPUT);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
 
  servo_motor.attach(7); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}
void put_off_fire()
{
    delay (500);
 
   moveStop();
   
   digitalWrite(pump, HIGH); delay(500);
     
  digitalWrite(pump,LOW);
   
  fire=false;
 
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
 

  if (digitalRead(Right_S) == 1 && digitalRead(Left_S) == 1 && digitalRead(Forward_S) == 1 && distance < 21)
  {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
   
  }
  else {moveForward();
    }

   
   if (digitalRead(Right_S) == 0)

   {
     delay(500);
    fire = true;
           
    }
   if (digitalRead(Left_S) == 0)
    {
     delay(500);
    fire = true;
           
    }
   if (digitalRead(Forward_S) == 0) 
    {
     delay(500);
    fire = true;
           
    }
     
  delay(300); //Slow down the speed of robot


     while (fire == true)
     {
      put_off_fire();     
     fire = false;}
   
    distance = readPing();

}

int lookRight(){ 
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
 
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
 
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
   
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
 
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
 
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH); //2
  digitalWrite(RightMotorBackward, HIGH); //5
 
  digitalWrite(LeftMotorBackward, LOW); //3
  digitalWrite(RightMotorForward, LOW); //4
 
  delay(500);
 
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
 
 
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
 
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
 
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

I suggest using Serial.print from critical places in the code to tell what the state of the sensors is.

I don't know how to use serial.print command in code, i will try it by looking tutorials. if you can please let me know where to put that code.

why it stops and do not move after extinguish and it move when i put any hurdle
it starts by moving backward and take all steps when hurdle came and
starts moving forward. (it look like i have reset the codes)

Okey. Some info.... The basic place is Serial - Arduino Reference

There is a lot of good information there.

In Setup You code "Serial.begin(115200);"
Then use "Serail.print( variable) in places where important variables are present.
In IDE You use, I think, in English, "Tools" and click on Serial monitor. That will open a window showing the messages from the Serial.print in the code.

In Setup You code "Serial.begin(115200);"

Make sure the speed setting in the right bottom corner of the Serial monitor matches this number.

@wvmarle
Thanks! Didn't want to give too much info at the same time.

The default is 9600 - that should be a "safe" one to give in example code. Though I do prefer 115200 myself as well, makes characters appear much faster. I figure best mention that as I've seen way too many "mangled characters!" kind of questions here due to not matching the two speeds.