switching between 2 loops possible?

Hi i got an robot project going the code works good for the funtions etc but i need to have 2 loops and to be able to switch between them. so i tried to do it with "while" and a pin if the loopbreaker pin 10 is high he should be at one loop and if its low he should switch to another but i think i got it wrong or some thing.

#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')
     {
   Serial.println("Manual");
// check for serial data
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// if available, blink LED and print serial data received.

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

}
}
else if (c == 'n'){//second loop
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; // 
}

I don't know exactly what you mean by different loops.

Certainly you can use a switch position to signal which of two functions should be used. You should use an IF statement to select because then the ELSE part can be the other function.

And if several functions included an IF test they could all respond differently depending on the switch.

Or you could create a couple of alternative master functions each of which calls several subsidiary functions.

It all depends what you want to achieve.

...R

hi tnx for your answer. This code is for my robot it makes me able to control it from my phone true bluetooth. But i had an ide of making a funktion that makes it go for it self via a sonar sensor. But the problem is that the first funktion where i control manualy is looking after a command from the bluetooth or else it continues spaming M_stop(it the comand for stoping the motors) which is interfering with my code for the detection with sonar sensor. Thats why i was wondering if it was possible to switch between 2 loops. or there is possible another solution to my problem that i havent tought of.

Yes you switch with an if statement. By loops you don't mean the loop function but the way your loop function runs.
So at the start you have an if the dodo the stuff for loop one and the else of that will be the stuff for loop two.

ok i put in an if and else statment but now the funktions in the if and else statement isent looping. any ide?

#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')
     {
   Serial.println("Manual");
// check for serial data
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// if available, blink LED and print serial data received.

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

}
}
else if (c == 'n'){//second loop
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; // 
}
if(Serial.read())
   {
     int c = Serial.read();
     if (c == 'm')
     {
   Serial.println("Manual");
// check for serial data
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// if available, blink LED and print serial data received.

If you can't be bothered to indent your code properly, and can't be bothered to use Tools + Auto Format, I can't be bothered trying to make heads or tails of your mess.

Grumpy_Mike:
Yes you switch with an if statement. By loops you don't mean the loop function but the way your loop function runs.
So at the start you have an if the dodo the stuff for loop one and the else of that will be the stuff for loop two.

This is nearly more incomprehensible than the OPs. :slight_smile: :slight_smile: :slight_smile:

...R

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; // 
}

If you want some thing or if you wonder about something just ask.

Why should I have to ask you to post code that isn't a incomprehensible mess? You'd have a much easier time writing the code is you paid attention to indenting as you went.

  if(Serial.read())
  {

If it what? This is completely inaccurate code, at the beginning of loop(). Since reading and discarding the first byte available, if any is available to read, is not a bright thing to do, I can't see that the code in the of block is going to accomplish anything useful. So, I didn't read it.

if(Serial.read())
  {

That was a code left over from old sketch. Any other thing that makes you stop reading the code?

That was a code left over from old sketch.

It's still in your code. It's complete nonsense. Isn't that a good enough reason to stop reading?

Why are you arguing have i offended you in some way?

Why are you arguing have i offended you in some way?

I'm not arguing. Yes, you are. No, I'm not.

The code is ugly. Life's too short to read ugly code. Explain what you are trying to do. Then, we can skip the crappy looking code, and simply explain what you need to do.

This code here is for my robot car i can control it with my simple app on the phone. The first code was almost the same as this one only diffrance is the other one was so i could switch between this funtion i explained above which is manual controling the robot and the new code was so i could use my sonar sensor so it could drive around for it self which was supposed to be automode.

int IN1=7;//
int IN2=5;//
int IN3=4;//
int IN4=2;//

int incomingByte = 0;

void setup(){

  Serial.begin(9600);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);

  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);

  // turn motors Off by default
  M_stop();

  delay(500);

}

void loop(){
  // 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("stop");

  }
}


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

}

The new ugly code

#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(){

  
    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; // 
}

void detection() is for the sonar function if you where wondering.

Look at the original code. Before it reads any serial data, it checks to see that there is something to read.

Look at the new code. It assumes that there is serial data to read. That is a piss poor assumption. 99.9% of the time, it is likely to be wrong.

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

Rubbish. By the time you get finished sending out 13 bytes for every one you read, the serial buffer is going to have overflowed. Get rid of the stupid delay().

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

What do you need to send to receive a 105?

        if  (incomingByte == 'i'){
          M_forward();
        }

What do you need to send to receive a 'i'?

Why write cryptic code? Why are you welded to the stupid delay() function? If you don't want to do something different for a while, DON'T SEND ANYTHING!

Put some responsibility for proper control on the user. Quit overusing delay().

      delay(25);

Another useless delay().

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; // 
}

What is with all the annoying empty comments?

I give up. I'm going to quit before one of us gets too irritated.

NWM i got the ugly code to work. You shouldent have bothered starting arguing but i guess it was your lose of time reading my ugly code and arguing about nothing.

@xenon, I can't help feeling that you have missed the important reason for you to take the trouble to de-uglify your code.

Neat code makes it much easier for people here to give you assistance. Because it saves us time we are more likely to help. And that is good for you.

...R

xzenon:
NWM i got the ugly code to work. You shouldent have bothered starting arguing but i guess it was your lose of time reading my ugly code and arguing about nothing.

Welcome to Arduino again, xzenon. Please know you have just been subjected to some tough love, and you are likely to be a much better programmer for the baptism by fire. The reformatting and scrutiny may have given you the drive to fix your own mistakes. I just wanted to thank you for exhibiting a positive attitude through PaulS's assistance.

I usually approach such dialog differently, but you need to understand (or it is my hope that you will come to understand) that a small number of members assist a very large population of users across multiple forum groups. I personally, spend 8 to 10 hours a week minimum and I have to be selective because some of the need is simply not documented well enough to enter a dialog with the Op... It would be like quicksand! Anyway, thus is not an apology but a request to stay the course and to give-back as you become more comfortable with your knowledge - we can always use good, clever folks. In the mean time, it would be great if you would post your working code for thee benefit of those that may be searching the forum for a similar problem.

At the top of each forum group are the "stickies" (push pin icon) on the responsibilities that the Op needs to perform. It is regrettable but often these are not read or are read after-the-fact. They change from time to time, I even review them myself after several years of being around.

Ray

Interesting read

Baptism by fire is right.

PaulS again we thank for your titbits of information.. Probably best if he gets himself a beer. :slight_smile: