// Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include #include #include #include #define CLOCK_ADDRESS 0x68 void RTC_write_register(unsigned char reg,unsigned char value) { Wire.beginTransmission(CLOCK_ADDRESS); Wire.send(reg); Wire.send(value); Wire.endTransmission(); } RTC_DS1307 RTC; void setup () { Serial.begin(9600); Wire.begin(); RTC.begin(); DateTime now = RTC.now(); DateTime compiled = DateTime(__DATE__, __TIME__); if (now.unixtime() < compiled.unixtime()) { Serial.println("RTC is older than compile time! Updating"); // following line sets the RTC to the date & time this sketch was compiled RTC.adjust(DateTime(__DATE__, __TIME__)); RTC_write_register(0x0B,0); RTC_write_register(0x0C,0x80); RTC_write_register(0x0D,0x80); RTC_write_register(0x0E,6); } } void loop () { DateTime now = RTC.now(); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); Serial.println(); delay(3000); }