Hello Guys
here is my code for setting alarams
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h> //added jim ************************
#include <DS1307RTC.h> //added jim ***************************
void setup()
{
Serial.begin(9600);
Serial.println("In setup...."); //added jim *****************
/*
using rtc, sync system time to rtc
setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011 //old way *****************
*/
// following lines added to set time from rtc, took from timeRtcSet example, added jim *****************
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// end of setting the time ******************************
// create the alarms
Alarm.alarmRepeat(11, 30, 30,led13OFF);
Alarm.alarmRepeat(11, 30, 00,led13ON);
//Alarm.alarmRepeat(11,16,0, MorningAlarm);
/*
Alarm.alarmRepeat(10, 35,45, MorningAlarm);
Alarm.alarmRepeat(10,36,0, MorningAlarm);
Alarm.alarmRepeat(10, 36,30, MorningAlarm);
Alarm.alarmRepeat(10,37,0, MorningAlarm);
Alarm.alarmRepeat(10, 15,30, MorningAlarm);
Alarm.alarmRepeat(10,16,0, MorningAlarm);
Alarm.alarmRepeat(10,16,30, MorningAlarm);
Alarm.alarmRepeat(10,17,0, MorningAlarm);
Alarm.alarmRepeat(10, 17,30, MorningAlarm);
Alarm.alarmRepeat(17,45,0,EveningAlarm); // 5:45pm every day
Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm); // 8:30:30 every Saturday
*/
//Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
//Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.print("Turning pin 13 LED off at ");
digitalClockDisplay();
Serial.println("Ending setup...."); //added jim *****************
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000);
}
// functions to be called when an alarm triggers:
void led13ON(){
//Serial.print("Turning pin 13 LED on at ");
//digitalClockDisplay();
digitalWrite(13, HIGH);
}
void led13OFF(){
//Serial.print("Turning pin 13 LED off at ");
//digitalClockDisplay();
digitalWrite(13, LOW);
}
void MorningAlarm(){
digitalClockDisplay();
Serial.println("Alarm: - turn lights off Jimbo");
}
void EveningAlarm(){
Serial.println("Alarm: - turn lights on");
}
void WeeklyAlarm(){
Serial.println("Alarm: - its Monday Morning");
}
void ExplicitAlarm(){
Serial.println("Alarm: - this triggers only at the given date and time");
}
void Repeats(){
Serial.println("15 second timer");
}
void OnceOnly(){
Serial.println("This timer only triggers once");
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
This is my code for setting time from ds1307 lib
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
at first i am able to sync time from RTC but after the power goes down my time get reset to 00