• Stars
    star
    159
  • Rank 231,281 (Top 5 %)
  • Language
    C++
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Time library add-on, schedule alarms to occur at specific dates/times
Alarms

The Alarm library is a companion to the Time library that makes it easy to 
perform tasks at specific times or after specific intervals.

Tasks scheduled at a particular time of day are called Alarms,
tasks scheduled after an interval of time has elapsed are called Timers.
These tasks can be created to continuously repeat or to occur once only.  

Here is how you create an alarm to trigger a task repeatedly at a particular time of day:
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  
This would call the function MorningAlarm()  at 8:30 am 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 a MorningAlarm() function in a sketch once only (when the time is next 8:30am)

Alarms can be specified to trigger a task repeatedly at a particular day of week and time of day:
  Alarm.alarmRepeat(dowMonday, 9,15,0, MondayMorningAlarm);  
This would call the function WeeklyAlarm() at 9:15am every Monday.

If you want the alarm to trigger once only on a particular day and time you can do this:
   Alarm.alarmOnce(dowMonday, 9,15,0, MondayMorningAlarm);  
This would call the function MondayMorning() Alarm on the next Monday at 9:15am.

Timers trigger tasks that occur after a specified interval of time has passed.
The timer interval can be specified in 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, you can 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. 

If you want to trigger once at a specified date and time you can use the trigger Once() method:
  Alarm. triggerOnce(time_t value,  explicitAlarm); // value specifies a date and time
(See the makeTime() method in the Time library to convert dates and times into time_t)

Your sketch should call the Alarm.delay() function instead of the Arduino delay() function when
using the Alarms library.  The timeliness of triggers depends on sketch delays using this function.
  Alarm.delay( period); // Similar to Arduino delay - pauses the program for the period (in milliseconds).


 
Here is an example sketch:

This sketch  triggers daily alarms at 8:30 am and 17:45 pm.
A Timer is triggered every 15 seconds, another timer triggers once only after 10 seconds.
A weekly alarm is triggered every Sunday at 8:30:30

#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);
}
Note that the loop code calls Alarm.delay(1000) -  Alarm.delay must be used
instead of the usual arduino delay function because the alarms are serviced in the Alarm.delay method.
Failing to regularly call Alarm.delay will result in the alarms not being triggered
so always use Alarm.delay instead of delay in sketches that use the Alarms library.

Functional reference:

// functions to create alarms and timers

Alarm.triggerOnce(value, AlarmFunction);
  Description: Call user provided AlarmFunction once at the date and time of the given value
  See the Ttime library for more on time_t values 
  
Alarm.alarmRepeat(Hour, Minute, Second,  AlarmFunction);
  Description:  Calls user provided AlarmFunction  every day at the given Hour, Minute and Second.

Alarm.alarmRepeat(value,  AlarmFunction);
  Description:  Calls user provided AlarmFunction  every day at the time indicated by the given value

Alarm.alarmRepeat(DayOfWeek, Hour, Minute, Second,  AlarmFunction);
  Description:  Calls user provided AlarmFunction  every week on the given  DayOfWeek, Hour, Minute and Second.

Alarm.alarmOnce(Hour, Minute, Second,  AlarmFunction);
  Description:  Calls user provided AlarmFunction once when the Arduino time next reaches the given Hour, Minute and Second.

Alarm.alarmOnce(value,  AlarmFunction);
  Description:  Calls user provided AlarmFunction  once at the next  time indicated by the given value

Alarm.alarmOnce(DayOfWeek, Hour, Minute, Second,  AlarmFunction);
  Description:  Calls user provided AlarmFunction  once only on the next  DayOfWeek, Hour, Minute and Second.

Alarm.timerRepeat(Period, TimerFunction);
  Description:  Continuously calls user provided TimerFunction  after the given period in seconds has elapsed. 

Alarm.timerRepeat(Hour, Minute, Second, TimerFunction);
  Description:  As timerRepeat above, but period is the number of seconds in the given Hour, Minute and Second parameters

Alarm.timerOnce(Period, TimerFunction);
  Description:  Calls user provided TimerFunction  once only after the given period in seconds has elapsed. 

Alarm.timerOnce(Hour, Minute, Second, TimerFunction);
  Description:  As timerOnce above, but period is the number of seconds in the given Hour, Minute and Second parameters

Alarm.delay( period)
 Description: Similar to Arduino delay - pauses the program for the period (in miliseconds) specified.
 Call this function rather than the Arduino delay function when using the Alarms library.
 The timeliness of the triggers  depends on sketch delays using this function.

Low level functions not usually required for typical applications:
  disable( ID);  -  prevent the alarm associated with the given ID from triggering   
  enable(ID);  -  enable the alarm 
  write(ID,  value);  -  write the value (and enable) the alarm for the given ID  
  read(ID);     - return the value for the given ID  
  readType(ID);  - return the alarm type for the given alarm ID
  getTriggeredAlarmId();   -  returns the currently triggered  alarm id, only valid in an alarm callback

FAQ

Q: What hardware and software is needed to use this library?
A: This library requires the Time library. No internal or external hardware is used by the Alarm library.

