USA, OH
Offline
Jr. Member
Karma: 0
Posts: 54
|
 |
« on: September 18, 2011, 03:41:31 pm » |
I have Adafruit datalogger shield with real-time clock. I need create files on SD with correct file creation dates. As I understand, SD library recommended by Adafruit cannot create files with dates. With so many SD libraries available which one I should stick to for creating a file with proper date?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Edison Member
Karma: 28
Posts: 1079
Arduino rocks
|
 |
« Reply #1 on: September 18, 2011, 05:16:05 pm » |
Sdfat http://code.google.com/p/sdfatlib/downloads/list supports timestamps on the Adafruit Data Logging Shield. Look at the AnalogLogger example in the SdFat/examples folder. Set USE_DS1307 nonzero at line 7 #define USE_DS1307 1 // set nonzero to use DS1307 RTC At line 40 change the comments like this: //#error remove this line and uncomment the next two lines. #include <Wire.h> #include <RTClib.h>
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 71
Posts: 6625
Arduino rocks
|
 |
« Reply #2 on: September 20, 2011, 08:59:20 am » |
Apropos of this, I had a go at using the file timestamp support in the Arduino 0022 SD library but couldn't figure it out - there are various functions in the source that suggest a current time callback is supported - is this so and if so how to use it?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Edison Member
Karma: 28
Posts: 1079
Arduino rocks
|
 |
« Reply #3 on: September 20, 2011, 10:08:18 am » |
The SD.h 0022 library is a wrapper for an old version of SdFat. The Arduino developers did not choose to expose timestamp callback support in their wrapper. Here is a sketch that works with their 0022 version of SD.h. It will produce a file, TEST_SD.TXT, that will be timestamped with creation and modify time. Modify time will be maintained if you write the file. #include <SD.h> #include <Wire.h> #include <RTClib.h>
File file; // test file const uint8_t SD_CS = 10; // SD chip select RTC_DS1307 RTC; // define the Real Time Clock object //------------------------------------------------------------------------------ // call back for file timestamps void dateTime(uint16_t* date, uint16_t* time) { DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields *date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields *time = FAT_TIME(now.hour(), now.minute(), now.second()); } //------------------------------------------------------------------------------ void setup() { Serial.begin(9600); Wire.begin(); if (!RTC.begin()) { Serial.println("RTC failed"); while(1); }; // set date time callback function SdFile::dateTimeCallback(dateTime);
if (!SD.begin(SD_CS)) { Serial.println("SD.begin failed"); while(1); } file = SD.open("TEST_SD.TXT", FILE_WRITE); file.close(); Serial.println("Done"); } //------------------------------------------------------------------------------ void loop() {}
|
|
|
|
« Last Edit: September 20, 2011, 10:10:32 am by fat16lib »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #4 on: December 05, 2011, 06:41:57 am » |
Many thanks for this info!. Lenny The SD.h 0022 library is a wrapper for an old version of SdFat. The Arduino developers did not choose to expose timestamp callback support in their wrapper. Here is a sketch that works with their 0022 version of SD.h. It will produce a file, TEST_SD.TXT, that will be timestamped with creation and modify time. Modify time will be maintained if you write the file. #include <SD.h> #include <Wire.h> #include <RTClib.h>
File file; // test file const uint8_t SD_CS = 10; // SD chip select RTC_DS1307 RTC; // define the Real Time Clock object //------------------------------------------------------------------------------ // call back for file timestamps void dateTime(uint16_t* date, uint16_t* time) { DateTime now = RTC.now();
// return date using FAT_DATE macro to format fields *date = FAT_DATE(now.year(), now.month(), now.day());
// return time using FAT_TIME macro to format fields *time = FAT_TIME(now.hour(), now.minute(), now.second()); } //------------------------------------------------------------------------------ void setup() { Serial.begin(9600); Wire.begin(); if (!RTC.begin()) { Serial.println("RTC failed"); while(1); }; // set date time callback function SdFile::dateTimeCallback(dateTime);
if (!SD.begin(SD_CS)) { Serial.println("SD.begin failed"); while(1); } file = SD.open("TEST_SD.TXT", FILE_WRITE); file.close(); Serial.println("Done"); } //------------------------------------------------------------------------------ void loop() {}
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Edison Member
Karma: 34
Posts: 1074
Arduino rocks; but with my plugin it can fly rocking the world ;-)
|
 |
« Reply #5 on: July 30, 2012, 09:02:46 am » |
Is this still the way to add timestamps to sd files with Arduino 1.0 or later? Best regards Jantje
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Edison Member
Karma: 28
Posts: 1079
Arduino rocks
|
 |
