Go Down

Topic: taking average of n sample (Read 832 times) previous topic - next topic

AMPS-N

Can some one suggest me simple code for taking average of n sample of arduino  analog signal.

assume that i have analog sensor, whenever i very output, the reading should get recorded and should return average of n sample recorded

AMPS

UKHeliBob

Have you got any code to read the analog value and output the average ?
Can you please post it here - please put it between code tags to make it readable.  Read this for advice http://arduino.cc/forum/index.php/topic,97455.0.html

If you can read the analog value then getting the average over a number of readings is simple

In pseudo-code :

Code: [Select]
set total to 0
set counter to 0
set limit to the number of times to read the value

while counter not equal to limit
  read the current value
  add the value to the total
  add 1 to the counter
  wait a little while
keep going round the loop
average = total/limit
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Quote
assume that i have analog sensor, whenever i very output, the reading should get recorded and should return average of n sample recorded

What's the problem. Take n readings, adding them up. Divide by n.

ea123


Can some one suggest me simple code for taking average of n sample of arduino  analog signal.

assume that i have analog sensor, whenever i very output, the reading should get recorded and should return average of n sample recorded




Hi,
consider using the moving average (http://en.wikipedia.org/wiki/Moving_average) if you want to continously compute the average of the last N samples.


Krupski

#4
Jan 14, 2013, 05:19 pm Last Edit: Jan 14, 2013, 05:30 pm by Krupski Reason: 1

Can some one suggest me simple code for taking average of n sample of arduino  analog signal.

assume that i have analog sensor, whenever i very output, the reading should get recorded and should return average of n sample recorded




Code: [Select]

uint16_t readAnalog(uint8_t port)
{
       uint16_t x, samples;
       uint32_t value;
       value = 0; // init accumulator;
       samples = 1000; // adjust this to your liking

       for(x = 0; x < samples; x++) {
               value += analogRead(port);
       }

       return (value / samples);
}


(edit to add):

If you want to do something like update a display ONLY when the Analog value CHANGES, then use this code as well:

Code: [Select]


uint16_t old_value = -1; // insure first time around updates

void loop(void)
{
       uint16_t value;
       value = readAnalog(A5); // get averaged analog reading

       if(value != old_value) { // if it changed
               // do something with the new value here
               old_value = value; // update the changed value
       }
}


Hope this helps.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

UKHeliBob

You are already getting the average of 1000 samples (actually 1001)

As the comment in the code says, "adjust this to your liking"
Is the number of samples to be taken fixed or will it change within the program ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up