File creation date with SD.h for the Data Logging Shield

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

File creation date with SD.h for the Data Logging Shield

Post by fat16lib »

Here is an example that timestamps files created on the Data Logging Shield using SD.h and RTClib.

This example will create the file "TEST_SD.TXT" with creation time and date from the DS1307.

Code: Select all

#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() {}

mboehm
 
Posts: 9
Joined: Fri Jun 06, 2014 12:46 pm

Re: File creation date with SD.h for the Data Logging Shield

Post by mboehm »

After searching a bit, I ended up poking at the SdFat library code to see how to enable file timestamps. I ended up writing this exact same code (the comments in SdFat.h are MUCHO helpful). Shouldn't this functionality be added to the adafruit SD library (seems trivial)?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”