# Averaging input from multiple input over a fixed amount of time (estimated)

I am trying to average input values for a number of inputs and have it then store those averages into a string to write to a txt doc on a sd card.

I am using:

``````// read 6 sensors and append to the string:
for (int analogPin = 0; analogPin < 6; analogPin++) {
dataString += String(sensor);
if (analogPin < 6) {
dataString += ",";
}
}
``````

to read the pins at the moment, but I want to change the analogRead(analogPin) to an average value and then use the averages to create the string.

but when I try to average the data I run into errors, or I get output that is 0 for something that should be higher (3.3V).

I am doing this to stabilize my values a bit better and remove jitter.

Any suggestions?

• Can you show us your entire sketch? Jun 20, 2018 at 23:10
• take 10 readings, discard the highest and the lowest, then average the remaining 8 readings Jun 21, 2018 at 0:00
• github.com/MajenkoLibraries/Average Jun 21, 2018 at 10:14

This function will average readings on an analog pin, include it and use it in place of analogRead(...) and it should do what you want.

``````int aveAnalogRead(int pin, int ave=10, int delayms = 0)
{
int tot = 0;
for(int i=0; i<ave; i++)
{
if(delayms) delay(delayms);
}
}
``````

``````for (int analogPin = 0; analogPin < 6; analogPin++)
• so this:`int pin, for (int n=0, n<10, n++){ pin = n; int aveAnalogRead( int ave=10, int delayms = 50) { int tot = 0; for(int i=0; i<ave; i++) { tot += analogRead(pin); delay(delayms); } return tot/ave; }}` Jun 21, 2018 at 14:39