Ping Programming Problem

i am stuck once again i can't seem to figure out why my motors will activate when the ping reads closer than danger threshold, however it will not execute/motors won't activate in the next part of code when the sensor or ping reads farther than danger threshold. what i am attempting to build is an obstacle avoidance rover. if you have any ideas it would greatly appreciated..... XD

here is my code:

int pingPin = 11; //pin for ultrasound sensor
long duration; //time it takes to recieve PING signal
int dangerThresh = 10; //threshold for obstacles (in cm)
int E1 = 5;   //E1: PWM control for Motor1
int M1 = 4; //M1: Direction for Motor1 pin 4
int E2 = 6;   //E2: PWM control for Motor2                      
int M2 = 7;  //M2: Direction for Motor2 pin 7    

void setup()
{ 
  pinMode(M1, OUTPUT);   
  pinMode(M2, OUTPUT); 
}

void loop()
{
 int distanceFwd = ping();
 if (distanceFwd<dangerThresh) //if path is blocked
  {
  digitalWrite(M1,HIGH);
  digitalWrite(M2, LOW);
  analogWrite(E1, 80);   //PWM Speed Control
  analogWrite(E2,100);   //PWM Speed Control
  }
else  if  (distanceFwd>dangerThresh)//if path is clear
  {  
  digitalWrite(M1,HIGH);
  digitalWrite(M2, HIGH);
  analogWrite(E1, 150);   //PWM Speed Control
  analogWrite(E2, 150);   //PWM Speed Control
  }
}
long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
 
  //Convert duration into distance
  //return duration / 74 / 2; //inches
  return duration / 29 / 2; // centimeters
}

code looks not bad.
you should check the values ping returns. If there is noise on the line you might average multiple e.g. 8 readings to remove noise.
bad contacts can also give "funny" effects .

unsigned long sum = 0;
for (int i=0; i<8; i++) sum += ping();
sum /= 8;

warning: int distanceFwd = ping();

ping returns a long and you assign it to an int. if the value is too big the int will overflow and become negative. As the distance is allways 0 or bigger you should use unsigned long for both.

don't know if this is the cause but it might help.

Are you aware that Serial.begin() and Serial.print()/Serial.println() can be used to debug your code?

Why does ping() return a long? Are you expecting to detect distances greater than 32767 cm?

 if (distanceFwd<dangerThresh) //if path is blocked
else  if  (distanceFwd>dangerThresh)//if path is clear

You won't get to the else statement unless distanceFwd is greater than OR EQUAL dangerThresh. All the the 2nd if test is doing is making sure than nothing happens if the two values are equal.

It seems to me that you really do want to do something if the values are equal.

duration is a global variable, used only in the ping function. Why is it global?

Finally, your safe/danger blocks do not look right to me. M1 is set HIGH in both cases. I'd recommend putting the code in those blocks in functions, with meaningful names, like goStraight(), turnLeft(), turnRight(), or whatever is appropriate.

Then, you can test that the two functions do what you expect them to do, regardless of what the ping sensor says.

Divide and conquer is the name of the game.