Arduino autonomous firefighting robot

Hi all! I’m trying to build an Arduino autonomous firefighting robot. I’ve complied the code this far. and when i upload the code to the board the robot is not moving. All the hardware components are tested and working separately. Can somebody help me with the code? Cheers!

#include<Servo.h>
#include <Ultrasonic.h>
#define pump 12
#define flame_R 50
#define flame_L 24
#define flame_M 36

Servo Pump_servo;

int diatance_L=0;
int diatance_R=0;
int diatance_M=0;

int motorA1 = 2;
int motorA2 = 3;
int motorB1 = 4;
int motorB2 = 6;

bool f_L=HIGH;
bool f_R=HIGH;
bool f_M=HIGH;

Ultrasonic ultrasonic_R(22,23);
Ultrasonic ultrasonic_L(52, 53);

void setup() {

Pump_servo.attach(11);
Serial.begin(9600);
pinMode(pump,OUTPUT);
pinMode(flame_R,INPUT);
pinMode(flame_L,INPUT);
pinMode(flame_M,INPUT);
analogWrite(8,100);
analogWrite(9,100);
}

void loop() {

Serial.print(diatance_L);
//Serial.print(" ");
//Serial.println(diatance_R);

diatance_L=ultrasonic_L.read();
diatance_R=ultrasonic_R.read();

if(diatance_L>30/&&diatance_R>30/){
brake();
left();
flame_Check();
delay(1000);
brake();
forward();
flame_Check();
delay(1500);
brake();
delay(100);}
elseright();

//Serial.println(diatance_L);
//Serial.print(" ");
//Serial.println(diatance_R);
flame_Check();
}
if(diatance_L<30/&&diatance_R<30/){
brake();
right();
flame_Check();
delay(1000);
brake();
forward();
flame_Check();
delay(1000);
brake();
delay(100);
flame_Check();}
else
left();
}
flame_Check();
delay(500);
}
void forward()
{

//Serial.println(“Forward”);
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,HIGH);
digitalWrite(motorB1,LOW);
digitalWrite(motorB2,HIGH);

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

}

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

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

void pumpstart()
{
digitalWrite(pump,HIGH);
}

void pumpstop()
{
digitalWrite(pump,LOW);
}

void flame_Check()

{ f_L=digitalRead(flame_L);
f_R=digitalRead(flame_R);
f_M=digitalRead(flame_M);

if(f_R==LOW||f_L==LOW||f_M==LOW)
{
brake();
if(f_R==LOW){

Pump_servo.write(0);
delay(15);

}
else if(f_L==LOW){

Pump_servo.write(180);
delay(15);

}
else if(f_M==LOW){

Pump_servo.write(90);
delay(15);

}
while(f_R==LOW||f_L==LOW||f_M==LOW){
pumpstart();
delay(1000);
f_L=digitalRead(flame_L);
f_R=digitalRead(flame_R);
f_M=digitalRead(flame_M);
}
pumpstop();

}
}

This is some sort of annual contest, isn't it? Every year there are questions about this, going back many years. And it seems every time a question is posted, the poster assumes everyone on the forum already knows about the contest, and the poster has not read the forum guide before posting.

Is there documentation given to those who enter the contest which suggests posting their questions on the Arduino forum?

Nope> yeah I've heard about the competition. But this is a side project of mine. Cheers mate!

Upe:
Nope> yeah I've heard about the competition. But this is a side project of mine. Cheers mate!

Seems you did not take the hint to read General Guidance and How to use the forum

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

...R

#include<Servo.h>
#include <Ultrasonic.h>
#define pump 12
#define flame_R 50 
#define flame_L 24
#define flame_M 36

Servo Pump_servo;

int diatance_L=0;
int diatance_R=0;
int diatance_M=0;

int motorA1 = 2;
int motorA2 = 3;
int motorB1 = 4;
int motorB2 = 6;

