Obstacle avoiding robot

I am making an obstacle avoiding robot. I am using the exact same parts and the exact same code as this tutorial:

But when front side and the right side is blocked and the left side is open, it does not move to left, but goes backwards.

I know that left() works fine. But distance_l does nothing. I don’t think the car is able to measure the left side, or somehow it is convinced that the left side is blocked when it is not.

Do you know how to solve this?

Here is an image of my car
And I attached my code aswell.

spyr.ino (6.08 KB)

Please post your code

Here is the code

#include <Servo.h> // Includes servo library.
// defines pins numbers
const int trigPin = 12;
const int echoPin = 11;
const int lm1=5; //left motorur
const int lm2=4; //left motorur
const int rm1=7; //right motorur
const int rm2=6; //right motorur
const int servoPin=13; //

Servo myservo;

// defines variables
long duration;
int distance;
int distance_f;
int distance_r;
int distance_l;
int maxLowDistance=20;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(lm1,OUTPUT);
pinMode(lm2,OUTPUT);
pinMode(rm1,OUTPUT);
pinMode(rm2,OUTPUT);

myservo.attach(servoPin);
myservo.write(90);

Serial.begin(9600); // Starts the serial communication.
}
void loop() {
distance_f=ping();
if(distance_f > maxLowDistance){
front();
delay(300);
}else{
Break();
get_Distance();
if(distance_r > maxLowDistance){
right();
delay(1000);
front();
}else if(distance_l > maxLowDistance){
left();
delay(1000);
front();
}else{
back();
delay(150);
Break();
}

}
}
void displayDistance(){
Serial.print(“Right Distance : “);
Serial.print(distance_r);
Serial.println(””);
Serial.print(“Front Distance : “);
Serial.print(distance_f);
Serial.println(””);
Serial.print(“Left Distance : “);
Serial.print(distance_l);
Serial.println(””);

}
void front(){
//Serial.println(“Forward Move”);
digitalWrite(rm2,HIGH);
digitalWrite(lm2,HIGH);
digitalWrite(rm1,LOW);
digitalWrite(lm1,LOW);

}
void back(){
// Serial.println(“Back Move”);
digitalWrite(rm1,HIGH);
digitalWrite(lm1,HIGH);
digitalWrite(rm2,LOW);
digitalWrite(lm2,LOW);
}
void right(){
digitalWrite(lm2,HIGH);
digitalWrite(lm1,LOW);
digitalWrite(rm1,HIGH);
digitalWrite(rm2,LOW);
}
void left(){
digitalWrite(rm2,HIGH);
digitalWrite(rm1,LOW);
digitalWrite(lm1,HIGH);
digitalWrite(lm2,LOW);
}
void Break(){
digitalWrite(lm2,LOW);
digitalWrite(lm1,LOW);
digitalWrite(rm1,LOW);
digitalWrite(rm2,LOW);
}

void get_Distance(){
myservo.write(0);
delay(500);
int temp_l1=ping();
myservo.write(45);
delay(500);
int temp_l2=ping();
if(temp_l1<temp_l2){
distance_l=temp_l1;
}else{
distance_l=temp_l2;
}

myservo.write(90);
delay(500);
distance_f=ping();

myservo.write(135);
delay(500);
int temp_r1=ping();
myservo.write(180);
delay(500);
int temp_r2=ping();
if(temp_r1<temp_r2){
distance_r=temp_r1;
}else{
distance_r=temp_r2;
}
myservo.write(90);
}

int ping(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
return distance;
}

What does the serial monitor show is happening?

Please remember to use code tags when posting code.

Mr.Sai is a Hardcore Coder in the Field of Arduino,Android,Java,C,C++,PHP,HTML,CSS

:o If you say so, dude.

I would add some debug output to the end of the get_Distance() function. Display all of the measured distances. It could be that your left sensor is broken and always returning zero.