Ultrasonic Sensor inconsistant reading when out of range

Hi There,

I've searched for answer to this problem and have not found any that seem to work.

When ever my ultrasonic sensor doesn't find a range, it outputs successive values at the upper and lower end of the range.

Heres a snippet of 6 successive pulses with some output. You will notice that there is both "left" and "right", to trouble shoot I disabled the right sensor to eliminate interference as a possible cause.

I can make this happen by either pointing t out into a distance usually greater than 2-3 meters or by putting a soft blanket in front of it.

This is particularly problematic because my rover take an input of ~3.5 cm and stops suddenly because it thinks something is too close. I don't want to simply eliminate reading > or < certain thresholds because depending on the circumstances these alternate reading can be ~2900 - ~6. I have tried numerous sensors. Any help would be very much appreciated.

=============
pulse start time =1481158784.55
pulse end time =1481158784.73
pulse duration = 0.176909208298
left = 3033.49, right =

=============
pulse start time =1481158785.76
pulse end time =1481158785.76
pulse duration = 0.000244855880737
left = 3.7, right =

=============
pulse start time =1481158786.8
pulse end time =1481158786.98
pulse duration = 0.17770409584
left = 3047.13, right =

=============
pulse start time =1481158788.01
pulse end time =1481158788.01
pulse duration = 0.000245094299316
left = 3.7, right =

=============
pulse start time =1481158789.05
pulse end time =1481158789.23
pulse duration = 0.17872595787
left = 3064.65, right =

=============
pulse start time =1481158790.28
pulse end time =1481158790.28
pulse duration = 0.000271081924438
left = 4.15, right =

Yes, they do that - sensors are not perfect, you have to code round it - filter inconsistent readings out by
comparing with the last few.

MarkT:
Yes, they do that - sensors are not perfect, you have to code round it - filter inconsistent readings out by
comparing with the last few.

Hi MarkT,

I'm quite new with arduino and have a lot to learn.
Could you give the example on how to filter inconsistent readings?
Or any function(s) related to filtering?