RC Car Controlled by Arduino Part 2

So I am making an RC car controlled by an arduino. an ultrasonic sensor signals the arduino to kill the motor when the car is within 10 inches of an object. The code works fine:

#define enable1 12 //enable throttle motor
#define enable2 4 //enable steering motor
#define MLP1 10 //MPL = Motor Logic Pin (MLP1/2 = Throttle,MLP3/4 = Steering)
#define MLP2 5
#define MLP3 11
#define MLP4 6
#define trigPin 9 //Trigger Pin for HC-SR04
#define echoPin 8//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(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/148);
if (distance >= 10){
Forward();
}
if (distance<10){
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);
}

int DistanceDetect(){ //for HC-SR04 (from http://winkleink.blogspot.com/2012/05/arduino-hc-sr04-ultrasonic-distance.html)

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

}

Then i discovered a problem…The car keeps coasting after the motor is killed. To stop this, I added a reverse pulse to the code (the moter spins backward for 1/10 second):

#define enable1 12 //enable throttle motor
#define enable2 4 //enable steering motor
#define MLP1 10 //MPL = Motor Logic Pin (MLP1/2 = Throttle,MLP3/4 = Steering)
#define MLP2 5
#define MLP3 11
#define MLP4 6
#define trigPin 9 //Trigger Pin for HC-SR04
#define echoPin 8//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(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/148);
if (distance >= 10){
Forward();
}
if (distance<10){
StopDriving();
Backward();
delay(100);
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);
}

int DistanceDetect(){ //for HC-SR04 (from http://winkleink.blogspot.com/2012/05/arduino-hc-sr04-ultrasonic-distance.html)

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

}

The car “stops” when it detects an object, but it keeps jerking back. I know this happens because the code keeps looping, but I don’t know how to fix it. An advice or solutions?..

Using an H bridge it is usually possible to arrange things so that the motor is short circuited by setting the inputs appropriately. The back EMF from the motor when in this condition means that the motor is very difficult to turn and is effectively braked.

If you cannot arrange this then, once you have triggered the reverse pulse set a variable, let's call it stopped, to true. Before issuing the reverse pulse check the stopped variable and if it is true do not do the reverse pulse again.

int DistanceDetect(){ //for HC-SR04 (from http://winkleink.blogspot.com/2012/05/arduino-hc-sr04-ultrasonic-distance.html)

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

}

Where is your return statement? A non-void function MUST return a value!

Besides the fact that this function as-is is useless, why aren't you calling it?

If you aren't using it, why aren't you deleting it?

Could you compare successive readings from the distance sensor to get some idea of how fast the vehicle is moving and remove the ‘reverse/braking’ when the forwards speed drops below a threshold?

Alternatively use edge detection to indicate when you first encounter the obstruction and only apply the reverse pules on the first detection.

I'm going to try UKHeliBob's idea. I'll repost when I do. And PaulS: the DistanceDetect function originally had a return statement.I accidentally deleted it.And I am not calling it because the code from the function is already in the void loop. But you're right, I should have deleted it

I wanted to know how can i modify this code so that when my rc car senses a wall, it will back away from it and then turn left?
How can I program this?

s_tanay: I wanted to know how can i modify this code so that when my rc car senses a wall, it will back away from it and then turn left? How can I program this?

You find the section of code which executes when the car senses a wall, and put in statements to make the car stop, back away and turn left.