Avoiding obstacles...very simple

I apologize as I know many will laugh, but I am the true definition of a newbie, noob, whatever you want to call me, but I do appreciate the help in advance!

I got my cheap flea market car, one dc motor driving both rear wheels, one spring loaded to return to center dc motor to turn right or left motor. One ping (parallax) sensor. I just want to start with basics. I did try to search all over, but could not find a basic idea for what I am trying to do, although I think everyone else on the planet already knows how to. I just want the car to go forward until it "sees" something and then turn the front wheels, back up for more or less 90 degrees and continue forward. Again, I really appreciate someone pointing out the obvious to me. Thanks!! - Dwayne

int motorPin = 12;
const int pingPin = 7;

void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(motorPin, HIGH);
digitalWrite(9, LOW);
analogWrite(3, 95);
}

void loop() {

long duration, inches;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

inches = microsecondsToInches(duration);

/*Serial.print(inches);
Serial.print("in, ");
Serial.println();
*/
digitalWrite(motorPin, HIGH);
analogWrite(11,0);

if (inches <= 12) {
digitalWrite(9, HIGH);
//delay(2000);
//int x = x + 1;
//do
//{

digitalWrite(motorPin, LOW);
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 100);
digitalWrite(8, LOW);
digitalWrite(13, LOW);
analogWrite(11, 255);
//} while (x < 10);

//for(int x = 0; x < 2000; x++);

//delay(2000);
//analogWrite(3,100);
//delay(3000);

}

//delay(250);

}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

I know everyone that is looking is laughing, but I really want to get this working tonight if possible...please help!! I know everyone reading can help me...thanks!

suggestions:

a) use this:

 [ code ][ /code ]

for posting code (the hash button next to the quote button)

b) use the auto-format function in your arduion IDE

c) tell us what currently happens when you run this code and what you expect to happen. more comments in the code might be nice - I have no idea what 13, 8 and 9 are connected to.

the only thing ovious I see right now (but I am not expert) is that you might want to set pingPin to INPUT inside your setup function. Also, you may consider setting pins 11 and 3 as output.

Thank you fkeel..

I am sorry, you are right on all counts, and thank you for even looking. I should make it much more clear. I will work on this and come back with better posts regarding this.

I am using an Uno with an Arduino Motor Shield, just trying to go forward, see an object, go back while turning, then go forward again. I think is probably one of the most basic robots I can imagine, but two days later, it sees the obstacle, turns the front wheels for a split second, reverses the wheels for a split second...until the ping distance is greater than 12 inches again, then starts forward immediately, no time for a backwards turn. I just want the code to run after sensing an object to give it time to turn. I left in a lot of junk I tried so maybe people could see my trying. Delay() does not seem to work...for loops no joy...do-while...nothing...

Again, I really am new, thanks for the etiquette tips and I appreciate the education in all aspects! - Dwayne

DwayneArduino:
I am sorry, you are right on all counts

Actually I am not. pingPin does not need to be initialized in setup. (as I said. I am no expert.)

I messed around with your code a bit. I still don't really understand it, as I dont know what the individual pins are connected to. (Also, I have never used the Arduino Motor Shield.)

int motorPin = 12;
const int pingPin = 7;
long duration, inches;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT); //What does it connect to?
  pinMode(8, OUTPUT); //What does it connect to?
  pinMode(motorPin, OUTPUT);
  pinMode(9, OUTPUT); //What does it connect to?

  //What are you doing here?
  digitalWrite(motorPin, HIGH);
  digitalWrite(9, LOW);
  analogWrite(3, 95);
}

void loop() {


  // checking the distance
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);

  /*Serial.print(inches);
   Serial.print("in, ");
   Serial.println();
   */

  if (inches <= 12) { //if you are closer than 12 inches, do this:
    digitalWrite(9, HIGH);
    digitalWrite(motorPin, LOW);
    digitalWrite(9, LOW);   //Disengage the Brake for Channel A
    analogWrite(3, 100);
    digitalWrite(8, LOW);
    digitalWrite(13, LOW);
    analogWrite(11, 255);
  } 
  else { //else do this:
    digitalWrite(motorPin, HIGH);
    analogWrite(11,0);
  }

}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

it sees the obstacle, turns the front wheels for a split second, reverses the wheels for a split second

I think this should potentially be solved now by the way I set up the if-else statement. Check that.

...for loops no joy...do-while...nothing...

your syntax seems to be wrong. Look at this:

while (x < 10){
  for(int y = 0; y < 2000; y++){
    delay(2000);
    analogWrite(3,100);
    delay(3000);
    //you do realize that you are telling your arduino to do a 10 000 000 millisecond delay here?
    //if my math is correct, thats ~166 minutes, which is almost 3 hours.
  }
}

See what I did different?