Smooth analog output

Hi there,

I've got a neat project I'm working on and I have a little snag that I can't seem to figure out on my own.

I'm using a parallax Ping))) to control the brightness of an LED (for now) and I've gotten the LED to brighten based on how close you are to the Ping))) but I want to smooth the output of the ledPin.

I am getting confused because I am using a 1 millisecond delay to control the Ping)))'s sensor value and I can't seem to figure out how to use the "smooth signal" or "fade LED" tutorials to achieve a smooth increase and decrease of LED brightness rather than the instantaneous LED reflection of the Ping)))'s measurements.

basically I don't want the LED to reflect the Ping)))'s distance measurement instantly, but instead fade from one value to the next.

taking distance readings at a rate of once a millisecond makes it difficult to see the effects of a fade but all that I'm really trying to avoid is the LED shooting between two dramatically readings ie: when the distance shoots from 3cm - 300cm.

so I've been trying to figure out some sort of debounce modification that incorporates analogWrite fading so that when ever there is a large enough jump in distance there is a fade to protect from ugly blinking.

so far my attempts have not been very fruitful.

I'd really like to know if what I'm proposing is possible, and if so how to achieve this effect.

thanks - IMUR

You need Low Pass Filter, LPF in short. First you feed analogRead value to LPF, and after you supply "filtered" output value to PWM analogWrite. analogRead ->LPF ->analogWrite

You could keep a running average in an array, but another technique is a digital filter, simplest example below:

float value = 0.0 ;

void loop ()
{
  value += 0.1 * (analogRead (pin) - value) ; // adjust 0.1 smaller for slower response and vice versa
  analogWrite (other_pin, (int) value) ;
}