obstacle avoiding robot code problems **beginner**

Hi, I been messing about trying to make an O/A robot. I've basically converted an R/C car. I took out the original chip board and replaced it with an arduino uno and arduino motorshield r3. I've also used a 4 pin ultrasound for distance.

this robot has proven to be slightly trickier than the normal dif drive axle as on this model it uses the back wheels for forward and back and the front wheels for turning. the front axle is controlled by an DC motor connected to some cogs that move the wheels and a spring for retuning to straight. pictures to follow.

the problem i'm having is when i upload the code it only seems to be running one part of the code and not the full sequence. so once it detects an object it forever stays in that look and doesnt reset. I've also found that weirdly uploaing this code made nothing work at allfor about a good 5 minutes followed by the robot going into avoinding obstacle mode randomly and now nothing again. really confused as to how this is happening. as i dont have enough experience yet, i feels that im writing a sketch and its doing its own thing.
sensor code works properly on its own and yet doesn't seem to when i incorporate motors and vise-versa.

motor A controls rear drive
motor b controls steering through DC motor

code and pictures below. thanks for the help.


[
URL=Photobucket | The safer way to store your photos][/URL]

const int dirA = 12;
const int pwmA = 3;
const int brakeA = 9;

const int dirB = 13;
const int pwmB = 11;
const int brakeB = 8; \


const int trigPin = 4;
const int echoPin = 5;

void setup() {
  Serial.begin(9600);

  analogWrite(pwmA, 0); // Stop the motor
  analogWrite(pwmB, 0);

  pinMode(dirA, OUTPUT);
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW); // Turn off the brakes

  pinMode(dirB, OUTPUT);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW); // Turn off the brakes

  pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
}

void loop() {

  long duration, distance, inches;
  // Check the distance sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH, 29100);  // Timeout at 500 cm

  if (duration != 0)
  { // An object is detected
    distance = (duration / 2) / 29.1; // convert the distance to centimeters.
    if (distance > 25) {
      digitalWrite (dirA, HIGH);
      analogWrite (pwmA, 200); // 200//
      digitalWrite (dirB, HIGH);
      analogWrite (pwmB, 0); //0//
    }
    else
    {
      digitalWrite (dirA, LOW);
      analogWrite (pwmA, 150); //150//
      delay(2000);
      digitalWrite(dirA, HIGH);
      analogWrite(pwmA, 150); // 150//

      digitalWrite (dirB, HIGH);
      analogWrite (pwmB, 255); //255//
      delay (2000);

    }

  }
}


/code]

oh its also got a switch for battery housing (yellow wires is pos . grnd and pos connected to motorshield)

Which bit of your code is supposed to reset it? As far as I can see, if duration == 0, nothing happens. What you need is "if duration ==0, and I have been turning up until now, then come out of the turn".

      analogWrite (pwmA, 200); // 200//

The comments are completely useless. Instead of that crap, you need comments that explain what the code is supposed to do, like:

// Set the motors screaming in reverse
  digitalWrite(onePin, oneState);
  analogWrite(anotherPin, someValue);

  digitalWrite(stillAnotherPin, anotherState);
  analogWrite(yetAnotherPin, yetAnotherValue);

Then, you can tell us whether that code does what the comment says, or not.

It is impossible for you to tell us whether the car 200's or not.