rc car program not working!!

Hi i am a newbie and I have a code see below and this code makes my arduino controlled rc car go forward until it senses a wall with its ultrasonic sensor, then it goes backwards and repeats. My question is how can make my car go backwards then turn LEFT after it has sensed a wall

here is the code

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

}

and this is what I am thinking how I can program it

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

please tell me what I am doing wrong and how can I fix it? Thank you all!!!

It seems odd that you are using pins 2 and 4 for two purposes each:

#define enable1 2 //enable throttle motor
#define MLP2 2

#define enable2 4 //enable steering motor
#define MLP3 4

yes because pin 2 is used for the throttle motor and for moving forwards and backwards pin 4 is the steering motor and used for moving left and right

it still works though

s_tanay: please tell me what I am doing wrong and how can I fix it? Thank you all!!!!

So what is the car doing then? What is "wrong" about it?

Nothing it wrong, when i upload this code

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

along with the other code in the first post, my rc car will move forward AND turn left at the same time before it senses the wall and then senses the wall and goes backward and left. What I want it to do is
go forward until it senses a wall then move backwards and turn left. I tried writing this but I can’t make it to work. That is my question

What do you suppose would happen if you called StopTurning() in the section of code that executes when your car starts moving forwards?

What do you suppose would happen if you called StopTurning() in the section of code that executes when your car starts moving forwards?

OMG!! I haven't thought of that, let me see what I can do

PeterH,

You are a genius!!!!!! It worked! Thank you so much.