Firefighter Code Help, Need to Combine Navigation code with Flame Detection

Ok, I am entering a firefighting contest at a local univesrsity, this is my first time programming or building a robot

I have written the code for therobot to navigate the maze,If your familiar with the maze used at trinity, this bot goes into the island room, then calls a function to be a left wall follower

#include <EncodersAB.h>
#include <Motors.h>
#include <SharpIR.h>


Motors drive = Motors(); //drive motors
SharpIR rightir = SharpIR(GP2D12,3);
SharpIR leftir = SharpIR(GP2D12,1);
SharpIR centerir = SharpIR(GP2D12,2);
volatile int flame=0;
int r=analogRead(5);
int l=analogRead(4);
int c = analogRead(0);
boolean wallFollow  = false;
boolean follow = false;

void setup()
{
 
  // This version separates the initial turn and then allows the bot to detect a room from the forward function
  Serial.begin(38400);
  delay(250);
  int r=analogRead(5);
  int l=analogRead(4);
  int c = analogRead(0);
 

}

void loop()
{
int r=analogRead(5);
int c = analogRead(0);
int l=analogRead(4);



 
 
   
  drive.set(125,125);
 

 
  
 
 
 
 
 
  if((centerir.getData()<15)){//against a wall with an opening on one side
 
    if(rightir.getData()>leftir.getData()){//Against a wall
            drive.set(0,0);
            delay(300);
            do{drive.set(150,-150);//Turn to the right
            }while(centerir.getData()<30);           
            drive.set(100,100);//Move forward to avoid the loop
            delay(1500); //May have to increase or decrease this value
    }
   
     if(rightir.getData()<leftir.getData()){
            drive.set(0,0);
            delay(300);
            do{drive.set(-150,150);//Turn to the left
            }while(centerir.getData()<30);    
            drive.set(100,100);//Move Forward to avoid the loop   
            delay(1500); //May have to increase or decrease this value
    }
  }
 
  if(((rightir.getData()>60)&&(leftir.getData()>40))&&(centerir.getData()>30)){// Opening on the right hand side /// this might not be required, possibly omit
     drive.set(0,0);
     delay(300);
     drive.set(100,100); //Get Clearance for the turn
     delay(1000);
     drive.set(150,-150); // turn right
     delay(450);
     drive.set(100,100); //move forward to avoid the loop
     delay(1000);
  }
 
 
  if(((leftir.getData()>60)&&(rightir.getData()<40))&&(centerir.getData()>30)){ //Opening on the left hand side not against a wall /// this might nor be required, possibly emit
     drive.set(0,0);
     delay(300);
     drive.set(100,100);//Get Clearance for the turn
     delay(1000);
     drive.set(-150,150);//Turn Left
     delay(450);
     drive.set(100,100);//Move into the turn
     delay(1000); //Was 1000 before, changing for test  
 }
 
 
 
 if((leftir.getData()<30)&&(rightir.getData()<30)&&centerir.getData()>15){ //Used for going down hallways
      do{drive.set(125,125);
         if(rightir.getData()<18){  //Alligns the robot in case it gets too close to the wall
          do{ drive.set(125,150); //150
          }while(rightir.getData()<18);
         }
         if(leftir.getData()<18){ //Alligns the robot in case it gets too close to the wall
           do{drive.set(150,125);//150
           }while(leftir.getData()<18);
         }
        
        if(leftir.getData()>30&&rightir.getData()<20&&centerir.getData()>15){
          drive.set(0,0);
          delay(250);
          drive.set(100,100);
          delay(1500); //changed from 1500
          drive.set(-150,150);
          delay(450);
          drive.set(100,100);
          delay(1500);
            if(rightir.getData()>30&&leftir.getData()<30){

      }
        }
      if(rightir.getData()>30&&leftir.getData()<30&&centerir.getData()>15){
        drive.set(0,0);
        delay(250);
          drive.set(100,100);
          delay(1500);
          drive.set(150,-150);
          delay(450);
          drive.set(100,100);
          delay(1500);
            if(rightir.getData()>30&&leftir.getData()<30){
              roomSearchright();
      }
      }
     
     
         
      }while(centerir.getData()>15); //runs this loop until the robot hits a wall or finds another opening ||(centerir.getData()>20)
 }
 
  

delay(500);
 
}

 

