I have used Arduino Time library and prepared an example sketch for you.
Enjoy!
Lukas
Code: Select all
#include <Time.h>
#include <TimeLib.h> /* https://playground.arduino.cc/Code/Time/ */
#include <SPI.h>
#include <Controllino.h> /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */
/*
CONTROLLINO - Demonstration of proper Real Time Clock usage, Version 01P01
IMPORTANT INFORMATION!
Please, select proper target board in Tools->Board->Controllino MINI/MAXI/MEGA before Upload to your CONTROLLINO.
Do not forget to properly setup the mechanical switch at your CONTROLLINO MINI!
https://controllino.biz/
(Check https://github.com/CONTROLLINO-PLC/CONTROLLINO_Library for the latest CONTROLLINO related software stuff.)
*/
void setup() {
Serial.begin(9600);
Controllino_RTC_init();
/* set time and date by separate values values to the RTC chip */
/* Day, WeekDay, Month, Year, Hour, Minute, Second); */
// Controllino_SetTimeDate(4,5,9,20,11,13,00);
/* or use another possibility and define the time and date by strings, e.g. "Nov 15 2018", "11:41:02" */
/* following example uses predefined C macros __DATE__ and __TIME__ which represent compilation time */
// Controllino_SetTimeDateStrings(__DATE__, __TIME__); /* set compilation time to the RTC chip */
syncSystemTimeWithRTC(); /* initial synchronization */
}
// the loop function runs over and over again forever
void loop() {
syncSystemTimeWithRTC(); /* performs the sync once per day */
digitalClockDisplay();
delay(5000);
}
int syncSystemTimeWithRTC()
{
unsigned char mHours, mMinutes, mSeconds, mDays, mMonths, mYears, mWeekday;
char mStatus;
static unsigned char mLastSyncDay = 0;
if (mLastSyncDay == 0 || mLastSyncDay != day())
{
/* re-sync the system clock with the RTC for the first time and then once per day at midnight */
mStatus = Controllino_ReadTimeDate( &mDays, &mWeekday, &mMonths, &mYears, &mHours, &mMinutes, &mSeconds);
if(mStatus != 0)
{
Serial.println("Error - RTC is not responding!");
return -1;
}
else
{
setTime(mHours,mMinutes,mSeconds,mDays,mMonths,mYears); // Set system time
if(timeStatus()!= timeSet)
{
Serial.println("Error - Unable to sync with the RTC!");
return -1;
}
else
{
Serial.println("System time was synchronized with the RTC.");
}
}
mLastSyncDay = mDays; /* set the day of the last synchronization with the RTC */
}
return 0;
}
/* following functions are taken from example TimeRTC (Time library ) */
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/* END OF THE SKETCH */