#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
const int led = 7;
void setup() {
// prepare pin as output
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
// wait for Arduino Serial Monitor
while (!Serial) ;
// get and set the time from the RTC
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// to test your project, you can set the time manually
setTime(01,02,00,10,3,24); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms, to trigger functions at specific times
//Alarm.alarmRepeat(06,59,50,MorningAlarm); // 9:00am every day
//Alarm.alarmRepeat(06,60,0,EveningAlarm); // 19:00 -> 7:00pm every day
//Alarm.alarmRepeat(dowSaturday,06,59,50,WeeklyAlarmON); // 8:30:30 every Saturday
//Alarm.alarmRepeat(dowSaturday,06,59,55,WeeklyAlarmOFF); // 8:30:30 every Saturday
Alarm.alarmRepeat(dowSunday,18,59,50,WeeklyAlarmON); // 8:30:30 every Saturday
Alarm.alarmRepeat(dowSunday,18,59,55,WeeklyAlarmOFF); // 8:30:30 every Saturday
Alarm.alarmOnce(01,02,03,ExplicitON);
}
void loop() {
digitalClockDisplay();
// wait one second between each clock display in serial monitor
Alarm.delay(1000);
}
// functions to be called when an alarm triggers
void MorningAlarm() {
// write here the task to perform every morning
Serial.println("Tturn light off");
digitalWrite(led, LOW);
}
void EveningAlarm() {
// write here the task to perform every evening
Serial.println("Turn light on");
digitalWrite(led, HIGH);
}
void WeeklyAlarmON() {
// write here the task to perform every evening
Serial.println("Turn light on");
digitalWrite(led, HIGH);
}
void WeeklyAlarmOFF() {
// write here the task to perform every evening
Serial.println("Turn light off");
digitalWrite(led, LOW);
}
void ExplicitON() {
// write here the task to perform every evening
Serial.println("Turn light on");
digitalWrite(led, HIGH);
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print("Time= ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print("Date= ");
Serial.print(day());
printNumber(month());
printNumber(year());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
void printNumber(int number) {
Serial.print("/");
if (number < 10)
Serial.print('0');
Serial.print(number);
}