0

I have a problem. I am making an autonomous (obstacle avoiding) firefighting firetruck. I already have a code however it only stops when it detects fire and when the ultrasonic reaches less than 15cm, the flame sensor automatically detects fire even there is no fire near the sensor. I get my code form different tutorials.

Here it is:

#include <Servo.h> 
#include "Ultrasonic.h"

//Constants
const int button = 2;     
const int led    = 3;     
const int pump = 4;     
const int motorA1= 6;     
const int motorA2= 9;         
const int motorB1=10;     
const int motorB2=11;     
const int flame=12;
const int buzzer=12;

Ultrasonic ultrasonic(A4 ,A5); 
Servo myservo;         



//Variables
int distance;         
int checkRight;
int checkLeft;
int function=0;         
int buttonState=0;            
int pos=90;            
int flag=0;           
int flame_detected;

void setup()
{
 Serial.begin(9600);
   myservo.attach(5);      
   myservo.write(pos);        
 pinMode(button, INPUT_PULLUP);
 pinMode(led,  OUTPUT);
 pinMode(buzzer, OUTPUT);
 pinMode(motorA1,OUTPUT);
 pinMode(motorA2,OUTPUT);
 pinMode(motorB1,OUTPUT);
 pinMode(motorB2,OUTPUT);
 pinMode(flame,INPUT);
 pinMode(pump,OUTPUT);

}

void loop()
{
 flame_detected = digitalRead(flame);
 if (flame_detected == true)
 {
   Serial.println("No flame detected. stay cool");
 //Check button state
 buttonState = digitalRead(button);
 unsigned long currentMillis = millis(); 


   if (buttonState == LOW) {
     delay(500);
     if ( flag == 0){
         function = 1;
         flag=1; 
     }
     else if ( flag == 1){ 
       function = 0;
       flag=0; 
     }    
   }

 if (function == 0){   
   myservo.write(90);    
   stop();         
   noTone(buzzer);     
   digitalWrite(led, HIGH);

 }
 else if (function == 1){
   //Read distance...
   distance = ultrasonic.Ranging(CM); 

   if (distance > 15){
     forward(); 
     noTone(buzzer);
     digitalWrite(led,LOW);
   }
   else if (distance <=15){
     stop(); 
     tone(buzzer,500); 
     digitalWrite(led,HIGH); 

     for(pos = 0; pos < 180; pos += 1){  
               myservo.write(pos);              
               delay(10);                      
           } 

           checkLeft = ultrasonic.Ranging(CM);   

           for(pos = 180; pos>=0; pos-=1){                               
               myservo.write(pos);              
               delay(10);                         
           }

           checkRight= ultrasonic.Ranging(CM);

           myservo.write(90);                   


           if (checkLeft < checkRight){
             left();
             delay(400);          
             }
           else if (checkLeft > checkRight){
             right();
             delay(400);
           }
           else if (checkLeft <=10 && checkRight <=10){
             backward(); 
             left();
           }
   }
 }

}

else {
 put_off_fire();
 Serial.println("Flame detected...! take action immediately.");
}
delay(500);
}
void forward(){
 digitalWrite(motorA1, HIGH);
 digitalWrite(motorA2, LOW);
 digitalWrite(motorB1, HIGH);
 digitalWrite(motorB2, LOW); 
}

void backward(){
 digitalWrite(motorA1, LOW);
 digitalWrite(motorA2, HIGH);
 digitalWrite(motorB1, LOW);
 digitalWrite(motorB2, HIGH);
}

void left(){
 digitalWrite(motorA1, HIGH);
 digitalWrite(motorA2, LOW);
 digitalWrite(motorB1, LOW);
 digitalWrite(motorB2, HIGH);
}

void right(){
 digitalWrite(motorA1, LOW);
 digitalWrite(motorA2, HIGH);
 digitalWrite(motorB1, HIGH);
 digitalWrite(motorB2, LOW); 
}

void stop(){
 digitalWrite(motorA1, LOW);
 digitalWrite(motorA2, LOW);
 digitalWrite(motorB1, LOW);
 digitalWrite(motorB2, LOW);
}

void put_off_fire()
{
 {
   delay (500);

   digitalWrite(motorA1, HIGH);
   digitalWrite(motorA2, HIGH);
   digitalWrite(motorB1, HIGH);
   digitalWrite(motorB2, HIGH);

  digitalWrite(pump, HIGH);
  delay(500); 
 }
 digitalWrite(pump,LOW);
 flame_detected=false; 
}

I really need help. Thanks

  • 1
    What flame sensor are you using? – chrisl Nov 1 at 9:07
  • 1
    You need to provide a lot of information about your sketch if you expect us to help with it. What hardware are you using? Where can we read about it? You need to document the various sections of your code, what they're supposed to do, and explain the parts that aren't operating as you expect them to. – Duncan C Nov 1 at 14:14
  • Sorry, I forget about that. I am using Arduino Uno, Hl-01 flame sensor, HC-SR04 ultrasonic sensor, 2wd robot, servo motor(allows ultrasonic and flame sensor to check side and front of the robot), button and l298n – Nydigorith Nov 2 at 7:18
  • I mostly rely on this tutorial ardumotive.com/obstacle-avoiding-robot.html for the obstacle avoiding code then I just added some code from other tutorials regarding with the fire/flame detection and fire fighting robot which I found on the Arduino project hub – Nydigorith Nov 2 at 7:25
  • I could not find a datasheet for that flame sensor. Can you link to one from? Maybe from your reseller? – chrisl Nov 3 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.