« Reply #6 on: July 30, 2012, 09:21:24 am » |
The version of SdFat used in 1.0 and 1.01 is the same old version used in 022 and before. Only the SD.h wrapper has changed so this is still the best way. If you had access to the SdFat API for files you could use the timestamp function to set all date/time fields on a per file basis. bool SdBaseFile::timestamp ( uint8_t flags, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second )
Set a file's timestamps in its directory entry.
Parameters: [in] flags Values for flags are constructed by a bitwise-inclusive OR of flags from the following list
T_ACCESS - Set the file's last access date.
T_CREATE - Set the file's creation date and time.
T_WRITE - Set the file's last write/modification date and time.
Parameters: [in] year Valid range 1980 - 2107 inclusive. [in] month Valid range 1 - 12 inclusive. [in] day Valid range 1 - 31 inclusive. [in] hour Valid range 0 - 23 inclusive. [in] minute Valid range 0 - 59 inclusive. [in] second Valid range 0 - 59 inclusive
Note: It is possible to set an invalid date since there is no check for the number of days in a month. Modify and access timestamps may be overwritten if a date time callback function has been set by dateTimeCallback().
Returns: The value one, true, is returned for success and the value zero, false, is returned for failure.
Or copy timestamps from another file: bool SdBaseFile::timestamp ( SdBaseFile * file )
Copy a file's timestamps
Parameters: [in] file File to copy timestamps from.
Note: Modify and access timestamps may be overwritten if a date time callback function has been set by dateTimeCallback().
Returns: The value one, true, is returned for success and the value zero, false, is returned for failure.
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Edison Member
Karma: 34
Posts: 1074
Arduino rocks; but with my plugin it can fly rocking the world ;-)
|
 |
« Reply #7 on: July 30, 2012, 09:29:08 am » |
fatlib Thanks for the great library and quick response. I'll try the callback first. if that doesn't satisfy my needs I'll look at using the fatlib natively.
Greetings from Belgium Jantje
|
|
|
|
|
Logged
|
|
|
|
|
Rapa Nui
Offline
God Member
Karma: 16
Posts: 889
Pukao hats cleaning services
|
 |
« Reply #8 on: July 30, 2012, 03:00:37 pm » |
FYI - I am using an external RTC, so I do this as well (I think I must not call RTC with each timestamp request then, comes from sdfat examples): ... void dateTime(uint16_t* date, uint16_t* time) { time_t t = now(); // return date using FAT_DATE macro to format fields *date = FAT_DATE(year(t), month(t), day(t)); // return time using FAT_TIME macro to format fields *time = FAT_TIME(hour(t), minute(t), second(t)); } ... setSyncProvider(rtc.get_tm); // the function to get the actual time from my RTC
if(timeStatus()!= timeSet) Serial.println(F("Unable to sync with the RTC")); else Serial.println(F("RTC has set the system time"));
setSyncInterval(1000); // set the number of seconds between re-sync of time
SdFile::dateTimeCallback(dateTime); ...
p.
|
|
|
|
« Last Edit: July 30, 2012, 03:10:57 pm by pito »
|
Logged
|
|
|
|
|
|
|
0
Offline
Edison Member
Karma: 28
Posts: 1079
Arduino rocks
|
 |
« Reply #10 on: July 31, 2012, 08:54:04 am » |
pito,
See my reply in your original post about RTC libs.
|
|
|
|
|
Logged
|
|
|
|
|
San Diego, California, USA
Offline
Newbie
Karma: 0
Posts: 7
Born to be a nerd
|
 |
« Reply #11 on: July 31, 2012, 02:39:18 pm » |
I have been using the Rogue Robotics RS-232 SD modules which don't do timestamps either. As a work-around I put year and then month sub-diectories on the SD (e.g. 12/07) before use and construct a filename for the day with its date in it (e.g. AR31.CSV). Each record has date and time fields. Most of the projects have used a dallas RTC, but i have also used a watch crystal and a CD4060 and even an actual watch (detecting the 1-sec current surge when it advances the hands). My latest does use the Adafruit logger shield, which is much cheaper, and its SD library. John
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Edison Member
Karma: 34
Posts: 1074
Arduino rocks; but with my plugin it can fly rocking the world ;-)
|
 |
« Reply #12 on: July 31, 2012, 04:32:02 pm » |
Fatlib It works great now. I'm using my gps module to get the time. Greetings from Belgium Jantje
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 31
|
 |
« Reply #13 on: August 09, 2012, 01:35:12 am » |
It has been so long I am not talking about arduino. I have a project with arduino and I will expose it yet. But, one part of the project (programmes) is to have a log file in SD card named by date.
|
|
|
|
|
Logged
|
|
|
|
|
|