How to exit one loop function and go to another-firefighting robot

I am in the process of programming a fire fighting robot. I am using arduino mega, motor driver, microphone, buzzer, ultrasonic sensor, servo, flame sensor, fan module.

The issue I have now is that when the arduino detects a sound loud enough, it begins the "auto_avoidance’ loop. My question is, how do I exit that loop once the flame sensor detects the flame?

Realistically, I would like for the robot to run the auto_avoidance then when the flame is detected, the robot will stop in its tracks, turn on the fan, and then turn left and right slowly to insure that the flame is extinguished.

#include <Servo.h>
#define IN1  8   //K1、K2 motor direction
#define IN2  9     //K1、K2 motor direction
#define IN3  10    //K3、K4 motor direction
#define IN4  12   //K3、K4 motor direction
#define ENA  5    // Needs to be a PWM pin to be able to control motor speed ENA
#define ENB  6    // Needs to be a PWM pin to be able to control motor speed ENB
#define LED1 2  //left led connect to D2
#define LED2 3  //right led connect to D3
#define SERVO     11  //servo connect to D11
#define echo    A3 // Ultrasonic Echo pin connect to A2
#define trig    A2  // Ultrasonic Trig pin connect to A3
#define mictrigger 4 //Digital Microphone Trigger
#define buzzer     2 //buzzer connect to D2
#define RSPEED   255  //right motor speed
#define LSPEED   255  //left motor speed
#define FlameDetect 13 //flame detect pin 13

#define fanpinpos  3 //fan turn on pin 3
#define fanpinneg 7

int  sensorDigitalPin = 4;    // Select the Arduino input pin to accept the Sound Sensor's digital output
int  digitalValue;            // Define variable to store the digital value coming from the Sound Sensor


enum DN
{
  START_AVOIDANCE,//start avoidance
  STOP_AVOIDANCE,//stop avoidance
  DEF
} Drive_Num = DEF;
int leftscanval, centerscanval, rightscanval, ldiagonalscanval, rdiagonalscanval;
const int distancelimit = 35; //distance limit for obstacles in front
const int sidedistancelimit = 18; //minimum distance in cm to obstacles at both sides (the car will allow a shorter distance sideways)
Servo head;

/*motor control*/
void go_ahead()//go ahead
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  //delay(t);
}
void go_back() //go back
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  //delay(t);
}
void go_stop() //stop
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}
void turn_left()//turn left
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  //delay(t);
}
void turn_right()//turn right
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  //delay(t);
}
/*set motor speed */
void set_motorspeed(int lspeed, int rspeed)
{
  analogWrite(ENA, lspeed);
  analogWrite(ENB, rspeed);
}
void buzz_on()   //open buzzer
{
  digitalWrite(buzzer, LOW);
}
void buzz_off()  //close buzzer
{
  digitalWrite(buzzer, HIGH);
}
void alarm() {
  buzz_on();
  delay(100);
  buzz_off();
}
/*******control led*******/
void open_led(int led_num)
{
  if (led_num == 1)  digitalWrite(LED1, LOW);
  else digitalWrite(LED2, LOW);
}
void close_led(char led_num)
{
  if (led_num == 1)  digitalWrite(LED1, HIGH);
  else digitalWrite(LED2, HIGH);
}
/*detection of ultrasonic distance*/
int watch() {
  long howfar;
  digitalWrite(trig, LOW);
  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  delayMicroseconds(15);
  digitalWrite(trig, LOW);
  howfar = pulseIn(echo, HIGH);
  howfar = howfar * 0.01657; //how far away is the object in cm
  Serial.println((int)howfar);
  return round(howfar);
}
/**************Microphone Frequency***************/

void Mic_Trig()
{
  Serial.begin(9600);               // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed
  pinMode(sensorDigitalPin, INPUT); // Define pin 7 as an input port, to accept digital input
  // Define LED13 as an output port, to indicate digital trigger reached
}

void loop() {

  digitalValue = digitalRead(sensorDigitalPin); // Read the value of the digital interface 7 assigned to digitalValue

  if (digitalValue == HIGH) // When the Sound Sensor sends signal, via voltage present, light LED13 (L)
    auto_avoidance();

  boolean stat = digitalRead(FlameDetect);//read the value of D13
  if (stat == LOW) {
       go_stop();
       turnonfan();
  }
  
    
  else
  turnofffan();


  


}


void turnonfan() {

  digitalWrite(fanpinpos, LOW); //turn on the fan
  digitalWrite(fanpinneg, HIGH); // turn on the fan
  delay(1000);
 
  
}

void turnofffan() {

  digitalWrite(fanpinpos, LOW);
  digitalWrite(fanpinneg, LOW);


}



void auto_avoidance() {
  head.write(90);
  delay(100);
  centerscanval = watch();
  if (centerscanval >= distancelimit) {
    set_motorspeed(LSPEED, RSPEED);
    go_ahead();
  }
  else {
    go_stop();
    alarm();
    head.write(120);
    delay(150);
    ldiagonalscanval = watch();

    head.write(180);
    delay(150);
    leftscanval = watch();

    head.write(90);
    delay(150);

    head.write(60);
    delay(150);
    rdiagonalscanval = watch();

    head.write(0);
    delay(150);
    rightscanval = watch();

    head.write(90);
    if (ldiagonalscanval >= sidedistancelimit && leftscanval >= sidedistancelimit) {
      set_motorspeed(LSPEED, RSPEED);
      go_back();
      delay(400);
      turn_left();
      delay(400);
    }
    else if (rdiagonalscanval >= sidedistancelimit && rightscanval >= sidedistancelimit) {
      set_motorspeed(LSPEED, RSPEED);
      go_back();
      delay(400);
      turn_right();
      delay(400);
    }
  }
}




void setup() {

  /****L298N****/
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  /****LED****/
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  close_led(1), close_led(2); //close led
  /*init HC-SR04*/
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trig, LOW);
  /*init buzzer*/
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer, HIGH);
  buzz_off();
  /*init servo*/
  head.attach(SERVO);
  head.write(85);
  /*init fan*/
  pinMode(13, INPUT);
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);

 
}

Embedded_ROBOT_Code.ino (5.63 KB)

A [u]while() loop[/u] or [u]do-while( loop)[/u] has a loop/exit condition. A for() loop does too, but it's not applicable here.

There is also a [u]break;[/u] command (normally used conditionally with an if-statement).

Or, in come cases you could use an interrupt.

You can't really do what you want as long as you have all your timing done with delay. If you're going to use delay then your device has to be a one track mind just follow the whole story from beginning to end sort of thing.

If you want to be able to respond to things while that other loop is running then you have to build it as a state machine so that when it gets called it gets in and out in a few milliseconds and lets the rest of the loop run. You don't have to stay in that function the whole time, just take the one step that needs to be taken at that moment and return. Use variables and millis() to keep up with what step needs to be taken at any given moment.

seems like auto_avoidance() is a sequencer, a state machine that steps thru a sequence of states in a specific order.

with some work, the sequence of operations, parameters and delays for auto_avoidance() could be captured in a table and a function called each iteration of loop() to step thru the table of operations until complete. there can be multiple tables with different sequences. there can be operations that conditionally select a new table, a new sequence or change the current index.

delays must be implemented with millis() so that the code return to loop() and only advances to the next step after the time has expired

by have such a mechanism, again called each iteration of loop(), other tests in loop() could abort the current sequence being follow, "stop in its tracks", and begin a new sequence.

i would think of loop() doing 2 things: monitoring sensors and perform some sequence.

of course a sequence can repeat itself by reseting the index back to zero

if done properly, you spend more time dealing with sequences that code