As @Team80_Giraffes have said there are many options of how you can compute the moving average.
My favorite is exponential smoothing which needs only one variable and one extra line of code:
float wSmooth = 0;
while(1)
{
int w = SensorValue[foo];
wSmooth = (3*wSmooth + w)/4;
// do something with wSmooth
delay(20);
}
However, I couldn’t blindly recommend it to you, because I don’t know what you are trying to average and what kind of noise you are trying to filter out or smooth.
For example, if you are reading quad encoder measurements and all the noise nicely falls in the 10 RPM range then exponential smoothing will work very well for you. Not only it is simpler to implement, but it will give more weight to the latest reading over the older data.
If you see large instrumental errors, like every once in a while getting 4563456345 or -345234532 instead of nice values around 100, then median smoothing may be what you need.
But, if your sensors are not that bad, you could make exponential smoothing slightly more robust against the outliers:
float wSmooth = 0;
while(1)
{
int w = SensorValue[foo];
if( abs(w-wSmooth) < 10.0 ) // is measurement inside expected sensor noise level?
wSmooth = (3*wSmooth + w)/4; // yes - gladly accept it
else
wSmooth = (7*wSmooth + w)/8; // no - be cautious and trust older averaged value more
// do something with wSmooth
delay(20);
}
Once again, if you tell us what exactly you are trying to average, or even better, log the data and post the graph, then we could give you more precise advice.