# taking average of n sample

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

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 :

``````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
add the value to the total
wait a little while
keep going round the loop
average = total/limit
``````

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.

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

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.

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

``````uint16_t readAnalog(uint8_t port)
{
uint16_t x, samples;
uint32_t value;
value = 0; // init accumulator;

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

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:

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

void loop(void)
{
uint16_t value;