0

I had been trying to make Arduino Fire Fighter Robot but facing several problems in interfacing 3 flame sensors and servo together. When I upload the following code( Which I had posted at the end of my writing) my robot goes backward and detects flame at the front sensor and displays " Flame on Front" on 16x2 LCD display and the 5v pump pumps out water and the red LED blinks. But I want that my robot will work in the following way- Initially, my robot will be at rest. There are 3 sensors attached at the front part of the robot. And there is a SG90 servo for rotating the water tank( in this tank there is a 5v pump motor and the pump motor will pump out water from this tank). When any one of the three flame sensors will detect flame then the servo will rotate in that direction and when the front flame sensor detects fire , the servo will be in 90-degree angle and water will be pumped out. LCD will display the status of the flame sensors. But the robot is not working properly. I am not able to interface 3 flame sensors and servo together. My robot is not working according to my expectation as I had said before.

int pos = 0;    
boolean fire = false;

#include <LiquidCrystal.h>  
int Contrast=20;// for adjusting the brightness of the LCD , 20 is selected for the contrast of LCD
LiquidCrystal lcd(30,31,32,33,34,35);   
int buzz= 38; //for led or buzzer
#define Left_S 9 // left sensor
#define Right_S 8     // right sensor
#define Forward_S 23  //forward sensor
#define LM1 52 //for pump motor     
#define LM2 53 //for pump motor
#define IN1 5   //for motor  
#define IN2 4  // for motor    
#define IN3 3      //for motor
#define IN4 22     // for motor
//int flame_pin1 = HIGH ; 
int LEDR= 36; // for danger signal
int LEDB=37;// for safe signal

void setup ( )  {
   analogWrite(6,Contrast);
     lcd.begin(16, 2);
      pinMode(Left_S, INPUT);
  pinMode(Left_S, INPUT);
  pinMode(Right_S, INPUT);
     pinMode (Forward_S,OUTPUT);
     pinMode (LEDB,OUTPUT);
 pinMode(LM1, OUTPUT);
 pinMode(LM2, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode( buzz,OUTPUT );
  pinMode(LEDR,OUTPUT);
  pinMode(LEDB, OUTPUT);
 // pinMode ( flame_sensor_pinM , INPUT ); 
  //Serial.begin ( 9600 );
   myservo.attach(25);
  myservo.write(90);
}
 void put_off_fire()
{
    delay (500);
 
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);
    
   digitalWrite(LM1, HIGH); 
   digitalWrite(LM2, LOW);
   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(LM1,LOW);
  digitalWrite(LM2,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
    lcd.setCursor(0, 1);
      lcd.print("No flame");
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);
    digitalWrite(LEDR,LOW);
    digitalWrite(LEDB,HIGH);
    digitalWrite(buzz,LOW);
    }
    
    else if (digitalRead(Left_S) ==0) //If Fire is straight ahead
    {
    //Move the robot forward
    lcd.setCursor(0, 1);
      lcd.print("Flame on Left");
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(LEDR,HIGH);
    digitalWrite(LEDB,LOW);
    digitalWrite(buzz,HIGH);
    fire = true;
    
    }
    else if (digitalRead(Forward_S) ==0) //If Fire is straight ahead
    {
    //Move the robot forward
    lcd.setCursor(0, 1);
      lcd.print("Flame on front");
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN2, LOW);
    digitalWrite(LEDR,HIGH);
    digitalWrite(LEDB,LOW);
    digitalWrite(buzz,HIGH);
    fire = true;
    
    }
    
    
    
    else if (digitalRead(Right_S) ==0) //If Fire is to the right
    {
    //Move the robot right
    lcd.setCursor(0, 1);
      lcd.print("Flame on Right");
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
    digitalWrite(LEDR,HIGH);
    digitalWrite(LEDB,LOW);
    digitalWrite(buzz,HIGH);
    fire= true;
    }
    
     delay(300); //Slow down the speed of robot
 
     while (fire == true)
     {
      put_off_fire();
     }
}
  • your code is badly indented, so it is difficult to follow the program flow without reformatting – jsotola Jun 28 at 17:35
1

If you format your code correctly, the code becomes less cluttered and the problem becomes more visible.

Also, if you think about the if statements, there is no need to check for no fire for all three sensors at the same time. If none of the three sensors see fire, then the only possibility that is left, is no fire.

Another thing, you have no digitalWrite(IN1, LOW); and no digitalWrite(IN3, LOW); in your code. You could put digitalWrite(IN1, HIGH); and digitalWrite(IN3, HIGH); in setup()

Here is your code rewritten and formatted.

Couple of unnecessary duplications are tagged.

There is no reason to use while (fire == true) because put_off_fire() clears the fire flag

Also, for future programs, while (fire == true) is a redundant form because it is same as while (fire)

Have a really close look at the if statement blocks.

You used copy&paste when writing the code, but you forgot to make changes.

#include <LiquidCrystal.h>  

LiquidCrystal lcd(30, 31, 32, 33, 34, 35);   

