Help please to improve this obstacle avoiding robot project.

I like the idea wildbill. Following your advise I introduced the following if statement:

if (distance >= 30 && OldDistance <= 29);{
 turnLeft = !turnLeft;

So no the new code is this one:

#include <Servo.h>

Servo motorLeft;          // Define left motor
Servo motorRight;        // Define right motor
#define trigPin A2
#define echoPin A3
boolean turnLeft = true;
int OldDistance;

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 motorLeft.attach(A5);    // Set left motor to pin A5
 motorRight.attach(A4);  // Set right motor to pin A4 
}

void loop() {
  
  int duration, distance;                               //ultrasonic sensor routine
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (distance >= 30 && OldDistance <= 29);{
  turnLeft = !turnLeft;
  }
   
  if (distance >= 30) { 
    Serial.println("Out of range");
    motorLeft.write(20);
    motorRight.write(20); 
    }
  
  if (distance <= 29 && turnLeft == true){
    Serial.print(distance);
    Serial.println( "cm");     
    motorLeft.write(170);
    motorRight.write(10);        
    }
 
  if (distance <= 29 && turnLeft == false){        
    Serial.print(distance);
    Serial.println( "cm");     
    motorLeft.write(10);
    motorRight.write(170);        
    }   
    
   OldDistance = distance;
}

To my understanding now the robot should alternate turns from one obstacle to the next one as I wanted. Instead when it finds and obstacle the weels try to go forward and backward at the same time so they get stucked. I really don´t know what is wrong now.

connect your trig and echo pins to digiral pins not with analog pins if ur using HC-SR04 distance sensor
check:http://skullbucks.in/projects/arduino/distancesensor.html

Oops:

  if (distance >= 30 && OldDistance <= 29);{

Get rid of the semicolon.

Made the two mods. Thanks wildbill and skullbucks. However, it still doesn´t works. The robot turns to one side or the other ramdomly.

I really don´t understand what´s not working. The code looks ok for me to do the task.

May it be a problem with the ' turnLeft = !turnLeft' statement ? I have seen that some people use ' bool turnLeft = !turnLeft ' instead. However I also tried this other way and there is no improvements.

biotech:
Made the two mods. Thanks wildbill and skullbucks. However, it still doesn´t works. The robot turns to one side or the other ramdomly.

I really don´t understand what´s not working. The code looks ok for me to do the task.

May it be a problem with the ' turnLeft = !turnLeft' statement ? I have seen that some people use ' bool turnLeft = !turnLeft ' instead. However I also tried this other way and there is no improvements.

plz mail me your circuit diagram with full pin details:mail.skullbucks@gmail.com
i'll mail you source code
one more thing dnt forget to mention your motors and sensors serial or model no. coz it needed for library

It's handy to put some serial print statements in your code

Serial.print("turning left");

or

Serial.print(yourvariable);

and set your robot with its wheels up, usb cable attached, and watch what parts of the code and choices are being accessed and when. You could have it serial print out your variable, for instance, and watch whether the expected or unexpected are going on.

Sorry guys for not being able to catch up yesterday and today so far.

I took off the serial prints statements just to see if the delays that went with them were causing any problem. That was not the case so I will put them back in again. Additionally, I'll send an email to skullbucks with my circuit diagram as requested.

Thanks again to you all for helping me!