The code below is the logging part of a much larger code of a data logger. Its working per se but there is one issue though ... when I open the recorded file in either Notepad or Excel I find empty rows in random . The logging interval is set for 500ms but in reality it happens about 10 to 20 millis more than 500 ms. That really is no issue as the data is used only for trend data plot.
But I wonder what is the reason for the gaps among records ... I have enclosed both the code and a snap shot of the Excel file. Hardware : Arduino Mega + 3.2" Touch TFT shield with SD Card interface from Waveshare.
// >>>>>>>>>>>>>>>>>>>>> LOG DATA TO DISK >>>>>>>>>>>>>>>>>>>>>>>>>
void logDataToSDC() {
if (f_logDataToDisk) {
// **************** WRITE HEADER DATA TO SDC ******************
if (f_writeHeaderData) {
char header[40];
f_writeHeaderData = false;
logFile = SD.open("FTPLOG.CSV", FILE_WRITE); // Open the file to write
if (logFile) {
sprintf(header, "Log_Start, %02d-%02d-20%02d , %02d:%02d:%02d", DayOfMonth, Month, Year, Hour, Minute, Second);
logFile.println(header);
logFile.println("MilliSecond, Flow(lpm) , Press(Bar) , Temp(DegC)"); // Column header data
logFile.close();
}
else {
tft.setTextSize (2);
tft.setTextColor(TFT_RED);
tft.fillRect(0, 0, 320, 25, TFT_WHITE);
print_at(10, 5, "SD CARD ERROR.NO LOGGING!"); // 25 char fill one line exact...
}
}
//**************** WRITE REAL TIME DATA TO SDC ****************
if ( millis() - loggingMs > loggingInterval ) // Time to write to SD card ??
{
int recInterval = millis() - loggingMs;
loggingMs = millis();
logFile = SD.open("FTPLOG.CSV", FILE_WRITE); // Open the file to write
char lpmData[10];
dtostrf(lpmValFloat, 6, 2, lpmData);
char pressData[10];
dtostrf(barValFloat, 6, 2, pressData);
char tempData[10];
dtostrf(tempValFloat, 6, 2, tempData);
if ( millis() - fileSizeMillis > fileSizeInterval )
{
fileSizeMillis = millis();
char fileKb[10];
sprintf(fileKb, "LOGGING. FileSize:%3dkB", logFile.size() / 1000);
tft.setTextSize (2);
tft.setTextColor(TFT_BLACK);
tft.fillRect(0, 0, 320, 25, TFT_GREEN);
print_at(10, 5, fileKb);
}
if (logFile) // Check if the file opened okay
{
logFile.print(recInterval); // Yes now write the data
logFile.print(',');
logFile.print(lpmData);
logFile.print(',');
logFile.print(pressData);
logFile.print(',');
logFile.println(tempData); // One row of data complete.
logFile.close(); // Write the data to SDC
}
else
{
tft.setTextSize (2);
tft.setTextColor(TFT_RED);
print_at(10, 5, " ");
print_at(10, 5, "SD CARD ERROR.NO LOGGING!"); // 25 char fill one line exact...
}
}
}
}
