obstacle bot just spins.

What is wrong with the below code? It is suppose to see if there is an object infront of it using a HC-SR04, if there is turn 90degrees. If not, drive forward. yet it just spins in circles...

/*
This program is adapted from the example Ping code from Arduino.

*/
 
//pin which triggers ultrasonic sound
const int pingPin = 8;
 
//pin which delivers time to receive echo using pulseIn()
int inPin = 7;
 
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers m2f, m1b in order to turn 90degrees.
int safeZone = 5;
 
//LED pin numbers
//int greenLed = 3, redLed = 2;
 
void setup() {
  // initialize serial communication
  Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
#define m1f 9
#define m2f 6
#define m1b 3
#define m2b 5
}

void loop()
{
  //raw duration in milliseconds, cm is the
  //converted amount into a distance
  long duration, cm;
 
  
 


 
  //setting up the input pin, and receiving the duration in
  //microseconds for the sound to bounce off the object infront
  pinMode(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
 
  //printing the current readings to ther serial display
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  //checking if anything is within the safezone, if not, keep
  //going forwards if safezone violated, turn 90degrees
  if (cm > safeZone)
  {
    digitalWrite(m1f, HIGH);
    digitalWrite(m2f, HIGH);
   
  }
  else
  {
   
     digitalWrite(m1b, HIGH);
    digitalWrite(m2f, HIGH);
    delay(200);
    digitalWrite(m1b, LOW);
    digitalWrite(m2f, LOW);
  }
 
  delay(100);
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;

    }

You've got it printing the distance to serial monitor. Is it printing the right values, or wrong ones?

hmm... its printing 100cm+ when my hand is only a few cm away. what would cause that?

I don't know if it is the cause of the problem, but..

if (cm > safeZone)
{
Here you don't write to m1b or m2b so they remain whatever they were.
}
else
{
Here you don't write to m1f or m2b so they remain whatever they were.
}

In fact, you never write to m2b.

If I write all the motors to low in the beginning, will that give me a clean slate?

bighat10:
If I write all the motors to low in the beginning, will that give me a clean slate?

I believe that pinMode(x,OUTPUT) also sets it low, but I'm not sure. (Maybe someone else can confirm.) Either way, it is good practice to set all your pins to a good initial state in your setup() function.

But what I meant was, inside the if(), where you set m1f and m2f high, you might want to set m1b and m2b low too, and in the else, where you set m1b and m2f low, you might want to set 1f and 2b to high.

Right now, if you have been inside the if at least once, and then get inside the else, you have m1f, m2f, m1b all high when you get to the delay. That might not be what you want, as it seems that you want to control motors in pairs.