Jump to content

How to create an Arduino alarm that calls a function

+ 2
  michael margolis's Photo
Posted May 16 2011 02:22 PM

You want to perform some action on specific days and at specific times of the day.

TimeAlarms is a companion library included in the Time library download (installing the Time library will also install the TimeAlarms library). TimeAlarms makes it easy to create time and date alarms:

 * TimeAlarmExample sketch
 * 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 timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 * At startup the time is set to Jan 1 2010  8:29 am

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

void setup()
  Serial.println("TimeAlarms Example");
  Serial.println("Alarms are triggered daily at 8:30 am and 17:45 pm");
  Serial.println("One timer is triggered every 15 seconds");
  Serial.println("Another timer is set to trigger only once after 10 seconds");

  setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010

  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day

  Alarm.timerRepeat(15, RepeatTask);            // timer for every 15 seconds
  Alarm.timerOnce(10, OnceOnlyTask);            // called once after 10 seconds

void MorningAlarm()
  Serial.println("Alarm: - turn lights off");

void EveningAlarm()
  Serial.println("Alarm: - turn lights on");

void RepeatTask()
  Serial.println("15 second timer");

void OnceOnlyTask()
  Serial.println("This timer only triggers once");

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

void digitalClockDisplay()
  // digital clock display of the time

void printDigits(int digits)
  // utility function for digital clock display: prints preceding colon and
leading 0
  if(digits < 10)

You can schedule tasks to trigger at a particular time of day (these are called alarms) or schedule tasks to occur after an interval of time has elapsed (called timers). Each of these tasks can be created to continuously repeat or to occur only once.

To specify an alarm to trigger a task repeatedly at a particular time of day use:

Alarm.alarmRepeat(8,30,0, MorningAlarm);

This calls the function MorningAlarm at 8:30 a.m. every day.

If you want the alarm to trigger only once, you can use the alarmOnce method:

Alarm.alarmOnce(8,30,0, MorningAlarm);

This calls the function MorningAlarm a single time only (the next time it is 8:30 a.m.) and will not trigger again.

Timers trigger tasks that occur after a specified interval of time has passed rather than at a specific time of day. The timer interval can be specified in any number of seconds, or in hour, minutes, and seconds:

Alarm.timerRepeat(15, Repeats);            // timer task every 15 seconds

This calls the Repeats function in your sketch every 15 seconds.

If you want a timer to trigger once only, use the timerOnce method:

Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds

This calls the onceOnly function in a sketch 10 seconds after the timer is created.

Note: Your code needs to call Alarm.delay regularly because this function checks the state of all the scheduled events. Failing to regularly call Alarm.delay will result in the alarms not being triggered. You can call Alarm.delay(0) if you need to service the scheduler without a delay. Always use Alarm.delay instead of delay when using TimeAlarms in a sketch.

The TimeAlarms library requires the Time library to be installed. No internal or external hardware is required to use the TimeAlarms library. The scheduler does not use interrupts, so the task-handling function is the same as any other functions you create in your sketch (code in an interrupt handler has restrictions, but these do not apply to TimeAlarms functions).

Timer intervals can range from one second to several years. (If you need timer intervals shorter than one second, the TimedAction library by Alexander Brevig may be more suitable; see http://www.arduino.c...ode/TimedAction.)

Tasks are scheduled for specific times designated by the system clock in the Time library. If you change the system time (e.g., by calling setTime), the trigger times are not adjusted. For example, if you use setTime to move one hour ahead, all alarms and timers will occur one hour sooner. In other words, if it’s 1:00 and a task is set to trigger in two hours (at 3:00), and then you change the current time to 2:00, the task will trigger in one hour. If the system time is set backward—for example, to 12:00—the task will trigger in three hours (i.e., when the system time indicates 3:00). If the time is reset to earlier than the time at which a task was scheduled, the task will be triggered immediately (actually, on the next call to Alarm.delay).

This is the expected behavior for alarms—tasks are scheduled for a specific time of day and will trigger at that time—but the effect on timers may be less clear. If a timer is scheduled to trigger in five minutes’ time and then the clock is set back by one hour, that timer will not trigger until one hour and five minutes have elapsed (even if it is a repeating timer—a repeat does not get rescheduled until after it triggers).

Up to six alarms and timers can be scheduled to run at the same time. You can modify the library to enable more tasks to be scheduled.

onceOnly alarms and timers are freed when they are triggered, and you can reschedule these as often as you want so long as there are no more than six pending at one time. The following code gives one example of how a timerOnce task can be rescheduled:

Alarm.timerOnce(random(10), randomTimer);  // trigger after random number of

void randomTimer(){
  int period = random(2,10);             // get a new random period
  Alarm.timerOnce(period, randomTimer);  // trigger for another random period

Arduino Cookbook

Learn more about this topic from Arduino Cookbook.

Create your own robots, toys, remote controllers, alarms, detectors, and many other projects with the Arduino device. This simple microcontroller board lets artists and designers build a variety of amazing objects and prototypes that interact with the physical world. With this book, you can dive right in and experiment with more than a hundred tips and techniques, no matter what your skill level is. You'll find the examples and advice you need to begin, expand, and enhance your projects right away.

See what you'll learn

0 Subscribe

2 Replies

  Thomas Galiana's Photo
Posted Oct 30 2012 03:43 PM

Mr. Margolis,

How is the Alarm object created? I Cannot find it anywhere explianed in the article.

Thomas Galiana
  Ali Hamza's Photo
Posted Mar 04 2013 02:24 AM


I was just trying your code. But it gives me error 'setTime' was not declared in the scope. Can you help me out with this thing please.

Thank you