simple obstacle evading robot with sharp sensor

This is my first project after the tutorials. What I am trying to do is powering tamiya dual gearbox tracked chassis. I used 4n35 optocouplers with bd137 transistors to protect my arduino. I am using a sharp sensor 2yoa21 to measure the distance. When sensor detects a range below determined value one track runs forward to change the heading of the robot. Nothing seems wrong with the wiring but somehow robot enters into unlimited turns instead of finding its way.
Here is the code any help is appreciated

int potPin = 0; // select the input pin for the sharpsensor
int ledPinIR = 11; // select the pin for the SENSOR LIMIT LED
int val = 0; // variable to store the value coming from the sensor
int ledPin = 12; // OPTOCOUPLER connected to the right engine
int ledPin1 = 13; // OPTOCOUPLER connected to the left engine
void setup() // run once, when the sketch starts

pinMode(ledPin, OUTPUT); // sets the righ engine pin as output
pinMode(ledPin1, OUTPUT); // sets the left engine pin as output
pinMode(ledPinIR, OUTPUT); // declare the ledPinIR as an OUTPUT

void loop() // run over and over again
val = analogRead(potPin); // sensor read
if (val < 250) // no object in range
digitalWrite(ledPin, HIGH); // sets right engine forward
digitalWrite(ledPin1, HIGH); // sets left engine forward
delay(100); // waits for a second
digitalWrite(ledPin, LOW); // stop to read the sensor
digitalWrite(ledPin1, LOW); // stop to read the sensor
else // object in range
digitalWrite(ledPinIR, HIGH); // led warning
digitalWrite(ledPin1, HIGH); // turn left
digitalWrite(ledPinIR, LOW); // stop to read sensor
digitalWrite(ledPin1, LOW);

I testet your code, seems to be correct.
Maybe your sensor values aren’t as expected?
try this:
add this line to the end of setup():


and this line after your val = analogRead(potPin):


Start the serial console for debugging and have a look at the values there. Are the values of the sensor always below 250 if an object is in range? And always higher than 250 is no object is in range?

And did you connect the sensor to the analog Pin 0 (and not to the digital pin 0)?
Another test: connect analog pin 0 to gnd… now the robot should always go forward…

When I connect analog 0 to VCC the warning LED blinks, when I don’t connect it at all, the warning LED blinks as well. If I connect it to GND, the LED does not blink, indicating, that the robot should be moving, just what you intend to do I guess.

The sensor has some random spikes. I used opto isolators to get rid of motor spikes but could not figure out what is wrong with the sensor

Take bohne's recommendation and spend the time to properly understand your sensor. Create a sketch which simply prints the analog value read off one of the analog pins. Hook the sensor up to this analog pin and test away.

Also, looking at the datasheet, the sensor output voltage never seems to exceed ~3.3V as noted in Figure 2. If this is the case, then the output as noted on screen should be within the range 0 to 675.

Post your results.