switching between 2 loops possible?

PaulS
Not every body are pro programmers. If you want some thing or if you wonder about something just ask. This is just a hobby for me that i havent been doing for almost a year because of to much work.

Auto format for PaulS

#include <Servo.h>
//*************************************
int IN1=7;//
int IN2=5;//
int IN3=4;//
int IN4=2;//

//*************************************
int inputPin =13 ; // 
int outputPin =12; // 
int Fspeedd = 0; // 
int Rspeedd = 0; // 
int Lspeedd = 0; // 
int directionn = 0; // 
Servo myservo; // 
int delay_time = 250; // 
int Fgo = 8; // 
int Rgo = 6; // 
int Lgo = 4; // 
int Bgo = 2; // 
//**************************************

int incomingByte = 0;

void setup(){

  Serial.begin(9600);

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

  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  //***************************
  pinMode(inputPin, INPUT); // 
  pinMode(outputPin, OUTPUT); // 
  myservo.attach(9); // 
  //***************************

  // turn motors Off by default
  M_stop();

  delay(500);

}

void loop(){

  if(Serial.read())
  {
    int c = Serial.read();
    if (c == 'm') //first loop for manual steering
    {
      Serial.println("Manual");
      // check for serial data
      if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();
      

        // say what you got:
        Serial.print("I received: ");
        Serial.println(incomingByte);
        // delay 10 milliseconds to allow serial update time
        delay(10);

        if  (incomingByte == 105){
          M_forward();
          delay(25);
        }

        else if (incomingByte == 106){
          M_left();
          delay(25);
        }

        else if (incomingByte == 108){
          M_right();
          delay(25);
        }

        else if (incomingByte == 107){
          M_reverse();
          delay(25);
        }

        else {
          M_stop();
          Serial.println("stop");
        }
      }
      else {
        M_stop();
        Serial.println("stop2");

      }
      delay(25);
    }
    else if(c == 'n'){//second loop for auto run
      Serial.println("Detection");
      detection();
    }
  }
}
void M_forward(){
  digitalWrite(IN1,LOW);//
  digitalWrite(IN2,HIGH);
  digitalWrite(IN3,HIGH);//
  digitalWrite(IN4,LOW);

}

void M_reverse(){
  digitalWrite(IN1,HIGH);//
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);//
  digitalWrite(IN4,HIGH);
}

void M_stop(){
  digitalWrite(IN1,HIGH);//
  digitalWrite(IN2,HIGH);
  digitalWrite(IN3,HIGH);//
  digitalWrite(IN4,HIGH);
}

void M_right() //
{
  digitalWrite(IN1,LOW);//
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,HIGH);//
  digitalWrite(IN4,LOW);

} 

void M_left() //
{
  digitalWrite(IN1,LOW);//
  digitalWrite(IN2,HIGH);
  digitalWrite(IN3,LOW);//
  digitalWrite(IN4,LOW);

}

void detection() //
{ 
  int delay_time = 250; // 
  ask_pin_F(); // 

  if(Fspeedd < 10) // 
  {
    M_stop(); //  
    M_reverse; // 
  }
  if(Fspeedd < 25) // 
  {
    M_stop(); // 
    ask_pin_L(); // 
    delay(delay_time); // 
    ask_pin_R(); //
    delay(delay_time); // 

    if(Lspeedd > Rspeedd) //
    {
      directionn = Lgo; //
    }

    if(Lspeedd <= Rspeedd) //
    {
      directionn = Rgo; //
    } 

    if (Lspeedd < 15 && Rspeedd < 15) //
    {
      directionn = Bgo; //
    } 
  }
  else //
  {
    directionn = Fgo; //
  }
}   
//*********************************************************************************
void ask_pin_F() // 
{
  myservo.write(90);
  digitalWrite(outputPin, LOW); // 
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH); // 
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW); // 
  float Fdistance = pulseIn(inputPin, HIGH); // 
  Fdistance= Fdistance/5.8/10; // 
  Serial.print("F distance:"); //
  Serial.println(Fdistance); //
  Fspeedd = Fdistance; // 
} 
//********************************************************************************
void ask_pin_L() // 
{
  myservo.write(177);
  delay(delay_time);
  digitalWrite(outputPin, LOW); // 
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH); // 
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW); // 
  float Ldistance = pulseIn(inputPin, HIGH); // 
  Ldistance= Ldistance/5.8/10; // 
  Serial.print("L distance:"); //
  Serial.println(Ldistance); //
  Lspeedd = Ldistance; // 
} 
//******************************************************************************
void ask_pin_R() // 
{
  myservo.write(5);
  delay(delay_time);
  digitalWrite(outputPin, LOW); // 
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH); // 
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW); // 
  float Rdistance = pulseIn(inputPin, HIGH); // 
  Rdistance= Rdistance/5.8/10; // 
  Serial.print("R distance:"); //
  Serial.println(Rdistance); //
  Rspeedd = Rdistance; // 
}