ultrasonic: bad distance values


My Ping))) Sensor sends me ~6 seconds about 2 to 5 bad distance values. (it jumps from 160 to 1 or 4)

I'm using the code from http://www.arduino.cc/en/Tutorial/Ping.
is this common or is there s problem with my sensor?

Now i sort out the bad frames in vvvv, but i think it would be much easier doing it directly on the arduino. could you help me?

best regards :slight_smile:

These sensors do this.
You can pre filter by not outputting anything that shows a sudden jump from the last reading. Alternately you could implement a running average of the last (say) ten readings.

Adjust alpha for the level of smoothing.

#define alpha 0.8

void loop()
 static double lastValue = 0;
 double newValue = readMyValue();
 double smoothValue = alpha * lastValue + (1 - alpha) * newValue;
 lastValue = newValue;

That is not a very good running average. You need to store n sample in an array and average them. Then replace the oldest sample with the newest one.

Its not a running average, its leaky integration and is very good at smoothing spikes and unwanted local maxima and minima out of a signal.

Try it - I have - it works well and is simple to implement.

Try it

I don't have to I spent three years teaching DSP to an MSc class.

This type of sensor depends on reflected sound waves. You can't have a lot of stuff in the way or even on its side. Could you post a picture?

If you face it towards the ceiling in the middle of the room, will you still get noise?