bool f_L=HIGH;
bool f_R=HIGH;
bool f_M=HIGH;

Ultrasonic ultrasonic_R(22,23);
Ultrasonic ultrasonic_L(52, 53);

void setup() {

  Pump_servo.attach(11);
  Serial.begin(9600);
  pinMode(pump,OUTPUT);
  pinMode(flame_R,INPUT);
  pinMode(flame_L,INPUT);
  pinMode(flame_M,INPUT);
  analogWrite(8,100);
  analogWrite(9,100);
}

void loop() {
  
  Serial.print(diatance_L);
  //Serial.print("   ");
  //Serial.println(diatance_R);
  
  diatance_L=ultrasonic_L.read();
    diatance_R=ultrasonic_R.read();
    
  while(diatance_L>30/*&&diatance_R>30*/){
    brake();
   forward();
    flame_Check();
  delay(1000);
        brake();
        forward();
        flame_Check();
        delay(1500);
        brake();
        delay(100);
    
    
    //Serial.println(diatance_L);
  //Serial.print("   ");
  //Serial.println(diatance_R);
    flame_Check();
  }
  while(diatance_L<30/*&&diatance_R<30*/){
    brake();
    backwards();
    flame_Check();
        delay(1000);
        brake();
        forward();
        flame_Check();
        delay(1000);
        brake();
        delay(100);
   
  }
flame_Check();
    delay(500);
   }
  void forward()
  {

    //Serial.println("Forward");
    digitalWrite(motorA1,LOW);
    digitalWrite(motorA2,HIGH);
    digitalWrite(motorB1,LOW);
    digitalWrite(motorB2,HIGH); 
    
    
      
}
void brake()
  {     digitalWrite(motorA1,LOW);
        digitalWrite(motorA2,LOW);
        digitalWrite(motorB1,LOW);
        digitalWrite(motorB2,LOW);
        
  }

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


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

void pumpstart()
{
  digitalWrite(pump,HIGH);
}

void pumpstop()
{
  digitalWrite(pump,LOW);
}

void flame_Check()

{     f_L=digitalRead(flame_L);
      f_R=digitalRead(flame_R); 
      f_M=digitalRead(flame_M);
   
      if(f_R==LOW||f_L==LOW||f_M==LOW)
    {
      brake();
      if(f_R==LOW){

       Pump_servo.write(0);
       delay(15); 
        
      }
      else if(f_L==LOW){

      Pump_servo.write(180);  
      delay(15);     
        
      }
      else if(f_M==LOW){

      Pump_servo.write(90); 
      delay(15);
        
      }
      while(f_R==LOW||f_L==LOW||f_M==LOW){
      pumpstart();
      delay(1000);
      f_L=digitalRead(flame_L);
      f_R=digitalRead(flame_R); 
      f_M=digitalRead(flame_M); 
      }
      pumpstop();
      
    }
}

Robin2:
please modify your post

@Upe, If you want to get good at Arduino/electronics/coding, attention to detail is a crucial skill to develop.

while(diatance_L>30/*&&diatance_R>30*/) You don’t appear to re-read “diatance” (my auto-correct has problems with that!) inside the loop. (I could be wrong - your inconsistent formatting makes it very hard to read your code)

If you can’t figure out what your code is doing, sprinkle a few prints around.

Hi,
Are you using an Arduino Mega?

Tom.. :slight_smile:

You probably get rid of the delay(15); for the servos as they are probably not needed and just slow the program down.

Hi,
Have you written your code in stages, to check that each part works separately?
If so;
Do you have code that JUST involves controlling the motor, NOTHING else?
Does it work?
If you don't have code JUST to control your motor, then write it and see if you have control.

What hardware are you using?
What servo?
What motor controller?
What ultrasonic sensor?
What model Arduino controller?

Also in the IDE, select TOOLS then AUTO FORMAT.
It will indent your code so it is easier to read.

Thanks.. Tom... :slight_smile:

