Help please to improve this obstacle avoiding robot project.

The use of a variable that goes from 0 to 1 sounds very good to me. However, I´m doing something wrong with the way I implement the algorithm.

This is the new code with the counter:

#include <Servo.h>

int i=0;
Servo motorLeft;          // Define left motor
Servo motorRight;         // Define right motor
#define trigPin A2
#define echoPin A3

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

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 30 || distance <= 0){
    Serial.println("Out of range");
    motorLeft.write(20);
    motorRight.write(20);
  }
  else   
    if (i=0);{
    Serial.print(distance);
    Serial.println( "cm");     
    motorLeft.write(170);
    motorRight.write(10);
    }
    if (i=1);{
    Serial.print(distance);
    Serial.println( "cm");     
    motorLeft.write(10);
    motorRight.write(170); 
    }   
    if (i++ == 2) i = 0;         
  }

The motors don´t run propertly but instead they kind of start and stop running continuously.

Can you tell me what is wrong please?