Real-Time Clock (RTC)

This library allows users to get a clock after setting the year, month, date, hour, minutes, and seconds. To use this library, create an instance like "RTC rtc" and describe #include <RTC.h>

begin

Description
Initialize RTC.
Syntax
bool rtc.begin()
Parameters
None
Returns
True or False

end

Description
Stop RTC.
Syntax
bool rtc.end()
Parameters
None
Returns
True or False

setDateTime

Description
Set a start time.
Syntax
bool rtc.setDateTime(int year, int mon, int day, int hour, int min, int sec, int week)
Parameters
year: Year
mon: Month
day: Day
hour: Hour
min: Minute
sec: Second
week: Use the following parameters.
RTC_WEEK_SUNDAY
RTC_WEEK_MONDAY
RTC_WEEK_TUESDAY
RTC_WEEK_WEDNESDAY
RTC_WEEK_THURSDAY
RTC_WEEK_FRIDAY
RTC_WEEK_SATURDAY
Returns
True or False

getDateTime

Description
Get a current time.
Syntax
bool rtc.getDateTime(int &year, int &mon, int &day, int &hour, int &min, int &sec, int &week)
Parameters
Variables are the same as setDateTime.
Returns
True or False

attachAlarmHandler

Description
Register a function when an alarm occurs.
Syntax
void rtc.attachAlarmHandler(void (*function)(void))
Parameters
function: Function to be called when an alarm occurs
Returns
None

setAlarmTime

Description
Set an alarm time.
Syntax
bool rtc.setAlarmTime (int hour, int min, int week_flag)
Parameters
hour: Hour
min: Minute
week_flag: Use the following week parameters:
RTC_ALARM_SUNDAY
RTC_ALARM_MONDAY
RTC_ALARM_TUESDAY
RTC_ALARM_WEDNESDAY
RTC_ALARM_THURSDAY
RTC_ALARM_FRIDAY
RTC_ALARM_SATURDAY
RTC_ALARM_EVERYDAY
Returns
True or False

alarmOn

Description
Set alarm on.
Syntax
rtc.alarmOn()
Parameters
None
Returns
None

alarmOff

Description
Set alarm off
Syntax
rtc.alarmOff()
Parameters
None
Returns
None

Sample Program

This program is for setting a time and alarm. Time and alarm notification is output by a flashing LED.


#include <Arduino.h>
#include <RTC.h>
 
RTC rtc;
void alarm_handler();
  
void setup()
{
  Serial.begin(9600);
  pinMode(PIN_LED_GREEN, OUTPUT); //green LED for alarm
  pinMode(PIN_LED_RED, OUTPUT); //red LED for error
  digitalWrite(PIN_LED_GREEN, LOW); //turn off
  digitalWrite(PIN_LED_RED, LOW); //turn off
  
  if(!rtc.begin()){
    digitalWrite(PIN_LED_RED, HIGH); // error
  }  
  rtc.setDateTime(2016, 9, 22, 23, 20, 50, RTC_WEEK_SATURDAY);
  
  rtc.attachAlarmHandler(alarm_handler);
  rtc.setAlarmTime(23, 21, RTC_ALARM_EVERYDAY);
  rtc.alarmOn();
  
}
  
void loop()
{
  int year, mon, day, hour, min, sec, week;
  rtc.getDateTime(year, mon, day, hour, min, sec, week);
  
  Serial.print(year, DEC);Serial.print("/");
  Serial.print(mon, DEC); Serial.print("/");
  Serial.print(day, DEC); Serial.print(" ");
  Serial.print(hour, DEC); Serial.print(":");
  Serial.print(min, DEC); Serial.print(":");
  Serial.println(sec, DEC);
  
  delay(500);
  
}
  
void alarm_handler()
{
  digitalWrite(PIN_LED_GREEN, HIGH); //blue led for alarm
}