yeah when i check the code in parts it works. i'm using a mega board, 9g servo,l293d motor driver, 3 one way flame sensors and hs04 ultrasonic sensors

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How are you powering all the motors and servos?

Thanks… Tom… :slight_smile:

Upe:
yeah when i check the code in parts it works.

If the parts work separately and if you combine the parts one at a time, at what point does it stop working?

Keep in mind that we only know about your project what you tell us.

…R

Hi,
Try this, you were using while statements, but not checking the variable that it was conditional on inside the while loop.

#include<Servo.h>
#include <Ultrasonic.h>
#define pump 12
#define flame_R 50
#define flame_L 24
#define flame_M 36
Servo Pump_servo;
int diatance_L = 0;
int diatance_R = 0;
int diatance_M = 0;
int motorA1 = 2;
int motorA2 = 3;
int motorB1 = 4;
int motorB2 = 6;
bool f_L = HIGH;
bool f_R = HIGH;
bool f_M = HIGH;

Ultrasonic ultrasonic_R(22, 23);
Ultrasonic ultrasonic_L(52, 53);

void setup()
{
  Pump_servo.attach(11);
  Serial.begin(9600);
  pinMode(pump, OUTPUT);
  pinMode(flame_R, INPUT);
  pinMode(flame_L, INPUT);
  pinMode(flame_M, INPUT);
  analogWrite(8, 100);
  analogWrite(9, 100);
}


void loop()
{
  Serial.print(diatance_L);
  //Serial.print("   ");
  //Serial.println(diatance_R);
  diatance_L = ultrasonic_L.read();
  diatance_R = ultrasonic_R.read();
  if (diatance_L > 30/*&&diatance_R>30*/)
  {
    brake();
    forward();
    flame_Check();
    delay(1000);
    brake();
    forward();
    flame_Check();
    delay(1500);
    brake();
    delay(100);
    //Serial.println(diatance_L);
    //Serial.print("   ");
    //Serial.println(diatance_R);
    flame_Check();
  }
  if (diatance_L < 30/*&&diatance_R<30*/)
  {
    brake();
    backwards();
    flame_Check();
    delay(1000);
    brake();
    forward();
    flame_Check();
    delay(1000);
    brake();
    delay(100);
  }
  flame_Check();
  delay(500);
}
//=============================
void forward()
{
  //Serial.println("Forward");
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, HIGH);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}
//=========================
void brake()
{ digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, LOW);
}
//=========================
void backwards() {
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW);
}
//================
void right()
{ digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, HIGH);
  digitalWrite(motorB1, HIGH); //right wheel
  digitalWrite(motorB2, LOW);
}
//============================
void left()
{ digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW); //left wheel
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}
//=================
void pumpstart()
{
  digitalWrite(pump, HIGH);
}
//=================
void pumpstop()
{
  digitalWrite(pump, LOW);
}
//=======================
void flame_Check()
{ f_L = digitalRead(flame_L);
  f_R = digitalRead(flame_R);
  f_M = digitalRead(flame_M);


  if (f_R == LOW || f_L == LOW || f_M == LOW)
  {
    brake();
    if (f_R == LOW)
    {
      Pump_servo.write(0);
      delay(15);
    }
    else if (f_L == LOW)
    {
      Pump_servo.write(180);
      delay(15);
    }
    else if (f_M == LOW)
    {
      Pump_servo.write(90);
      delay(15);
    }
    if (f_R == LOW || f_L == LOW || f_M == LOW)
    {
      pumpstart();
      delay(1000);
      f_L = digitalRead(flame_L);
      f_R = digitalRead(flame_R);
      f_M = digitalRead(flame_M);
    }
    pumpstop();
  }
}

Its untried but my get things moving.

Tom… :slight_smile:

Thank you so much everybody for helping me out and happy new year!!

Hi,
Has your problem been solved?

Tom... :slight_smile: