Example
This example uses the external TimeLib, DS3232RTC and LiquidCrystal_I2C libraries found here. Please install them and restart the Arduino IDE before trying to compile.
#define MY_DEBUG
#define MY_RADIO_RF24
#include <SPI.h>
#include <MySensors.h>
#include <TimeLib.h>
#include <DS3232RTC.h> // A DS3231/DS3232 library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
bool timeReceived = false;
unsigned long lastUpdate=0, lastRequest=0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
setSyncProvider(RTC.get);
requestTime();
lcd.begin(16,2);
}
void presentation() {
sendSketchInfo("RTC Clock", "1.0");
}
void receiveTime(unsigned long controllerTime) {
Serial.print("Time value received: ");
Serial.println(controllerTime);
RTC.set(controllerTime);
timeReceived = true;
}
void loop()
{
unsigned long now = millis();
if ((!timeReceived && (now-lastRequest) > (10UL*1000UL))
|| (timeReceived && (now-lastRequest) > (60UL*1000UL*60UL))) {
Serial.println("requesting time");
requestTime();
lastRequest = now;
}
if (now-lastUpdate > 1000) {
updateDisplay();
lastUpdate = now;
}
}
void updateDisplay(){
tmElements_t tm;
RTC.read(tm);
lcd.home();
lcd.print(tm.Day);
lcd.print("/");
lcd.print(tm.Month);
lcd.print(" ");
printDigits(tm.Hour);
lcd.print(":");
printDigits(tm.Minute);
lcd.print(":");
printDigits(tm.Second);
lcd.setCursor ( 0, 1 );
lcd.print("Temp: ");
lcd.print(RTC.temperature()/4);
lcd.write(223);
lcd.print("C");
}
void printDigits(int digits){
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}