Q: Why must I use Alarm.delay() instead of delay()?
A: Task scheduling is handled in the Alarm.delay function.
Tasks are monitored and  triggered from within the Alarm.delay call so Alarm.delay should be called
whenever a delay is required in your sketch.
If your sketch waits on an external event (for example,  a sensor change),
make sure you repeatedly call Alarm.delay while checking the sensor.
You can call Alarm.delay(0) if you need to service the scheduler without a delay.

Q: Are there any restrictions on the code in a task handler function?
A: No. The scheduler does not use interrupts so your task handling function is no
different from other functions you create in your sketch. 

Q: What are the shortest and longest intervals that can be scheduled?
A:  Time intervals can range from 1 second to years.
(If you need timer intervals shorter than 1 second then the TimedAction library
by Alexander Brevig may be more suitable, see: http://www.arduino.cc/playground/Code/TimedAction)

Q: How are scheduled tasks affected if the system time is changed?
A: Tasks are scheduled for specific times designated by the system clock.
If the system time is reset to a later time (for example one hour ahead) then all
alarms and timers will occur one hour later.
If the system time is set backwards (for example one hour back) then the alarms and timers will occur an hour earlier.
If the time is reset before the time a task was scheduled, then the task will be triggered on the next service (the next call to Alarm.delay).
This is  the expected behaviour for Alarms � tasks scheduled for a specific time of day will trigger at that time, but the affect on timers may not be intuitive. If a timer is scheduled to trigger in 5 minutes time and the clock is set ahead by one hour, that timer will not trigger until one hour and 5 minutes has elapsed.

Q: What  is the valid range of times supported by these libraries?
A: The time library is intended to handle times from Jan 1 1970 through Jan 19 2038.
 The Alarms library expects dates to be on or after Jan1 1971 so clocks should no be set earlier than this if using Alarms.
(The functions to create alarms will return an error if an earlier date is given).

Q: How many alarms can be created?
A: Up to six alarms can be scheduled.  
The number of alarms can be changed in the TimeAlarms header file (set by the constant dtNBR_ALARMS,
note that the RAM used equals dtNBR_ALARMS  * 11)

onceOnly Alarms and Timers are freed when they are triggered so another onceOnly alarm can be set to trigger again.
There is no limit to the number of times a onceOnly alarm can be reset.

The following fragment gives one example of how a timerOnce  task can be rescheduled:
Alarm.timerOnce(random(10), randomTimer);  // trigger after random number of seconds

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

More Repositories

1

Time

Time library for Arduino
C++
1,238
star
2

Audio

Teensy Audio Library
C++
955
star
3

OneWire

Library for Dallas/Maxim 1-Wire Chips
C++
542
star
4

Encoder

Quadrature Encoder Library for Arduino
C++
489
star
5

cores

Teensy Core Libraries for Arduino
C
474
star
6

TimerOne

TimerOne Library with optimization and expanded hardware support
C++
438
star
7

CapacitiveSensor

Detect touch or proximity by capacitve sensing
C++
360
star
8

teensy_loader_cli

Command line Teensy Loader
C
316
star
9

AltSoftSerial

Software emulated serial using hardware timers for improved compatibility
C++
308
star
10

SerialFlash

Library for using SPI Flash memory with a filesystem-like interface
C++
296
star
11

OctoWS2811

Control thousands of WS2811/2812 LEDs at video refresh speeds
C++
247
star
12

RadioHead

Version of RadioHead library for Teensy boards
C++
237
star
13

ILI9341_t3

Optimized ILI9341 TFT Library
C
224
star
14

XPT2046_Touchscreen

Touchscreen Arduino Library for XPT2046 Touch Controller Chip
C++
208
star
15

USBHost_t36

USB Host Library for Teensy 3.6 and 4.0
C++
166
star
16

MahonyAHRS

C++
152
star
17

WS2812Serial

Non-Blocking WS2812 LED Library
C++
137
star
18

FreqCount

Measures the frequency of a signal by counting the number of pulses during a fixed time.
C
136
star
19

PS2Keyboard

PS/2 Keyboard Library for Arduino
C++
132
star
20

DS1307RTC

Use a DS1307 Real Time Clock chip with the Time library
C++
127
star
21

Ethernet

Ethernet library for Teensy (Wiznet W5100 / W5200 / W5500)
C++
125
star
22

SPI

SPI library for Teensy & Arduino IDE
C++
119
star
23

MsTimer2

Run a function using a timer
C++
104
star
24

CoreMark

Benchmark CPU Performance on Arduino Compatible Boards
C
102
star
25

DmxSimple

C++
90
star
26

EEPROM

C++
76
star
27

TimerThree

TimerThree Library with optimization and expanded hardware support
C++
75
star
28

LittleFS

C
71
star
29

Tlc5940

16 channel PWM LED driver based on the Texas Instruments TLC5940 chip.
C
71
star
30

SD

C++
67
star
31

FreqMeasure

Measures the elapsed time during each cycle of an input frequency.
C
64
star
32

MotionCal

Motion Sensor Calibration Tool
C
63
star
33

NXPMotionSense

NXP Motion Sensors for Teensy and Arduino
C++
61
star
34

ILI9341_fonts

Extra fonts for use with ILI9341_t3
C
50
star
35

hid_listen

C
47
star
36

PulsePosition

Multiple High-Res Input & Output PPM Encoded Signal Streams on Teensy 4.x, 3.x & LC
C++
45
star
37

FreqMeasureMulti

Measures the elapsed time during each cycle of up to 8 input frequencies.
C++
34
star
38

ST7735_t3

Teensy 3.x Optimized Version of Adafruit_ST7735 (1.8 inch TFT Display)
C
32
star
39

X-Plane_Plugin

TeensyControls Plugin for X-Plane Flight Simulator
C
31
star
40

MakerFaire2017

Monolith Synth
C++
24
star
41

Bridge

Communicate between Teensy & Raspberry Pi - similar to Arduino Yun
Arduino
24
star
42

RSA_signature_speed

Simple CPU speed benchmark
C
24
star
43

ARM-Toolchain

Shell
23
star
44

PWMServo

Control RC Servo motors with interrupt-resilient PWM
C++
23
star
45

Teensyduino_Examples

Examples for Teensyduino, installed into Arduino's File > Examples > Teensy menu
C++
22
star
46

teensy41_psram_memtest

C++
21
star
47

teensy41_extram

C
20
star
48

Wire

Wire library used on Teensy boards
C++
20
star
49

LedDisplay

Print text to an Avago HCMS-29xx LED display
C++
19
star
50

FrequencyTimer2

Create a zero jitter frequency output, and run your own function on each period.
C++
19
star
51

teensy41_ethernet

C
15
star
52

CryptoAccel

Assembly
15
star
53

TouchGuitar

Arduino
13
star
54

arm_math

C
11
star
55

k66_ethernet

Experiments with K66 Ethernet
Arduino
10
star
56

USB-Serial-Print-Speed-Test

How many lines/second can your Arduino print to the serial monitor?
C++
10
star
57

AVR_DTMF

Efficient 8 bit AVR-based DTMF Decoding
C
10
star
58

Tectonic_Pitch_Shift

C++
9
star
59

FreqCountMany

Measure 10 frequencies with Teensy 4.0 & 4.1
C++
9
star
60

ARM_Toolchain_2014q1_Source

GCC ARM Toolchain Source - for running Arduino on Raspberry Pi
Shell
9
star
61

secure_plugin

Teensy 4 Security plugin for Arduino IDE
Java
8
star
62

SoundfontDecoder

Python
8
star
63

ST7789_t3

C++
7
star
64

Arduino-1.8.9-Teensyduino

Java
7
star
65

ht1632c

Objective-C
7
star
66

Arduino-1.8.5-Teensyduino

C
7
star
67

TopOctaveGenerator

C++
6
star
68

Verify_All_Arduino_Libraries

Perl
6
star
69

teensy_size

C
6
star
70

Arduino-1.8.13-Teensyduino

Java
5
star
71

Arduino-examples-for-Teensyduino

The slightly modified examples for Teensyduino
Arduino
5
star
72

TalkieKeyboards

Five 104-key keyboards trigger speaking 520 words
C
5
star
73

LargeSamplePlayer

C++
5
star
74

Arduino-1.6.0-Teensyduino

C
5
star
75

USB_Tester

C++
5
star
76

hab_tests

C
5
star
77

teardown2018

LED Visualization Shown at Teardown 2018 Hardware Conference
C++
5
star
78

Arduino-1.8.19-Teensyduino

Java
4
star
79

timecode_now

C
4
star
80

Arduino-1.8.8-Teensyduino

Java
4
star
81

Arduino-1.8.10-Teensyduino

Java
4
star
82

Lighting_Controller

C++
4
star
83

SerialDiscovery_JSON

C
4
star
84

Phazerville-Screen-Capture

C++
4
star
85

Arduino-1.8.7-Teensyduino

Java
3
star
86

MyFault

C
3
star
87

Arduino-1.8.12-Teensyduino

Java
3
star
88

ARM_Toolchain_2016q3_Source

Shell
3
star
89

precompile_helper

Help arduino-builder to generate a precompiled Arduino.h header, to speed up compiling Arduino sketches
C
3
star
90

Arduino-1.6.4-Teensyduino

C
3
star
91

AudioWorkshop2015

Audio Workshop at Hackaday Supercon 2015
3
star
92

antplus

Stand-alone Ant Plus code from Michael McElligott
C++
3
star
93

Arduino-1.8.14-Teensyduino

Java
3
star
94

Video_Start_Button

C
2
star
95

Noritake_VFD_CUY

Noritake_VFD_CUY: C++ code library and demo files
C++
2
star
96

Arduino_Dev_List_Topics

2
star
97

Servo

C++
2
star
98

Magnetometers_Test

Simple Magnetometer Testing
C++
2
star
99

StepperPulse

Experiments with DMA to efficiently generate stepper pulse & direction
Arduino
2
star
100

Arduino-1.6.12-Teensyduino

C
2
star