Go Down

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

#### AJITnayak

##### Jan 14, 2013, 02:09 pm
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

#1
##### Jan 14, 2013, 02:31 pm
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 0set counter to 0set limit to the number of times to read the valuewhile counter not equal to limit  read the current value  add the value to the total  add 1 to the counter  wait a little whilekeep 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

#2
##### Jan 14, 2013, 02:32 pm
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.
The art of getting good answers lies in asking good questions.

#### ea123

#3
##### Jan 14, 2013, 04:00 pm

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 pmLast 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);}`

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 updatesvoid 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

#5
##### Jan 14, 2013, 05:34 pm
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