Datasheet

create LOGGER00.CSV and incrementing every time when the file already exists, until we
get to LOGGER99.csv, we basically make a new file every time the Arduino starts up
To create a file, we use some Unix style command flags which you can see in the
logfile.open() procedure. FILE_WRITE means to create the file and write data to it.
Assuming we managed to create a file successfully, we print out the name to the Serial
port.
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,time,light,temp");
#if ECHO_TO_SERIAL
Serial.println("millis,time,light,temp");
#if ECHO_TO_SERIAL// attempt to write out the header to the file
if (logfile.writeError || !logfile.sync()) {
error("write header");
}
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
// If you want to set the aref to something other than 5v
//analogReference(EXTERNAL);
}
OK we're wrapping up here. Now we kick off the RTC by initializing the Wire library and
poking the RTC to see if its alive.
Then we print the header. The header is the first line of the file and helps your spreadsheet
or math program identify whats coming up next. The data is in CSV (comma separated
value) format so the header is too: "millis,time,light,temp" the first item millis is milliseconds
since the Arduino started, time is the time and date from the RTC, light is the data from
the CdS cell and temp is the temperature read.
You'll notice that right after each call to logfile.print() we have #if ECHO_TO_SERIAL and
a matching Serial.print() call followed by a #if ECHO_TO_SERIAL this is that debugging
output we mentioned earlier. The logfile.print() call is what writes data to our file on the SD
card, it works pretty much the same as the Serial version. If you set ECHO_TO_SERIAL to
be 0 up top, you won't see the written data printed to the Serial terminal.
© Adafruit Industries https://learn.adafruit.com/adafruit-data-logger-shield Page 79 of 85