Calculating average of analog read values

is there anyway calculate an average of an int analogRead values from a sensor and store in another int ?
since the values are always changing that means that the average is also gonna change but i want to take the average at a certain time, say when a button is pressed for example.. is that possible ?

1 Like
 int average = 0;
 for (int i=0; i < 10; i++) {
 average = average + analogRead(A0);
 }
 average = average/10;
2 Likes

you can get smoothed values as follows: as n increases the current value gets less weight.
this is not an average per se but is usually good enuff to filter out noise and doesn't require maintaining
an actual list of previous values.

smoothed = (smoothed * n-1 + analogRead()) / n

1 Like