Toggle between two void functions

Hi everyone
I am currently building a small robot with bluetooth control. It also avoids obstacles using its sonar sensor. With the my app I have built to control the robot it has a checkbox for automatic mode this enables the robot to run about with no user interaction. To implement this I have made the app send the number 11 to the Arduino when the box is checked and to send 12 to the Arduino when it is not checked. This works successfully as the Arduino receives the numbers.

I have created to void functions one named automatic() and one named manual() these both work individually well. When I run the app and check the box, the app sends a 11 to the Arduino which then runs automatic mode. Then I send a 11 by unchecking the box and the automatic loop continues to run which is not what I want.

I would like to be able to toggle between these two loops. Is there a way to stop and start these loops externally?

Here is my full code :

#include <AFMotor.h>
AF_DCMotor Motor1(1);
AF_DCMotor Motor2(2);
#define trigPin 12 // define the pins of your sensor
#define echoPin 13 
#define headlightsOne 4
#define headlightsTwo 5
#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
boolean toggleMode = false;
void setup()
{
  Motor1.setSpeed(255);
  Motor2.setSpeed(255);
  Serial.begin(9600);
  pinMode(headlightsOne, OUTPUT);
  pinMode(headlightsTwo, OUTPUT);
  digitalWrite(headlightsOne, HIGH);
  digitalWrite(headlightsTwo, HIGH);
  toggleMode = false;
  pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  dht.begin();
  
  
  
}



 void transmit(){ //transmitting mode
        float t_c = dht.readTemperature();
        Serial.print(t_c);
        delay(1000);
        
}
void automatic(){
  long duration, distance; // start the scan
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ 
  {   
    Motor1.run(FORWARD);  // Turn as long as there's an obstacle ahead.
    Motor2.run (BACKWARD);
  }
  
else {
   delay (15);
   Motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward! 
   Motor2.run(FORWARD);  
  } 
  if (toggleMode == true){
    automatic();
  }
}

void manual() {
        int c = Serial.read();
        
        if (c == 49) {
          Motor1.run(FORWARD);
          Motor2.run(FORWARD);     
          }
          else if (c == 50) {
            Motor1.run(RELEASE);
            Motor2.run(RELEASE);
          }
          
          else if (c == 51) {
            Motor1.run(BACKWARD);
            Motor2.run(BACKWARD);
          }
          else if (c == 52) {
            Motor1.run(RELEASE);
            Motor2.run(RELEASE);
          }
          else if (c == 53) {
            Motor1.run(BACKWARD);
            Motor2.run(FORWARD);
          } 
          else if (c == 54) {
            Motor1.run(RELEASE);
            Motor2.run(RELEASE);
          }
          else if (c == 55) {
            Motor1.run(FORWARD);
            Motor2.run(BACKWARD);
          }
          else if (c == 56) {
            Motor1.run(RELEASE);
            Motor2.run(RELEASE);
          }
          else if (c == 9){
            digitalWrite(headlightsOne, HIGH);
            digitalWrite(headlightsTwo, HIGH);
           
          }
           else if (c == 10){
            digitalWrite(headlightsOne, LOW);
            digitalWrite(headlightsTwo, LOW);
           
          }
}

void loop()
{ 

transmit(); //runs thorughout


  if (Serial.available() > 0) {
    int c = Serial.read();
    if (c == 11){ //toggle between  manual and automatic
      automatic();
    }
    if (c == 12){
      manual();
    }       

  
  }
}

If you have any ideas they would be greatly appreciated
Thanks ;D

I presume you only send an 11 or 12 occasionally - when you want the system to change mode.

You need a variable to record the state of the system and when you receive a value that should set the state. For example ...

    int c = Serial.read();
    if (c == 11){ //toggle between  manual and automatic
      systemState = 'A';
    }
    if (c == 12){
      systemState = 'M';
    }

The the code in loop() could include

if (systemState == 'A') {
   automatic();
}
else {
   manual();
}

For a program like this you should not use delay() anywhere as it block other activities. Use millis() to manage timing without blocking as illustrated in Several Things at a Time

...R

Thanks Robin what sort of variable should I use would it be a Char? Also I would calling another loop cause the other to stop functioning?
Thanks again

I have just tested your code and it gives the same result as mine I think the problem is that the loop won’t stop when the other loop is called.
do you have any ideas on how to do this?
Thanks

supernoob:
I have just tested your code and it gives the same result as mine

You need to post that program (complete program) so I can see exactly what you have done.

...R

Do you type 11 in as 2 ascii characters

char c = Serial.read();
    if (c == '1'){ //toggle between  manual and automatic
      automatic();
    }
    if (c == '2'){
      manual();
    }

Try sending 1 and 2 with the change I made above to see if it switches

Wondering the same thing as zhomeslice

in your code you have

    int c = Serial.read();
    if (c == 11){ //toggle between  manual and automatic
      automatic();
    }
    if (c == 12){ 
     ...

Serial.read only reads ONE byte - so are you really sending the character 11 (vertical Tab) or 12 (form feed)??

if you are sending the number 11 from the keyboard as two characters a '1' followed by another '1' then your code above receives actually the ASCII representation of just the first character '1' which means c will get the value 49 (ASCII value of the char '1')