#define Left_S       9                       // left    sensor
#define Right_S      8                       // right   sensor
#define Forward_S   23                       // forward sensor

#define LM1         52                       // for pump motor     
#define LM2         53                       // for pump motor

#define IN1          5                       // for motor  
#define IN2          4                       // for motor    
#define IN3          3                       // for motor
#define IN4         22                       // for motor

//int flame_pin1 = HIGH ;

int LEDR     = 36;                           // for danger signal
int LEDB     = 37;                           // for safe signal
int buzz     = 38;                           // for led or buzzer
int pos      =  0;    
int Contrast = 20;                           // for adjusting the brightness of the LCD , 20 is selected for the contrast of LCD

boolean fire = false;

void setup() {

    analogWrite(6, Contrast);

    lcd.begin(16, 2);

    pinMode( Left_S            , INPUT  );
    pinMode( Left_S            , INPUT  );
    pinMode( Right_S           , INPUT  );
    
    pinMode( Forward_S         , OUTPUT );
    pinMode( LEDB              , OUTPUT );   // xxxxxxxxxxxxx        duplicate

    pinMode( LM1               , OUTPUT );
    pinMode( LM2               , OUTPUT );

    pinMode( IN1               , OUTPUT );
    pinMode( IN2               , OUTPUT );
    pinMode( IN3               , OUTPUT );
    pinMode( IN4               , OUTPUT );

    pinMode( buzz              , OUTPUT );

    pinMode( LEDR              , OUTPUT );
    pinMode( LEDB              , OUTPUT );   // xxxxxxxxxxxxx        duplicate
    
//  pinMode( flame_sensor_pinM , INPUT  );
 
//  Serial.begin ( 9600 );

    myservo.attach(25);
    myservo.write(90);
}

void put_off_fire() {

    delay (500);

    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, HIGH);

    digitalWrite(LM1, HIGH); 
    digitalWrite(LM2, LOW );
    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(LM1, LOW);
    digitalWrite(LM2, LOW);

    myservo.write(90);                       // xxxxxxxxxxxxx        duplicate

    fire = false;                            // xxxxxxxxxxxxx        not needed
}


void loop() {

    myservo.write(90);                       // xxxxxxxxxxxxx        duplicate
//  Sweep_Servo();    

    lcd.setCursor(0, 1);

    fire = true;                             // preset the state (gets cleared when no fire is detected

    if (digitalRead(Left_S) == 0) {          // If Fire is straight ahead

        lcd.print("Flame on Left");

        digitalWrite(IN1 , HIGH);            // Move the robot forward
        digitalWrite(IN2 , LOW );
        digitalWrite(IN3 , HIGH);
        digitalWrite(IN2 , LOW );
    }

    else if (digitalRead(Forward_S) == 0) {  // If Fire is straight ahead

        lcd.print("Flame on front");

        digitalWrite(IN1 , HIGH);            // Move the robot forward
        digitalWrite(IN2 , LOW );
        digitalWrite(IN3 , HIGH);
        digitalWrite(IN2 , LOW );
    }

    else if (digitalRead(Right_S) == 0) {    // If Fire is to the right

        lcd.print("Flame on Right");

        digitalWrite(IN1 , HIGH);            // Move the robot right
        digitalWrite(IN2 , HIGH);
        digitalWrite(IN3 , HIGH);
        digitalWrite(IN4 , LOW );
    }

    else {                                   // fire was not detected by any of the sensors

        fire = false;

        lcd.print("No flame");

        digitalWrite(IN1 , HIGH);            // Do not move the robot
        digitalWrite(IN2 , HIGH);
        digitalWrite(IN3 , HIGH);
        digitalWrite(IN4 , HIGH);
    }

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

    if (fire) {
        
        digitalWrite(LEDR, HIGH);            // do fire lights
        digitalWrite(LEDB, LOW);
        digitalWrite(buzz, HIGH);

        put_off_fire();
    }

    else {
        
        digitalWrite(LEDR, LOW );            // do nofire lights
        digitalWrite(LEDB, HIGH);
        digitalWrite(buzz, LOW );

    }
}
| improve this answer | |
  • Thanks for your suggestion. But the right motor starts to move when I upload the code to Arduino. And the servo is not also rotating. Suppose, the servo is at 90-degree angle. When the left sensor will detect fire then the servo needs to rotate at the left side. The servo's position remain at 90 degree position. Why? – NUSRATRAHMAN Jun 29 at 11:01
  • Will I have to erase myservo.write(90) and fire= false; from Void put off fire()? Beside these two lines you wrote duplicate and not necessary – NUSRATRAHMAN Jun 29 at 11:31
  • no, you do not have to delete duplicate lines ... i simply pointed out that you have duplicate code which indicates that you my want to review your code to make sure that you have not overlooked someting else – jsotola Jun 29 at 15:16
  • Thank u so much. But right motor starts to rotate automatically when I upload the code why? Your suggestion will be highly appreciated. – NUSRATRAHMAN Jun 29 at 15:43
  • test your motors ... write a sketch that runs one motor for one second, then reverses it, then the other motor – jsotola Jun 29 at 16:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.