choochoo22
Active member
I've been using the SD library with a project, mostly successfully, but I would like to add time stamps to files created and can't seem to find a way to do that with SD. So... I'm now trying to switch to SdFat.
The idea is first to adapt the SdFat TeensyRtcTimestamp example to the minimum required to create a file with a timestamp on a Teensy 3.5 and write text to the file, then apply that technique to the final project. Several problems have occurred adapting the example:
The idea is first to adapt the SdFat TeensyRtcTimestamp example to the minimum required to create a file with a timestamp on a Teensy 3.5 and write text to the file, then apply that technique to the final project. Several problems have occurred adapting the example:
- Both the example and my edited version generate a list of errors as shown below, but they run anyway. Why the errors and how to get rid of them?
- Among the errors is a warning to use TimeLib.h, not Time.h, but both the example and my code already use TimeLib.h so why the warning?
Code:
In file included from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\sys\stat.h:9:0,
from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\sys\_default_fcntl.h:188,
from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\sys\fcntl.h:4,
from c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\fcntl.h:1,
from c:\users\rick\documents\arduino\libraries\arduino_118105\src\common\fsapiconstants.h:30,
from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/ExFatLib/ExFatFile.h:36,
from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/ExFatLib/ExFatVolume.h:28,
from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/ExFatLib/ExFatLib.h:27,
from C:\Users\Rick\Documents\Arduino\libraries\arduino_118105\src/SdFat.h:33,
from C:\Users\Rick\Documents\Arduino\Experimental\SdFat_create_with_time_stamp\SdFat_create_with_time_stamp.ino:1:
C:\Users\Rick\Documents\Arduino\libraries\Time-master/time.h:1:2: warning: #warning "Please include TimeLib.h, not Time.h. Future versions will remove Time.h" [-Wcpp]
#warning "Please include TimeLib.h, not Time.h. Future versions will remove Time.h"
^
Sketch uses 33404 bytes (6%) of program storage space. Maximum is 524288 bytes.
Global variables use 5564 bytes (2%) of dynamic memory, leaving 256572 bytes for local variables. Maximum is 262136 bytes.
- The example sketch successfully creates a file with a correct time stamp. My sketch, shown below, creates a file with a blank timestamp. Not default, blank. How to get the correct time stamp?
- The example successfully prints text to the file, my sketch attempts to print but the created file is empty.
- This sketch executes a file.close() but once this process is working the final project will create a lengthy log file that won't get closed. The SD library has a file.flush() command for that situation that writes data to the file without closing it. How can this be done with SdFat?
Code:
#include <SdFat.h>
#include <TimeLib.h>
#define SD_CONFIG SdioConfig(FIFO_SDIO)
SdFs sd;
FsFile file;
//------------------------------------------------------------------------------
// Call back for file timestamps. Only called for file create and sync().
void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
// Return date using FS_DATE macro to format fields.
*date = FS_DATE(year(), month(), day());
// Return time using FS_TIME macro to format fields.
*time = FS_TIME(hour(), minute(), second());
// Return low time bits in units of 10 ms.
*ms10 = second() & 1 ? 100 : 0;
}
void setup() {
setSyncProvider(getTeensy3Time);
Serial.begin(9600);
while(!Serial);
Serial.println(minute()); // This is just to be sure RTC actually sets the clock
// It does
#define fileName "Foo.txt"
// Set callback
FsDateTime::setCallback(dateTime);
sd.begin(SD_CONFIG);
if(sd.exists(fileName)) sd.remove(fileName);
sd.open(fileName, FILE_WRITE);
file.print("Mary had a little lamb.");
file.println();
file.close();
}
void loop() {
// put your main code here, to run repeatedly:
}
//------------------------------------------------------------------------------
time_t getTeensy3Time(){
return Teensy3Clock.get();
}
//------------------------------------------------------------------------------