I'm trying to get familiarized with Arduino by writing (yet another) data logger.
My first experiment was to use timer one to launch a function which opens a file, writes the current value of millis() and then closes it. Something like this:
#include <TimerOne.h>
#include <SD.h>
File myFile;
void setup()
{
...
Timer1.initialize();
Timer1.attachInterrupt(write_to_file, SAMPLING_PERIOD);
...
}
void write_to_file(void){
myFile = SD.open("experiment.xls", FILE_WRITE);
myFile.println(micros());
myFile.close();
}
Then I used OOCalc to calculate the difference between every two consecutive saved values (and therefore obtaining the actual "sampling rate", so to speak) . Some small inaccuracies appeared (as expected), but I'm surprised that:
a) The inaccuracies appear periodically.
b) If timerOne is set to a period of say 1 second, the saved values of micros() are always less than 1 second apart.
c) If the sampling rate is more or less high, values of micros are not always increasing (sometimes I found say 10000 before 9999) .
Here's an example for SAMPLING_RATE=10000000 (1s)
Any idea why this happens?
Thank you!
Using Arduino UNO, with a fat16 SD Card