void roomSearchright() //Used to look around a room entered from the right side, exit left, three rotations broken up to find the flame
{
 
  boolean follow = false;
    drive.set(0,0);
    delay(250);
   
   do{ drive.set(100,100);
   }while(centerir.getData()<20); 
   drive.set(0,0);
   delay(500);
  
    drive.set(105,-105);//Rotate Around The Room Looking for fire
    delay(466);
    drive.set(0,0);
    delay(250);
   
   drive.set(105,-105);//Rotate Around The Room Looking for fire
    delay(466);
    drive.set(0,0);
    delay(250);
   
   drive.set(105,-105);//Rotate Around The Room Looking for fire
    delay(466);
    drive.set(0,0);
    delay(200);
   
  
   
    drive.set(0,0);
    delay(500);
   
   
    if(centerir.getData()>20){
     do{drive.set(100,100);
     }while(centerir.getData()>20);
    }
   
  do{  drive.set(150,-150);
    delay(450);
  }while(centerir.getData()<20);
    follow = true;
   
  
   
   
  
   if(follow == true){
         
          wallFollowing();
   }
  }
 





 
  void find_flame()
{
 
 
  
    
      
}

void wallFollowing(){
 
  // IR sensor returns distance in centimeters
    int val = leftir.getData();
   
   
 
  
   
if(centerir.getData()>20){

    if(val < 14){
        // if we are way too close, turn away fast
        drive.set(225,20);
    }else if(val < 19){
        // if we are too close, drift away
        drive.set(200,180);
    }else if(val > 25){
        // too far away, turn towards wall more
        drive.set(20,200);
    }else{
        // default, drift towards wall
        drive.set(180,225);
    }
   
}
if(centerir.getData()<20){
  do{
      drive.set(150,-150);
  }while(centerir.getData()<20);
}


 
  //stops here before leaving the room, imediately after the turn, write wall follopwing code
 


 wallFollowing();
}

This code works very consistently, I have also written flame detection code below, which i have tested and it works

#include <EncodersAB.h>
#include <Motors.h>
#include <SharpIR.h>


Motors drive = Motors(); //drive motors
SharpIR rightir = SharpIR(GP2D12,3);
SharpIR leftir = SharpIR(GP2D12,1);
SharpIR centerir = SharpIR(GP2D12,2);

int r=analogRead(5);
int l=analogRead(4);




void setup()
{
 
 
  Serial.begin(38400);
  delay(2000);
  int r=analogRead(5);
  int l=analogRead(4);
 
  pinMode(12,OUTPUT);
 
}

void loop()
{
int r= analogRead(5);

int l= analogRead(4);


 if(r>l){
    drive.set(100,150);
  }else if(l>r){
    drive.set(150,100);
  }


if(r<50&&l<50){
  drive.set(0,0);

 
}


 
 
    
}

void fan_time()
{
  drive.set(0,0);
 
  digitalWrite(12,HIGH);
}

Now the problem im having is that i cannot combine the two, I am using two photosensors to detect the flame (No UV tron) - Basically i need it to check for the flame while doing the roomsearchright function and the wall following function, I have tried and have had no luck so far, can anyone help me combine these two bits of code?

void setup()
{
 
  // This version separates the initial turn and then allows the bot to detect a room from the forward function
  Serial.begin(38400);
  delay(250);
  int r=analogRead(5);
  int l=analogRead(4);
  int c = analogRead(0);

What's the point of the analogReads?

Probably the best way of merging the two is to first get rid of all the long delays in your first sketch.

have a look at the Blink Without Delay example for clues on how to do this.

Theyre used to input the data from the photosensors, i need to get them to interrupt the code somehow and call a function that contain the second code that i posted

Theyre used to input the data from the photosensors,

But then you throw away the result, so they're a waste of code.
You do this in both sketches.

Any time you have a delay, unless you have an interrupt, you're not doing anything else, like looking for a flame.
Even if you do have an interrupt, you may not be able to take the action you want, so IMHO you need to break your code down into a set of states or phases, possibly with sub-states.

Another advantage of Awol's approach is that you can do your wall detection continuously too, so you can make minor steering corrections when necessary, rather than relying on your robot's ability to drive straight during your delay() statements as you do now.

is it possible to put if statements after each comman?

if(analogRead(5)<800||analogRead(4)<800){
find_flame();
}

this would call a function that would find the flame, would this work?

i would place this statement between each movement command in the roomsearchright() method
and inside the if statements in the wallFollowing() function

You could do it that way, but it would be quite tedious.

Think of your program as a number of differet states: turning left, turning right, going forward, looking for the donut shop...

You stay in a state for quite a while, but changing state is relatively quick.
More importantly, the decisions are made in pretty much one place.
That's a state machine.

Another issue with that method is that you run the risk of missing the flame altogether if your robot is moving fast enough. Is 1.5 seconds long enough for it to move past it? Ideally, you want to be running through loop with no delays at all. That way you can be reading all your sensors very frequently and act on what they see. It'll also make your code simpler, although you'll have to change it quite a bit to get there. That's what a state machine will get you.

Here's a simple example of the state machine approach - it shows a delay based sketch turned into a state based version: http://arduino.cc/forum/index.php/topic,55879.0.html The state variable, step, and timer functions take care of the motors, which means that any sensor calls can be handled with one invocation per sensor each time loop is run.