# How to get the overall average of the data received

I have an arduino uno with two sensors. Voltage and current sensors. I need to get the average value of voltage and current each time the data is received. But i don't know how to add up the current and previous values and divide it to how many values that it has received.

Can someone help me, please? I need it for my project

This is the code i have made

``````void setup(){
Serial.begin(9600);
}
void loop(){
int n=5;
float sum=0;
float ave=0;
for(int x=0;x<n;x++){
float voltage = map(volt,0,1023,0,2500);
voltage/=100;
Serial.print("Voltage: ");
Serial.print(voltage,2);
sum=sum+voltage;
}
ave=sum/n;
Serial.print("Average: ");
Serial.print(average,2);
delay(3000);
}
``````

But this only takes the average of 5 datas. I want to get the average of all data received.

• – Majenko Dec 11 '19 at 16:23
• it's actually communicative: `(1/3 + 2/3 + 3/3) == (1+2+3)/3` – dandavis Dec 11 '19 at 18:38
• @Majenko does your library offer cumulative average, running average, exponential average, or any/all of those? – Duncan C Dec 11 '19 at 20:16
• To the OP, do you want a cumulative average? As mentioned in the comments on DataFiddler's answer, with a very large number of samples the average will change so slowly that it will be hard to detect a change. (e.g. After a million samples have been averaged, if the input value suddenly change to 0, it will take 100,000 new samples, all at 0, for the average value to change by 10%) – Duncan C Dec 11 '19 at 20:24
• You might want to use an exponential value or other rolling average instead. – Duncan C Dec 11 '19 at 20:24

``````void setup(){
Serial.begin(9600);
}
void loop(){
static unsigned long count=0;
static float sum=0;
float ave=0;
float voltage = map(volt,0,1023,0,2500) / 100.0;
Serial.print("Voltage: ");
Serial.print(voltage,2);
sum=sum+voltage;
count++;
ave=sum/count;
Serial.print("  Average: ");
Serial.println(ave,2);
delay(3000);
}
``````

This runs quite a while. The average is restarted only with a Reset of your arduino.

Alternatively, you might want to learn about a moving average (aka low pass filter)

• You println an unknown variable `average`. You declared `ave`. You should also mention that with this method the algorithm can only run some time until float overflows. A saver way would be A(1) = x(1); A(n+1) = A(n) * (n/(n+1)) + x(n+1)/(n+1). "A(n) = avarage in the n-th iteration; x(n) the n-th sensor value." – Peter Paul Kiefer Dec 11 '19 at 17:27
• `ave` fixed. It will overflow after 400 years of uninterrupted operation, thanks to `delay(3000);` – DataFiddler Dec 11 '19 at 18:11
• This code receives equal value or output for voltage and average of it. Maybe it doesn't count the previous datas it received – Rica Jacutina Dec 11 '19 at 18:14
• Did you observe the `static` keyword of `sum`? Eventually you add more Serial.println to check sum and count as well for debugging? – DataFiddler Dec 11 '19 at 18:20
• Oh. I got it! Thank you so much. I just put a wrong variable on the serialprint for average. 😂 – Rica Jacutina Dec 11 '19 at 18:26