Hardware Reference
In-Depth Information
second = String(datetime.second(), DEC);
//Concatenate the strings into date and time
date = year + "/" + month + "/" + day;
time = hour + ":" + minute + ":" + second;
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("log.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(date);
dataFile.print(F(","));
dataFile.print(time);
dataFile.print(F(","));
dataFile.println(dataString);
dataFile.close(); //Data isn't actually written until we
//close the connection!
//Print same thing to the screen for debugging
Serial.print(date);
Serial.print(F(","));
Serial.print(time);
Serial.print(F(","));
Serial.println(dataString);
}
else
{
Serial.println(F("Couldn't open log file"));
}
delay(refresh_rate);
}
The RTC library is imported by the sketch via #include "RTClib.h" and an
RTC object is created with RTC_DS1307 RTC; . The RTC is an I 2 C device, and relies
on the Wire library, so that needs to be included, too. This is the same library
you used in Chapter 8, “The I 2 C Bus.” In setup() , RTC.isrunning() checks to
see if the RTC is not already running. If it isn't, the date and time are set based
on the compile time, determined by your computer's clock. Once this is set, the
time will not be reset as long as the battery stays connected to the RTC. Also
in setup() , a column header is inserted into the log file, adding a note that the
logging has been restarted. This is useful for appending to the log file each time
you restart the system.
During each pass through the loop, the datetime object is set to the current
date and time. You can then extract the year, month, hour, and so on from this
object and convert them to strings that you can concatenate into the date and
Search WWH ::




Custom Search