Switching between autonomous and manually controled

Hello guys, i’m doing this project as the final one of my highscool and i’m having a bit of a problem here. I made a car and i need to switch between auto-pilot and manual control (from a remote control or a smartphone) and i can’t do it because there is a bug on the autopilot ( the program need to loop and it doesn’t, it runs very fast and the car only goes backwards). Can anyone help me asap?
This is the program:
#include <Servo.h> //servo library
#include <IRremote.h>

Servo myservo; // create servo object to control servo

int Echo = A4;
int Trig = A5;

/define channel enable output pins/
#define ENA 5 // Left wheel speed
#define ENB 6 // Right wheel speed
/define logic control output pins/
#define IN1 7 // Left wheel forward
#define IN2 8 // Left wheel reverse
#define IN3 9 // Right wheel reverse
#define IN4 11 // Right wheel forward
#define carSpeed 150 // initial speed of car >=0 to <=255
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
///////////////////////////////////////////////////////////////
#define F 16736925 // FORWARD
#define B 16754775 // BACK
#define L 16720605 // LEFT
#define R 16761405 // RIGHT
#define S 16712445 // STOP
#define UNKNOWN_F 5316027 // FORWARD
#define UNKNOWN_B 2747854299 // BACK
#define UNKNOWN_L 1386468383 // LEFT
#define UNKNOWN_R 553536955 // RIGHT
#define UNKNOWN_S 3622325019 // STOP
#define RECV_PIN 12
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long val;
unsigned long preMillis;

int led = 13 ;
int btn = 2;
int leitura;
void forward(){
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println(“Forward”);
}

void back() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println(“Back”);
}

void left() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println(“Left”);
}

void right() {
analogWrite(ENA, carSpeed);
analogWrite(ENB, carSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println(“Right”);
}

void stop() {
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println(“Stop!”);
}

//Ultrasonic distance measurement Sub function
int Distance_test() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance / 58;
return (int)Fdistance;
}

void setup() {

myservo.attach(3); // attach servo on pin 3 to servo object

Serial.begin(9600);

pinMode(led, OUTPUT);
pinMode(btn,INPUT);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
stop();
irrecv.enableIRIn();
leitura= digitalRead(btn);
}

void loop() {
if (leitura == HIGH) {
digitalWrite(led, HIGH);
myservo.write(90); //setservo position according to scaled value
delay(500);
middleDistance = Distance_test();

if(middleDistance <= 20) {
stop();
delay(500);
myservo.write(10);
delay(1000);
rightDistance = Distance_test();

delay(500);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
leftDistance = Distance_test();

delay(500);
myservo.write(90);
delay(1000);
if(rightDistance > leftDistance) {
right();
delay(360);
}
else if(rightDistance < leftDistance) {
left();
delay(360);
}
else if((rightDistance <= 20) || (leftDistance <= 20)) {
back();
delay(180);
}
else {
forward();
}
}
else {
forward();
}
}
else if (leitura == LOW){
digitalWrite (led, LOW);
if (irrecv.decode(&results)){
preMillis = millis();
val = results.value;
Serial.println(val);
irrecv.resume();
switch(val){
case F:
case UNKNOWN_F: forward(); break;
case B:
case UNKNOWN_B: back(); break;
case L:
case UNKNOWN_L: left(); break;
case R:
case UNKNOWN_R: right();break;
case S:
case UNKNOWN_S: stop(); break;
default: break;
}
}
else{
if(millis() - preMillis > 500){
stop();
preMillis = millis();
}
}
}
}

As i am testing i think this is impossible because it would have to be 2 void loops running at the same time, and that's impossible, guess i'll have to switch Arduino's during the presentation of the project

davidsr2001: As i am testing i think this is impossible because it would have to be 2 void loops running at the same time, and that's impossible, guess i'll have to switch Arduino's during the presentation of the project

Rename your loop() to be autonomous_loop() and make a similar loop named manual_loop().

Make a new loop() which will decide which loop to call.

Paul

Something like that indeed - basically a finite state machine, with two states. Manual and automatic. The RC switches between the two; the program behaviour depends on the state.

The automatic mode is another part of your code where I'd expect to see a finite state machine.

And do get rid of all those delay() calls. That's slowing down reaction of your code big time, and may very well be part of your problem. Look at the "many things a time" thread for ideas on how to implement it all.

The functions delay() and delayMicroseconds() block the Arduino until they complete and will interfere with the responsiveness of a program.
.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

Also please use the AutoFormat tool to indent your code for easier reading.

Thanks to everyone who replied to this post, after a lot of research i came to find out that a simple WhileLoop and the use of the reset button in the Arduino gets the job done.

[/void loop() { while (leitura==HIGH){ digitalWrite(led,HIGH); autonomous (); } digitalWrite (led,LOW); manual (); }]

I may be kinda new here but this is really the first time that I heard of the reset button being used as integral part of controlling an Arduino program.