On Wed, May 5, 2021 at 09:17 AM, Howard Dutton wrote:
...so it takes the analog sample in the main loop, averages, then drops it in a global variable. The "(99 + 1) / 100" can be "(999 + 1) / 1000" or "(24 + 1) / 25", etc. The depth of averaging gives the frequency response vs. noise tradeoff. This is referred to as a rolling average and has the advantage of not requiring a massive array of values like a normal average does.
You could also bracket the analogRead() and averaging code so it runs at a fixed interval largely independent of the MCU frequency, etc. Below is for 1 Hz samples in which case you would probably want the depth of averaging to be only 5 or 10 so it's fairly responsive to changes.
static unsigned long lastSampleTime = 0; if ((long)(millis() - lastSampleTime) > 1000) { lastSampleTime = millis(); // the code }