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.