Exiting an "if" statement

Hi!
I am trying to make a a robot using the Arduino UNO, and it is programmed to communicate with a keypad app on my Android smartphone via bluetooth. Now when I press "a" on the keypad, it begins using a sonar sensor and runs autonomous. I want to be able to return to bluetooth communication after entering autonomous mode, but currently it stops communicating with the bluetooth after entering autonomous. Any ideas on how to repair this code?

Thanks!

#include <Servo.h>
#include <AFMotor.h>
#include <SoftwareSerial.h>
#define trigPin 17
#define echoPin 16
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
Servo servoMain; // Define our Servo
boolean sweep = true;
int pos = 0;

SoftwareSerial mySerial(17, 14); // TX, RX
String command = ""; // Stores response of bluetooth device

char val;
void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  Serial.println("Type AT commands!");
  // SoftwareSerial "com port" data rate. JY-MCU v1.03 defaults to 9600.
  mySerial.begin(9600);

  servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
  // Read device output if available.
  if (mySerial.available()) {
    val = mySerial.read();  
    Serial.println(val);     // read it and store it in 'val'

    if( val == 'C')              
    {
      motor1.setSpeed(0);
      motor2.setSpeed(0);
      motor1.run(FORWARD);
      motor2.run(FORWARD);
    } 
    if( val == 'L' )               
    {
      motor1.setSpeed(225);
      motor2.setSpeed(200);
      motor1.run(BACKWARD);
      motor2.run(FORWARD);
      delay(50);
    } 
    if( val == 'D' )               
    {
      motor1.setSpeed(200);
      motor2.setSpeed(200);
      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      delay(100);
    } 
    if( val == 'R' )              
    {
      motor1.setSpeed(225);
      motor2.setSpeed(200);
      motor1.run(FORWARD);
      motor2.run(BACKWARD);
      delay(50);
    } 
    if( val == 'U' )              
    {
      motor1.setSpeed(200);
      motor2.setSpeed(200);
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      delay(100);
    } 
    if( val == 'a' )              
    {      
      val = mySerial.read();  
       Serial.println(val);     // read it and store it in 'val' 

        int duration, distance;
        pinMode(trigPin, OUTPUT);
        pinMode(echoPin, INPUT);

        if (sweep == true)
        {
          pos = 0;
          servoMain.write(pos); // Turn Servo Left to 45 degrees
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;
          if (distance >= 200 || distance <= 0){
            Serial.println("Out of range");
          }
          if (sweep == false) 
          {
            Serial.print(distance);
            Serial.println(" cm");
          }
          if ( 10 >= distance >= 1)
          {
            motor1.setSpeed(200);
            motor2.setSpeed(200);
            motor1.run(BACKWARD);
            delay(300); 
            motor1.setSpeed(100);
            motor2.setSpeed(100);
            motor1.run(FORWARD);  
          }
          delay(350); // Wait 1 second
          pos = 45;
          servoMain.write(pos); // Turn Servo Left to 45 degrees
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;
          if (distance >= 200 || distance <= 0){
            Serial.println("Out of range");
          }
          if (199 >= distance >= 1)
          {
            Serial.print(distance);
            Serial.println(" cm");
          }
          if ( 10 >= distance >= 1)
          {
            motor1.setSpeed(200);
            motor2.setSpeed(200);
            motor1.run(BACKWARD);
            delay(300); 
            motor1.setSpeed(100);
            motor2.setSpeed(100);
            motor1.run(FORWARD);  
          }
          delay(350); // Wait 1 second
          pos = 135;
          servoMain.write(pos); // Turn Servo Left to 0 degrees
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;
          if (distance >= 200 || distance <= 0){
            Serial.println("Out of range");
          }
          else {
            Serial.print(distance);
            Serial.println(" cm");
          }
          if ( 10 >= distance >= 1)
          {
            motor1.setSpeed(200);
            motor2.setSpeed(200);
            motor2.run(BACKWARD); 
            delay(300); 
            motor2.setSpeed(100);
            motor1.setSpeed(100);
            motor2.run(FORWARD);
          }
          delay(350); // Wait 1 second
          pos = 180;
          servoMain.write(pos); // Turn Servo back to center position (90 degrees)
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;
          if (distance >= 200 || distance <= 0){
            Serial.println("Out of range");
          }
          else {
            Serial.print(distance);
            Serial.println(" cm");
          }
          if ( 10 >= distance >= 1)
          {
            motor1.setSpeed(200);
            motor2.setSpeed(200);
            motor2.run(BACKWARD);
            delay(300); 
            motor1.setSpeed(100);
            motor2.setSpeed(100); 
            motor2.run(FORWARD);
          }
          delay(350); // Wait 1 second
          pos = 135;
          servoMain.write(pos); // Turn Servo Right to 135 degrees
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;
          if (distance >= 200 || distance <= 0){
            Serial.println("Out of range");
          }
          else {
            Serial.print(distance);
            Serial.println(" cm");
          }
          if ( 10 >= distance >= 1)
          {
            motor1.setSpeed(200);
            motor2.setSpeed(200);
            motor2.run(BACKWARD);
            delay(300);  
            motor1.setSpeed(100);
            motor2.setSpeed(100);
            motor2.run(FORWARD);
          }
          delay(350); // Wait 1 second
          pos = 45;
          servoMain.write(pos); // Turn Servo Right to 180 degrees
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;
          if (distance >= 200 || distance <= 0){
            Serial.println("Out of range");
          }
          else {
            Serial.print(distance);
            Serial.println(" cm");
          }
          if ( 10 >= distance >= 1)
          {
            motor1.setSpeed(200);
            motor2.setSpeed(200);
            motor1.run(BACKWARD);
            delay(300); 
            motor1.setSpeed(100);
            motor2.setSpeed(100);
            motor1.run(FORWARD);
          }
          delay(350); // Wait 1 second
        }
  
    }
    if( val == 'b' )              
    {

    }
    if( val == 'c' )              
    {

    }
    if( val == 'd' )              
    {

    }
    if( val == 'e' )              
    {

    }
    if( val == 'f' )              
    {

    }
    if( val == 'g' )              
    {

    }
    if( val == 'h' )              
    {

    }
  }
}
void loop()
{
...
      void loop() {

What exactly are you trying to accomplish here, by nesting a function implementation within a function implementation with the same name. Does this even compile?

My apologies, I was attempting something using the two void loops and forgot to remove them before posting here. I have updated the code above to a working version. I am trying to find a way to quit the autonomous code and return to bluetooth communication, but currently it doesn't respond to any bluetooth command after entering autonomous.

We'll start of with some obvious issues first:

kumararidam:
using the two void loops and forgot

There is no such thing as a "void loop". You have a loop function that has a void return type. You call them banks, not cash banks.

delay(350); // Wait 1 second

It most certainly is not going to wait for 1 second.

 if (mySerial.available()) {
    val = mySerial.read();  
    ...
    if( val == 'a' )              
    {      
      val = mySerial.read();

See if there is at least 1 byte to read. Read it. If it matches the value, read another one. How do you know there is another one to read? With all the delays in your code, it may not appear to make a a difference, but it's poor coding to assume there are two bytes to read when you're only guaranteed to have 1, and it may come back to bite you in the ass in the future.

Now to the actual question, When it completes its "autonomous" section of code, what does it do? Does it repeat? Does it sit there and do nothing?

I can't compile the program, even if I include the header files. The first obvious problem is that you have a second loop() function defined within the first loop() function. I think the compiler will throw up on this.

In over 40 years of using C, this is the first time I've seen:

if ( 10 >= distance >= 1)

in an expression. Anytime you have to stare at a single line of code for over a minute to try and decide what it does, especially one this "simple", you need to rewrite it for code maintenance reasons. I know what it does, but I'm wondering if that matches the intent of the writer.

          digitalWrite(trigPin, HIGH);
          delayMicroseconds(1000);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1;

How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?
How many times do you think you need to type this code?

if ( 10 >= distance >= 1)

I can remember making this mistake when I wrote my first BASIC programs, way back when a TRS80 was high tech, because this is the mathematical representation of the intent. In code you write each comparison is a single comparison between values.

if ((10 >= distance ) && (distance >= 1))

or, as a style thing,

if ((distance >= 1) && (distance <= 10))

Thank-you for your responses. I am currently a high-school student programming an Arduino for the first time, so please bear with me as I try and learn from my mistakes. :slight_smile:

I will correct the "distance" line to rewrite the math equation properly. But my main question is how do I quit the "if" statement for "val == 'a'"? At the end of it, the program keeps looping so the robot is consistently moving in autonomous mode and it doesn't respond to my commands anymore.

how do I quit the "if" statement for "val == 'a'"?

At beginning or the end of the (val == 'a') IF statement, add "val = ' '; " ,this will clear val, so now val does not equal 'a' anymore.

If you are rewriting code, then consider using a function for this block of code which is repeated many times:

      motor1.setSpeed(200);
      motor2.setSpeed(200);
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      delay(100);

becomes

setMotors(FORWARD, 200, FORWARD, 200, 100);

if you define a function

void setMotors(uint8_t dirM1, uint16_t spdM1, uint8_t dirM2, uint16_t spdM2, uint16_t waitDelay)
{
      motor1.setSpeed(spdM1);
      motor2.setSpeed(spdM2);
      motor1.run(dirM1);
      motor2.run(dirM2);
      delay(waitDelay);
}

I would also say that at some point you will need to get rid of the delays in you code for this software to run properly. Look at the Blinkwithoutdelay example in the IDE for how you can do this.

Thank-you for your responses. I tried to clear val at the end of the code and the program doesn't compile due to an "empty character set". I then set val to 0 at the end...that compiled but changed nothing in the program's functioning. Later today I will simplify the code by re-writing as recommended above.

For now, I still need to solve the if statement's looping issue. Here are the two original codes that I am trying to modify and merge together and putting them together is what causes the if statement's looping issue:

Bluetooth code:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 8`); // RX, TX
String command = ""; // Stores response of bluetooth device

int left1 = 13;
int left2 = 12;
int right1 = 11;
int right2 = 10;
int testLed = 3;
int blueValue=0;

char val;
void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  Serial.println("Type AT commands!");
  // SoftwareSerial "com port" data rate. JY-MCU v1.03 defaults to 9600.
  mySerial.begin(9600);
  pinMode(left1,OUTPUT);
  pinMode(left2,OUTPUT);  
  pinMode(right1,OUTPUT);  
  pinMode(right2,OUTPUT);
  pinMode(testLed,OUTPUT);
}

void loop()
{
  // Read device output if available.
  if (mySerial.available()) {
    val = mySerial.read();  
    Serial.println(val);     // read it and store it in 'val'

    if( val == 'C')              
    {
      digitalWrite(left1, LOW);   
      digitalWrite(left2, LOW);    
      digitalWrite(right1, LOW);   
      digitalWrite(right2, LOW);
      delay(100);    
    } 
    else if( val == 'L' )               
    {
      digitalWrite(left1, HIGH);   
      digitalWrite(left2, LOW);    
      digitalWrite(right1, LOW);   
      digitalWrite(right2, HIGH);    
      delay(100);
    } 
    else if( val == 'D' )               
    {
      digitalWrite(left1, LOW);   
      digitalWrite(left2, HIGH);    
      digitalWrite(right1, HIGH);   
      digitalWrite(right2, LOW);    
      delay(100);
    } 
    else if( val == 'R' )              
    {
      digitalWrite(left1, HIGH);   
      digitalWrite(left2, LOW);    
      digitalWrite(right1, LOW);   
      digitalWrite(right2, HIGH);    
      delay(100);
    } 

    else if( val == 'U' )              
    {
  digitalWrite(left1, HIGH);   
  digitalWrite(left2, LOW);    
  digitalWrite(right1, LOW);   
  digitalWrite(right2, HIGH);    
  delay(100);
    } 
     else if( val == 'a' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
     else if( val == 'b' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
     else if( val == 'c' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
    else if( val == 'd' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
     else if( val == 'e' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
     else if( val == 'f' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
     else if( val == 'g' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
     else if( val == 'h' )              
    {
 digitalWrite(testLed, HIGH);
      delay(50);
      digitalWrite(testLed, LOW);
      delay(50);
    }
  }
}

Autonomous robot code:

#include <Servo.h>
#include <AFMotor.h>
#define trigPin 17
#define echoPin 16
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
Servo servoMain; // Define our Servo
boolean sweep = true;
int pos = 0;


void setup()
{
servoMain.attach(10); // servo on digital pin 10
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  motor1.setSpeed(100);   
  motor2.setSpeed(100);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
}


void loop()
{
 int duration, distance;
 
 if (sweep == true)
{
  pos = 0;
  servoMain.write(pos); // Turn Servo Left to 45 degrees
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
  Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  if ( 10 >= distance >= 1)
  {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor1.run(BACKWARD);
  delay(300); 
  motor1.setSpeed(100);
  motor2.setSpeed(100);
  motor1.run(FORWARD);  
  }
  delay(350);
  pos = 45;
  servoMain.write(pos); // Turn Servo Left to 45 degrees
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
  Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  if ( 10 >= distance >= 1)
{
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor1.run(BACKWARD);
  delay(300); 
  motor1.setSpeed(100);
  motor2.setSpeed(100);
  motor1.run(FORWARD);  
  }
  delay(350);
  pos = 135;
  servoMain.write(pos); // Turn Servo Left to 0 degrees
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
  Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  if ( 10 >= distance >= 1)
  {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor2.run(BACKWARD); 
  delay(300); 
  motor2.setSpeed(100);
  motor1.setSpeed(100);
  motor2.run(FORWARD);
  }
  delay(350);
  pos = 180;
  servoMain.write(pos); // Turn Servo back to center position (90 degrees)
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
  Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  if ( 10 >= distance >= 1)
  {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor2.run(BACKWARD);
  delay(300); 
  motor1.setSpeed(100);
  motor2.setSpeed(100); 
  motor2.run(FORWARD);
  }
  delay(350); 
  pos = 135;
  servoMain.write(pos); // Turn Servo Right to 135 degrees
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
  Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  if ( 10 >= distance >= 1)
  {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor2.run(BACKWARD);
  delay(300);  
  motor1.setSpeed(100);
  motor2.setSpeed(100);
  motor2.run(FORWARD);
  }
  delay(350);
  pos = 45;
  servoMain.write(pos); // Turn Servo Right to 180 degrees
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
  Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  if ( 10 >= distance >= 1)
  {
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor1.run(BACKWARD);
  delay(300); 
  motor1.setSpeed(100);
  motor2.setSpeed(100);
  motor1.run(FORWARD);
  }
  delay(350);

}
}

I tried to clear val at the end of the code and the program doesn't compile due to an "empty character set".

Single quotes need a space inbetween them, otherwise you will get that error. val = '_'; you need to set it to something your not looking for, after you do it.

Now using IF/ELSE statements are fine, but the better way would be to use case statements.