# NIneteenth data sample always different

Hello,

I have code that monitors an analogue pin for a period of time, averaging the reading and calculating the variance, and also the proportion of the readings that represent a saturated input.

``````void averageReading(float *mean, float *varn, float *OoBFrac, int avgTime) {
// read STRAIN_GAUGE_PIN for avgTime ms and report mean, variance and proportion of out-of-bounds readings

unsigned long S = 0;           // accumulation of readings
unsigned long S2 = 0;          // accumulation of squared readings
unsigned int OoBCount = 0;     // count of readings equal to zero
unsigned int count = 0;        // count of all readings
int x;

unsigned long start = millis();
unsigned long now = start;
while (now - start < avgTime) {
if (x <= SAT_THRESH_LO || x >= SAT_THRESH_HI) {
OoBCount += 1;
}
S += (unsigned long)x;
S2 += (unsigned long)x * x;
count += 1;
now = millis();
}

*mean = (float)S / count;
*varn = sqrt((float)S2 / count - *mean * *mean);
*OoBFrac = (float)OoBCount / count;
}
``````

Each reading has a little bit of scaling applied, and then is written out to a CSV file on an SD card.

``````      while (poll == LOW) {
timeNow = (float)(millis() - start) / 1000.0;
// rescale to real units
mean = (mean - currentCalibration.zeroOffset) * scaleFactor;
varn = varn * scaleFactor;
writeLine(timeNow, mean, varn, multiText);
logFile.println(multiText);
}
``````

The recorded data looks normal, except every 19th reading is higher than all the surrounding readings and the indicated variance is much larger. It appears to be the 19th reading regardless of the averaging time. E.g. if the averaging time is about 100 ms, the defect appears every 1.9 s, but if the averaging time is about 50 ms, the defect appears very 0.95 s. Also, the time recorded on those lines advances by about 7 or 8 ms more than normal lines.

Does the SD library accumulate printed data into a buffer, and only transfer after accumulating 512 bytes? And even if it does, and that takes 7 or 8 ms every 19 lines, why is it affecting the reading of the analogue pin for the next line?

Thanks,

Neil

Could be a buffering issue. As you posted only snippets of your code I'll take your word for 19 readings being 512 bytes written in total. Maybe the SD card and/or your SD card code block for a moment as the card is writing the data??