I needed to add the normal Date/Time stamp to the SD CARD files my project creates, to ensure my final product will impress customers as being professional, rather than seeming "hobbyist". But the forum topics I found for the purpose require more libraries which in turn require initialization statements; all of which consumes more of the Uno's limited memory. In addition, comments on those topics revealed those libraries only work for some hardware/software versions, leaving more questions than answers.
So.... using some code provided by Nick Gammon and some by fat16lib, I created a sketch that only needs the SD.h library already running your SD Card. I hand-entered a "fake" time/date, so you can easily test the sketch. But to make it useful, you'll need to add code to retrieve the real time/date from whatever hardware source you are using. In my project, I'm using this realtime clock.
And here's my test sketch:
#include <SD.h>
File file;
const uint8_t CS = 10; // SD chip select
// YOUR SKETCH SHOULD UPDATE THESE SIX VALUSE
// EACH TIME BEFORE CREATING YOUR SD CARD FILE
unsigned int year = 2015;
byte month = 6;
byte day = 18;
byte hour = 7;
byte minute = 8;
byte second = 9;
void dateTime(uint16_t* date, uint16_t* time)
{
*date = FAT_DATE(year, month, day);
*time = FAT_TIME(hour, minute, second);
}
void setup()
{
Serial.begin(9600);
// THIS LINE SETS YOUR SKETCH TO SAVE YOUR
// TIME AND DATE INTO ANY FILE YOU CREATE.
SdFile::dateTimeCallback(dateTime);
if (!SD.begin(CS))
{
Serial.println("SD.begin failed");
while(1);
}
file = SD.open("TEST_IT.TXT", FILE_WRITE);
file.close();
Serial.println("Done");
}
void loop() {}