Setting an Alarm with RTC

Hi guys, I am having trouble finding how to set an alarm with the DS1307 RTC. I want to run a function at a given time and all I can find is the TimeAlarms library, which doesn't use the RTC but instead uses the time given by the Time library. So being able to call a function when the RTC says it's that time and runs is all I want it to do. Any help is appreciated.

Thanks, Ken

1 Like

http://www.instructables.com/id/Arduino-alarm-clock/step2/Step-2-check-the-function-of-the-Phi-1-shield-with/here code in zip file to set alaram

#include <Time.h>
#include <TimeAlarms.h>

void setup()
{
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms 
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  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 
}

void  loop(){  
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
  Serial.println("Alarm: - turn lights off");    
}

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);
}

code to generate alaram

Hi-

You need to sync with the RTC.

This code uses the alarm library AND an RTC.

Jim

/*
 * TimeAlarmExample.pde
 *
 * This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
 * and simulates turning lights on at night and off in the morning
 * A weekly timer is set for Saturdays at 8:30:30
 *
 * A timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 *
 * System time now set from RTC, jim august 2014
 * remember to set the rtc first...
 * added wire and ds1307rtc libraries
 */

#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);
}

AMPS-N:
here code in zip file to set alaram

AMPS I'll bet that's the code the OP's using, but it has nothing to do with RTC. See my post above, which syncs the system time with the RTC.

Ya thanks for information. My first link to configure with RTC & LCD shield.
I wounder in which situation we use this type of configuration / sync with PC,We use when there is real time application.

Hi JimboZA, I gave that code a try and it says "Unable to sync with the RTC" and starts counting from 0:00:00. I checked my wiring and it works with other sketches, so any ideas why this is happening. Thanks

I checked my wiring and it works with other sketches, so any ideas why this is happening. Thanksit working with which sketh, post your code .
the code which are working use it .

Kenn13542:
Hi JimboZA, I gave that code a try and it says "Unable to sync with the RTC" and starts counting from 0:00:00. I checked my wiring and it works with other sketches, so any ideas why this is happening. Thanks

Hmmmmm....... <<<<< that's a "no" 8)

I don't recall having any trouble when i put that posted sketch together, iirc it worked straight out the blocks.

And if the RTC is working with other sketches it must be good.

You could retro-fit the time synch stuff to an existing sketch which works?- then add the alarms.