Pages: [1]   Go Down
Author Topic: taking average of n sample  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
DELHI
Offline Offline
God Member
*****
Karma: 0
Posts: 600
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

AMPS

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3462
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 4
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 23
Posts: 665
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.
« Last Edit: January 14, 2013, 11:30:31 am by Krupski » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3462
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: