Problem with my R/C car program

Hi guys, I am a newbie here and I have used this code here to program my R/c car:

#define enable1 2 //enable throttle motor
#define enable2 4 //enable steering motor
#define MLP1 3 //MPL = Motor Logic Pin (MLP1/2 = Throttle,MLP3/4 = Steering)
#define MLP2 2
#define MLP3 4
#define MLP4 5
#define Sp1 11
#define Sp2 12
#define trigPin 9 //Trigger Pin for HC-SR04
#define echoPin 10//Echo Pin
int duration;
int distance;

void setup(){
pinMode (enable1,OUTPUT);
pinMode (enable2,OUTPUT);
pinMode (MLP1,OUTPUT);
pinMode (MLP2,OUTPUT);
pinMode (MLP3,OUTPUT);
pinMode (MLP4,OUTPUT);
pinMode (trigPin, OUTPUT);
pinMode (echoPin,INPUT);

}

void loop(){
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(3000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/148);
if (distance >= 10){
Forward();
}

if (distance<10){ Here is where my problem is.
Backward();
Speaker();
delay(1000);
StopDriving();

}
}

void Backward(){ //drive backward
digitalWrite(enable1,HIGH);
digitalWrite(MLP1,HIGH);
digitalWrite(MLP2,LOW);
}

void Forward(){ //drive forward
digitalWrite(enable1,HIGH);
digitalWrite(MLP1,LOW);
digitalWrite(MLP2,HIGH);
}

void Left(){ //turn left
digitalWrite(enable2,HIGH);
digitalWrite(MLP3,LOW);
digitalWrite(MLP4,HIGH);
}

void Right(){ //turn right
digitalWrite(enable2,HIGH);
digitalWrite(MLP3,HIGH);
digitalWrite(MLP4,LOW);
}

void StopTurning(){ //self-explanatory
digitalWrite(enable2,LOW);
digitalWrite(MLP3,LOW);
digitalWrite(MLP4,LOW);
}

void StopDriving(){ //self-explanatory
digitalWrite(enable1,LOW);
digitalWrite(MLP1,LOW);
digitalWrite(MLP2,LOW);
}

void Speaker() {
digitalWrite(Sp1, HIGH);
digitalWrite(Sp2, HIGH);
delay(1000);
}

int DistanceDetect(){

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

}

I use a HC-SR04 Ultrasonic Sensor and a Arduino Uno REV 3
This program makes my r/c car move forward until it detects a wall, once it
does, it jerks backward for a second then continues going forward.

It works, but I want my car to move backward from the wall and then turn left for a second and it will continue on with the program
Just tell what I have to add to my program above so it turns left and moves away from the wall

THANKS EVERYBODY!!!

int DistanceDetect(){

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

}

As pointed out in the other thread on this topic, an "int" function really ought to return a value.

if (distance<10){         Here is where my problem is.
        Backward();
     Speaker();   
    delay(1000);
    StopDriving();
  }

If you can figure out what this is doing, it doesn't seem too hard to achieve what you want.

Please go back and modify your post to use code tags.

I do not really understand what you mean, I have labeled the part where I believe I should add a command to make my R/C car move left

this is the command I believe I should use Left();

not so sure though??

Please don't cross-post - it wastes time.

#define enable1 2 //enable throttle motor
#define enable2 4 //enable steering motor
#define MLP1 3 //MPL = Motor Logic Pin (MLP1/2 = Throttle,MLP3/4 = Steering)
#define MLP2 2
#define MLP3 4
#define MLP4 5
#define Sp1 11
#define Sp2 12
#define trigPin 9 //Trigger Pin for HC-SR04
#define echoPin 10//Echo Pin
int duration;
int distance;

void setup(){
pinMode (enable1,OUTPUT);
pinMode (enable2,OUTPUT);
pinMode (MLP1,OUTPUT);
pinMode (MLP2,OUTPUT);
pinMode (MLP3,OUTPUT);
pinMode (MLP4,OUTPUT);
pinMode (trigPin, OUTPUT);
pinMode (echoPin,INPUT);

}

void loop(){
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(3000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/148);
if (distance >= 10){
Forward();
}

if (distance<10){ Here is where my problem is.
Backward();
Left();
Speaker();
delay(1000);
StopDriving();

}
}

void Backward(){ //drive backward
digitalWrite(enable1,HIGH);
digitalWrite(MLP1,HIGH);
digitalWrite(MLP2,LOW);
}

void Forward(){ //drive forward
digitalWrite(enable1,HIGH);
digitalWrite(MLP1,LOW);
digitalWrite(MLP2,HIGH);
}

