I currently use the following function to calculate moving average for temperature readings that are happening each 200ms
.
uint16_t ntc_average(uint16_t adcdata)
{
static uint8_t first = 1;
static uint16_t t1,t2,t3,t4,t5;
if(first == 1)
{
first = 0;
t1 = t2 = t3 = t4 = t5 = adcdata;
}
t5 = t4;
t4 = t3;
t3 = t2;
t2 = t1;
t1 = adcdata;
adcdata = (t1+t2+t3+t4+t5)/5;
return(adcdata);
}
However, 5 points are not enough and I might need much much longer buffer to smooth more. For example once or twice per 10-20 readings the value may drop one point up or down and I need to smooth that. Increasing tn variables seems ugly... I guess that I might need t1-t50.
Can anyone suggest another function in C
which I can use for smoothing temperature readings? Note that the values are unsigned integers and not float.