Writing time inconsistencies
Hi,
I've been writing a data logger application which basically gathers info every 100ms, displays it on a screen and appends a line in a CSV file. The application runs on an Arduino Pro Mini 3.3V@8MHz, coupled with an INA219 breakout board, an SSD1306 oled display and an Adafruit SD breakout.
I don't really need the SD writing to be super fast, I just need it to be formatted as CSV and I need the timings to be consistent. Gathering the information on the INA219 via I²C takes a maximum of 11ms, and refreshing the OLED screen via I²C takes a maximum of 72ms, for a total of 83ms, which leaves 17ms max of spare for the program to write a line in the CSV file, which should be plenty enough (each line consists of a long integer and two floats).
I've conducted test timings, and got these results : https://drive.google.com/file/d/1SH9kUl9Xmr5aU9VTeIhbuK1IXiJ49pkx/view?usp=sharing
The data shows most of the timings remain under 17ms, which is fine, but over 10% of measurments are higher, sometimes reaching 103 ms.
Here is where I declare the variables :
uint8_t cycles = 0;
const uint8_t chipSelect = 10;
SdFat sd;
SdFile measurFile;
Here is the SSD setup() code :
sd.begin(chipSelect);
measurFile.open("MEAS.csv", O_WRITE | O_CREAT | O_TRUNC);
measurFile.print("Time,Voltage,Current\n");
measurFile.sync();
Here is the writeFile() function, which is called each time 100ms have passed, and which writes a line : ` void writeFile() { char buf[32], voltbuf[8]={0}, curbuf[8]={0};
//prepare buffers with the voltage and current values in strings
dtostrf(loadvoltage, 6, 3, voltbuf);
dtostrf(current_mA, 6, 3, curbuf);
//format a csv line : time,voltage,current\n
sprintf(buf, "%ld,%s,%s\n", elapsed, voltbuf, curbuf);
//write the line in the file
measurFile.write(buf);
//after 9 cycles (1 sec.), apply SD buffer changes to file in SD
if(cycles >=9)
measurFile.sync();
//increment cycles count + reset to 0 after 10 cycles
cycles++;
cycles %= 10;
} `
A timer interrupt sets a boolean flag every 100ms, and the calls are made in the main loop when said flag is set. Except for I²C, SPI, Timer1 interrupt and Serial, nothing is used (and I will get rid of Serial once this issue is fixed)
I looked at other forums and the example codes, and don't understand what I've done wrong. I understand the buffer management is paramount, and attempt to flush it every second (every 10 lines written) as a compromise.
Would you have an idea of why those inconsistencies?
SD card are written in 512 byte blocks. When you call sync, you force a write of the current data block, a read of a directory block, update of the directory entry, write of the directory entry. The next write requires a read of the data block.
So your call to sync forces an unnecessary 2048 bytes of I/O to the SD. Sync should only be called when you want insure the the file will be closed if a crash occurs.
Writing an SD block can occasionally require almost 100 ms on some cards due to wear leveling operations in the SD.
You will need to buffer data if you want to consistently write data to a file at an exact interval of 100 ms.
See this example in SdFat-beta.