void Left(){ //turn left
digitalWrite(enable2,HIGH);
digitalWrite(MLP3,LOW);
digitalWrite(MLP4,HIGH);
}

void Right(){ //turn right
digitalWrite(enable2,HIGH);
digitalWrite(MLP3,HIGH);
digitalWrite(MLP4,LOW);
}

void StopTurning(){ //self-explanatory
digitalWrite(enable2,LOW);
digitalWrite(MLP3,LOW);
digitalWrite(MLP4,LOW);
}

void StopDriving(){ //self-explanatory
digitalWrite(enable1,LOW);
digitalWrite(MLP1,LOW);
digitalWrite(MLP2,LOW);
}

void Speaker() {
digitalWrite(Sp1, HIGH);
digitalWrite(Sp2, HIGH);
delay(1000);
}

int DistanceDetect(){

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

}

I changed a part here in this code, when i uploaded it, what happens is the car goes forward AND turns left at the same time, when it senses a wall, it turns backwards and turns left. It is not what I am looking for.?

sorry about cross-posting

tags PLEASE

it turns backwards and turns left

Backward();
        Left();

Not surprising that it does both, is it?

i dont understand the tags
if you want you can just copy the code and put it.

if you want you can just copy the code and put it.

I don't want to do it, I want you to do it.

They do this:
Your code in a nice scrolling box, not in sprawling mass down the page

Didn't you read the posting guidelines?
Why not?

 #define enable1 2 //enable throttle motor
#define enable2 4 //enable steering motor
#define MLP1 3 //MPL = Motor Logic Pin (MLP1/2 = Throttle,MLP3/4 = Steering)
#define MLP2 2
#define MLP3 4
#define MLP4 5
#define Sp1 11
#define Sp2 12
#define trigPin 9 //Trigger Pin for HC-SR04
#define echoPin 10//Echo Pin
int duration;
int distance;

void setup(){
  pinMode (enable1,OUTPUT);
  pinMode (enable2,OUTPUT);
  pinMode (MLP1,OUTPUT);
  pinMode (MLP2,OUTPUT);
  pinMode (MLP3,OUTPUT);
  pinMode (MLP4,OUTPUT);
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin,INPUT);


}

void loop(){
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(3000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/148);
  if (distance >= 10){
    Forward();
  }
  
  
  if (distance<10){         Here is where my problem is.
        Backward();
        Left();
     Speaker();   
    delay(1000);
    StopDriving();

 
    
    
  }
}

void Backward(){ //drive backward
  digitalWrite(enable1,HIGH);
  digitalWrite(MLP1,HIGH);
  digitalWrite(MLP2,LOW);
}

void Forward(){ //drive forward
  digitalWrite(enable1,HIGH);
  digitalWrite(MLP1,LOW);
  digitalWrite(MLP2,HIGH);
}

void Left(){ //turn left
  digitalWrite(enable2,HIGH);
  digitalWrite(MLP3,LOW);
  digitalWrite(MLP4,HIGH);
}

void Right(){ //turn right
  digitalWrite(enable2,HIGH);
  digitalWrite(MLP3,HIGH);
  digitalWrite(MLP4,LOW);
}

void StopTurning(){ //self-explanatory
  digitalWrite(enable2,LOW);
  digitalWrite(MLP3,LOW);
  digitalWrite(MLP4,LOW);
}

void StopDriving(){ //self-explanatory
  digitalWrite(enable1,LOW);
  digitalWrite(MLP1,LOW);
  digitalWrite(MLP2,LOW);
}

void Speaker() {
  digitalWrite(Sp1, HIGH);
  digitalWrite(Sp2, HIGH);
  delay(1000);
}

int DistanceDetect(){ 

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

}

f

like this?

so where do I put the command so that it turns left away from the wall?

Yes, but you've still got the back/left movement going on at the same time.
You could slip another "delay" in there, but eventually, you're going to want to do it properly.

 if (distance<10){         
        Backward();
        delay(1000);
        Left();
     Speaker();   
    delay(1000);
    StopDriving();

would I put it here or where else?

Why don't you just try it?
That's the great thing about the Arduino.
Play with it, experiment, don't just keep asking people who don't have your hardware.

ok thanks!!!

still does the same thing

OK.
Keep trying and thinking.
Have a look at some of the function names for clues.

i will look

can you please tell me why the rc car goes forward and turns left before it senses the wall, when I command it to turn left after it has senses the wall?

Time for some debug prints?

what are those?

i am really new to arduino