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 volt=analogRead(A0);
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.
(1/3 + 2/3 + 3/3) == (1+2+3)/3