Go Down

Topic: Which SD library to create file with date? (Read 27698 times) previous topic - next topic

betterthanalemur

Still works in Arduino 1.0.6, Thanks so much!!!

Code: [Select]

// Just like the above posts
// after your rtc is set up and working you code just needs:

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());
}

//put that outside of any functions, before your 'setup'

//Then inside your 'loop' or 'setup'

void loop(void)
{

  //Other code that does stuff

  //put this next line *Right Before* any file open line:
  SdFile::dateTimeCallback(dateTime);
  dataFile = SD.open(datafilename, FILE_WRITE);

  //other code that does stuff

}






Thanks so much!!!!!!!!!!!!!!!!!!!!!!

hipotenuso13

Amazing, thanks for this simple and clean code!!!

postmaster-ino

#17
Nov 10, 2019, 05:50 pm Last Edit: Nov 10, 2019, 05:51 pm by postmaster-ino
Arduino IDE 1.8.8 - works today too!
Thank you very much - only 4 years later i found a solution without a known problem.
Now it works fine with actual date/time on sd-card.

Sorry for digging out the body

Still works in Arduino 1.0.6, Thanks so much!!!

Code: [Select]

// Just like the above posts
// after your rtc is set up and working you code just needs:

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());
}

//put that outside of any functions, before your 'setup'

//Then inside your 'loop' or 'setup'

void loop(void)
{

  //Other code that does stuff

  //put this next line *Right Before* any file open line:
  SdFile::dateTimeCallback(dateTime);
  dataFile = SD.open(datafilename, FILE_WRITE);

  //other code that does stuff

}






Thanks so much!!!!!!!!!!!!!!!!!!!!!!

Go Up