A simple way to do a moving average is to use floating point math.
- Code: Select all | TOGGLE FULL SIZE
float adcResult = 0;
adcResult = adcResult * 0.95 + analogRead(0) * 0.05;
It's computationally a little heavier but gives pretty good results. The filter above is similar to a 20-point moving average, meaning that changes will appear instantly on the output, but will be moderated by previous values. It's good when you want smoothed values with minimal lag; you don't have to wait to collect 20 samples, you just throw away a small piece of the old value and add in a small piece of the new value.
You could do this with integers too, 7*adcResult + analogRead(0) and then divide by 8 or shift right by 3. You just have to make sure not multiply by too much or you'll have to switch to longs.
A true moving average, based on a rolling buffer, does have some advantages too. You can decide how to weight older values, or try to throw away outlying data points from sporadic noise. But it does use up more memory.