help with ping sensor

I made code for a basic robot that will go straight until the sensor sees something 10 inches away, then it will shut down one motor spinning it around. When I upload the code it seems that the ping sensor isn't working. The code is below, can you tell me what I'm doing wrong.

const int dangerThresh = 10;
long duration;
const int pingPin = 7;

void setup(){
pinMode(12,OUTPUT); //a
pinMode(9,OUTPUT);
pinMode(2,OUTPUT);
pinMode(13,OUTPUT); //b
pinMode(8,OUTPUT);
}

void loop() {
int distanceFwd = ping();
if (distanceFwd>dangerThresh) //path is ok
{
pinMode(12,HIGH);
pinMode(9,LOW);
analogWrite(3,200);

pinMode(13,HIGH);
pinMode(8 ,LOW);
analogWrite(11,200);

digitalWrite(2,HIGH);
}

else
{
pinMode(9,HIGH);
digitalWrite(2,LOW);
delay(500);
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
delay(500);
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
delay(500);
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);

}
}

long ping()
{

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

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

return duration / 29 / 2;
}

Read this before posting a programming question

Code tags please.

Hey i'm not the only one that working on the ping module to night.

May i ask why you are not using the NewPing libraries?

Personally I will do a slow down that is proportional to the distance of the object, down to a 0 m/s at a selected minimal distance to beguine with. This operation drive me nuts since last night.

Will talk about it during the night see you.

You are calling ping() and assign the result to an int but ping() is defined as returning a long. However, the value that you return should be a float because of return duration / 29 / 2;

Have you tried monitoring the value you get when calling ping ?

As has been suggested, try the NewPing library.

You can cut lot of fat in the Newping libraries. If the size is the problem. Personally I use the metric system, but you seem to use imperial, so you can cut on the metric system.

One thing is for sure, is that Newping will at least tel you if you stuff work well or not. Cause it told me that there was a pen 1 meter away when the pen was actually 1 meter away.

The libraries work very well.

One thing is for sure, is that Newping will at least tel you if you stuff work well or not. Cause it told me that there was a pen 1 meter away when the pen was actually 1 meter away.

It is the sensor that should be credited, not the library.

It is the sensor that should be credited, not the library.

Well, considering the fact that the sensor does absolutely nothing if not hooked to something, at least a 555 or some data emitting device. I tend to say it’s the libraries + the Arduino.

But I must also agree that the fact that it’s very sensitive is from the fact that it’s well made and though.

So let’s say it’s a combination. :wink: Without team work, in this case, nothing happen.

What I mean, although i have not tried, is I don't think the Ping device would work with a switch hook on trigger and a LED hook on echo. Also, My visual system is not fast enough to catch a blink of The LED on the Ping back of the signal.

And by the time i do the back and forth calculation I'm gonna hit the wall for sure. :wink:

Never mind, the thing work with a switch and a LED lol.

But to know the distance, the way I think about it, would require at least a counter with a wide enough register and at least 3 555 and a few NAND gate to start and stop the process.

2 of those 555 to do a monostable function for emition and détection.
1 as clock to activate the counter to know how long between emition and reception of the ping.

And still I would have to do the calculation to translate time into distance.