# How to store the average values of the sensor data in SD card?

for (n=1; n<11; n++)
{
avg=(avg+q)/n;
}

For this ‘for’ loop ‘n++’ is triggered when the next sensor data(q) comes, and I need help in how to increment ‘n’ when next data comes. When n=11, the avg. value will get printed in the SD card. Please help!!

``````if (newDataIsAvailable)
{
calculate average
increment n
if n equals 11
save the average to the SD card
initialise the average to zero
initialise n to zero
end if
}
``````

How will you determine that new data is available ?

I have a data detection function, which I would use. Thanx for the tip UKHeliBob.

A divide operation is one of the slowest math operations you can do. Is there any reason not to use:

``````#define SAMPLES      10    // Put at top of program...
// A bunch of code...

for (n = 0, avg = 0; n < SAMPLES; n++)
{
avg += q;
}
avg = avg / SAMPLES;   // You could use /= operator, but this is easier to read
``````

Also, is there a reason for not storing the average in EEPROM?