The code is pretty straight forward, i am storing the last 10 values and outputting the mean value. However, the 0 element of the array remains unchanged with initialized value '0'.
long int avg[10] = {0,0,0,0,0,0,0,0,0,0};
int i;
float temp2;
for (i=0;i<9;i++){
avg[i] = avg[i+1]; //shift all values to the left
}
avg[9] = temp2; //temp2 is the last value
temp2 = 0; //i am reusing temp2 as mean result
for (i=0;i<10;i++){
temp2 += avg[i];
}
temp2 /= 10;
By dividing the result by 9, the value is corrected but its purely a workaround and i would like to know why it is happening.
Thanks
EDIT:
so lets say my adc reads values close to 250. in the first runs, values are added in the array right to left:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 251
0 0 0 0 0 0 0 0 251 252
0 0 0 0 0 0 0 251 252 248
etc.
while after a lot of runs the values should have filled the table, the table always has this form:
0 251 252 248 250 247 253 252 248 247
i hope i made it more clear
-- edit 2:
Why are you downvoting my question? Is it because you dont understand it? What serious compiler would compile that without an error/warning that i did not initialize temp2 and what exactly is the probability that this problem would appear because of that reason?
You have not initialized
int temp2
. That's why you are getting such result.