PING sensor code refining help

hi all.

i'm using the ping sensor to control the brightness of LEDs. i'm checking distances and if within a range setting a flag triggers the LEDs to be brightened or faded by 1 each loop of the program. however - if i'm right at the edge of the range and the sensor values are jumping by one, it causes the LEDs to flicker. any suggestions on refining my code to avoid this? should i be averaging values to smooth it out?

thank you!

Averaging would probably work well.

The other option is to implement a dead-band (e.g. if the LED is turned off, it stays off for N seconds).

thank you! i'll give averaging a try - i don't know if i want to go the dead-band route as i need it to be immediately responsive, but i'll give it a try if averaging fails me.