user67250: 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();
     }
}


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

    }
}


67250: <p>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 &quot; Flame on Front&quot; 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.</p>
<pre><code>int pos = 0;    
boolean fire = false;

#include &lt;LiquidCrystal.h&gt;  
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 &lt;= 130; pos += 1) { 
    myservo.write(pos); 
    delay(10);  
  }
  for (pos = 130; pos &gt;= 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 &amp;&amp; digitalRead(Right_S)==1 &amp;&amp; digitalRead(Forward_S) ==1) //If Fire not detected all sensors are zero
    {
    //Do not move the robot
    lcd.setCursor(0, 1);
      lcd.print(&quot;No flame&quot;);
    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(&quot;Flame on Left&quot;);
    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(&quot;Flame on front&quot;);
    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(&quot;Flame on Right&quot;);
    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();
     }
}
</code></pre>
 40318: <p>If you format your code correctly, the code becomes less cluttered and the problem becomes more visible.</p>
<p>Also, if you think about the <code>if</code> statements, there is no need to check for <code>no fire</code> for all three sensors at the same time. If none of the three sensors see fire, then the only possibility that is left, is <code>no fire</code>.</p>
<p>Another thing, you have no <code>digitalWrite(IN1, LOW);</code> and no <code>digitalWrite(IN3, LOW);</code> in your code. You could put <code>digitalWrite(IN1, HIGH);</code> and <code>digitalWrite(IN3, HIGH);</code> in <code>setup()</code></p>
<p>Here is your code rewritten and formatted.</p>
<p>Couple of unnecessary duplications are tagged.</p>
<p>There is no reason to use <code>while (fire == true)</code> because <code>put_off_fire()</code> clears the <code>fire</code> flag</p>
<p>Also, for future programs, <code>while (fire == true)</code> is a redundant form because it is same as <code>while (fire)</code></p>
<p>Have a really close look at the <code>if</code> statement blocks.</p>
<p>You used copy&amp;paste when writing the code, but you forgot to make changes.</p>
<pre><code>#include &lt;LiquidCrystal.h&gt;  

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 &lt;= 130; pos += 1) { 
        myservo.write(pos); 
        delay(10);  
    }

    for (pos = 130; pos &gt;= 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(&quot;Flame on Left&quot;);

        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(&quot;Flame on front&quot;);

        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(&quot;Flame on Right&quot;);

        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(&quot;No flame&quot;);

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

    }
}
</code></pre>
