Problems in my code for robot

Hello,
I am doing a project which is a obstacle avoiding robot. But the problem is my code only works for forward, backward & right direction moves of the robot. Left direction is not working.
I have attached my full code below. In this project I have used:

  1. Arduino UNO (1 pcs)
  2. Ultrasonic Sensor (1 pcs)
  3. Robot Chassis V1.1 (1 pcs)
  4. Re Charger able Battery (2 pcs)
  5. L298N Motor Drive (1 pcs)
  6. Jumper wires (1pack = 40 pcs)
  7. Charging & power board (1 pcs)
  8. Robot Charger (1 pcs)

So Please tell me whether the problem is & also check the other mistakes in my code which is need to correction. My project have to submit between 2-3 days. So please help me. I will be really grateful to you.
Thanks in advance.
—Tanvir.

Code:

const int numOfReadings = 10; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
// setup pins and variables for HC-SR04 sonar device
int echoPin = 12; // HC-SR04 echo pin (digital 12)
int trigPin = 13; // ping pin (digital 13)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm)
int motor1Pin1 = 10;
int motor1Pin2 = 9;
int motor2Pin1 = 5;
int motor2Pin2 = 6;
int pulsewidth1 = 255;
int pulsewidth2 = 180;
int pulsewidth3 = 10;
int pulsewidth4 = 20;
void setup() {
// set the motor pins as outputs:
Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(trigPin, LOW); // stop sending the pulse
pinMode(echoPin, INPUT);
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
}
// check the average distance and move accordingly
if (averageDistance <= 10){
  // go backwards
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
if (averageDistance <= 35 && averageDistance > 10) {
// turn left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
if (averageDistance <= 35 && averageDistance > 10) {
// turn right
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
}
if (averageDistance > 35) {
// go forward
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
}
}

my code for robot.docx (20.4 KB)

click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.

Post a photo of your L298 Motor Driver and a link to the vendor it came from.
Draw a schematic with pen and paper of your circuit that shows all the connections on the arduino and the motor driver ONLY.
(LEAVE EVERYTHING ELSE OUT for now.)
Take a photo of this hand drawn schematic and post it. Please don't post a Fritzing . We want a schematic that you had to draw while looking at YOUR circuit so we KNOW that the schematic you post matches the wiring in the photo you are going to post.
While you drawing that schematic, ask yourself why you are not doing anything in your code to control the speed of your motors.
Be prepared to give a good reason why you have no analogWrite statements in your code. What kind of robot is that ?
Have you ever seen a robot that only has two speeds , full speed and STOP ? Do you drive your car like that ? How would you like to ride on a bus where the driver had the accelerator buried in the floor board until he came to a light and then slammed on the brakes ? Does that make sense ?
Have you read the datasheet for the L298 ?

Why is this :

 // turn left
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);

not the same as this: ?

// turn right
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);

STEP-1
Add LEDS for ALL YOUR DIRECTION PINS
STEP-2
Take a photo of your circuit from directly above , close enough to see wiring detail and post it.

Your robot does turn left, it just doesn’t do it for very long :wink:

if (averageDistance <= 35 && averageDistance > 10) {
    // turn left
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
  }
  if (averageDistance <= 35 && averageDistance > 10) {
    // turn right