Hye.. I am doing a project in which I am using arduino to get data from a sensor.. As there is lot of fluctuations within a second, i want to get 100 values in 1 second and take their average output. How can i do that? please help
A Google search for "how to average 100 readings arduino" yields nearly 1,300,000 hits like this one.
To get 100 values in 1 second you take a reading every 10ms, sum the readings and after 1 second (or 100 readings) divide the sum by 100.
It always comes down to accuracy.
A kludge would be to average 10 readings, then store that as a value, do that 10 times, smaller buffer, average is always the last 10 groups of readings.
Averaging is pretty simple... Add & divide. You can keep a running-total so you don't have to save all of the readings.
The [u]Smoothing Example[/u] gives you a moving average. As written, it averages 10 readings with a 1ms delay but you can change that. The moving average does require you to store a number of readings in an array.
...I'm working on an audio project and I just wrote a little test-program that averages 1000 readings and saves the minimum & maximum. The loop is running at "full speed" (no delays) and it takes about 100ms (1/10th of a second) to take 1000 readings, sum, and keep track of the min & max.
// ReadAnalogAverage
// For audio input testing
// Prints min, max, and average to the serial monitor
// Should work with a line-level or headphone-level signal
// Typically the input is biased at Vcc/2 for a reading of ~512 with no signal
// Reads input A0
// Global Variables
int Min;
int Max;
int Analog;
unsigned long Sum;
unsigned long Average; //Should be equal to the bias.
void setup()
{
Serial.begin(9600);
delay(100); //"Stabilization time". Probably not necessary
}
//Main Loop
void loop()
{
Min = 1023; //Initilize/reset to limit
Max = 0; //Initilize/reset to limit
Sum = 0; //Initialize/reset
//Take 1000 readings, find min, max, and average. This loop takes about 100ms.
for (int i = 0; i < 1000; i++)
{
Analog = analogRead(A0);
Sum = Sum + Analog; //Sum for averaging
if (Analog < Min)
Min = Analog;
if (Analog > Max)
Max = Analog;
}
Average = (Sum/1000);
// print results
Serial.print ( " Min = ");
Serial.print (Min);
Serial.print ( " Max = ");
Serial.print (Max);
Serial.print (" Average = ");
Serial.println (Average);
// delay(500); //Optional delay for readability
} //End
void loop()
{
Analog;
?
thankyou very much all the respected members...
your help means a lot....
Special thanks to DVDdoug for sharing the code...
I am gonna try this and will update regarding the results...I want to clarify one thing. Output printed value is the average of 1000 readings taken in 100 millisecond? It means you are getting 10 averaged values per second. Can you get these 10 values printed in Comm port results?
Can you share the Comm port output results?
DVDdoug:
Averaging is pretty simple... Add & divide.You can keep a running-total so you don't have to save all of the readings.
The [u]Smoothing Example[/u] gives you a moving average. As written, it averages 10 readings with a 1ms delay but you can change that. The moving average does require you to store a number of readings in an array.
Does it depend on the PC (data logger) speed that how much values can be averaged and communicated through serial port? I mean if I want to log an avg of 1000 cycles per second or 1kHz, are there any arduino or PC requirements?
AWOL:
void loop()
{
Analog;
?
Right, that should be:
int Analog;
It means you are getting 10 averaged values per second.
No. The code directly adds-up 1000 readings then divides by 1000. That turns-out to take about 1/10th of a second. (I took out the timer code. I had a timer because I was curious about how fast it was running but I really didn't need it.)
I don't keep track of multiple readings, I just keep track of the sum. (The smoothing example does save the last 10 readings in an array.)
Can you get these 10 values printed in Comm port results?
You can "print out" any values/variables you like and see them on the Serial Monitor. (Open the Serial Monitor by clicking the magnifying glass icon in the upper-right corner of the Arduino IDE.)
DVDdoug:
Right, that should be:
int Analog;
Or
Analog = 0; // reset value
Analog was already declared globally.
leo..