perhaps a worked example will help.
Here is the sketch posted earlier with timers added to 'turn off' pins at a preset interval after the alarm is triggered - this is the second option mentioned above.
/*
* DateTimeAlarmSimple.pde
*
* Set the time by sending 'T' followed by hh:mm:ss -> T08:29:55 sets the time for 8:29:55 am
* Alarms are set in code by changing paramaegers to createAlarm() in setup
* This example calls onAlarmA() at 8:30 am and onAlarmB() at 5:45 pm (17:45)
*/
#include <DateTime.h>
#include <DateTimeAlarms.h>
#define TIME_MSG_LEN 9 // Number of characters needed to decode a time message
#define TIME_HEADER 'T' // Header for serial messages
AlarmID_t alarmA, timerB;
AlarmID_t alarmC, timerD;
void setup(){
Serial.begin(9600);
Serial.println("Set clock by sending Thh:mm:ss");
alarmA = dtAlarms.createAlarm(AlarmHMS(8,30,0), &onAlarmA, false); // 8:30am
timerB = dtAlarms.createTimer(AlarmHMS(1,30,0), &onAlarmA, false); // timer for 1 hour 30 minutes
alarmC = dtAlarms.createAlarm(AlarmHMS(17,45,0),&onAlarmC, false); // 5:45pm
timerD = dtAlarms.createTimer(AlarmHMS(0,0,10), &onAlarmC, false); // timer for 10 seconds
}
void onAlarmA(AlarmID_t Sender){
if(Sender == alarmA)
{
Serial.print("Alarm A On ");
// turn on the pin for alarmA
dtAlarms.enable(timerB); // enable timer
}
else // if its not alarmA it must be timerB so turn the pin off
{
Serial.print("Alarm A Off ");
// turn off the pin
dtAlarms.disable(timerB); // disable timer
}
timeDisplay();
}
void onAlarmC(AlarmID_t Sender){
if(Sender == alarmC)
{
Serial.print("Alarm C On ");
// turn on the pin for alarmC
dtAlarms.enable(timerD); // enable timer
}
else // if its not alarmC it must be timerD so turn the pin off
{
Serial.print("Alarm C Off ");
// turn off the pin
dtAlarms.disable(timerD); // disable timer
}
timeDisplay();
}
void loop(){
processSerial(); // process a message to set the time
if(DateTime.available()) { // update clocks if time has been synced
dtAlarms.delay(1000);
timeDisplay();
}
}
void processSerial()
{
// Process a message available on serial port
// This version just sets the time of day
char tag;
int Hr,Min,Sec;
// time message = Thh:mm:ss
while(Serial.available() >= TIME_MSG_LEN ) // process messages when all characters are received
{
if(Serial.read() == TIME_HEADER )
{
Hr = getNumber();
Serial.read(); // ignore the colon
Min = getNumber();
Serial.read(); // ignore the colon
Sec = getNumber();
// time_t time = DateTime.makeTime(Sec, Min, Hr, 0, 0, 0 );
// because the actual day is never set, the current day is retained when adjusting time
time_t time = previousMidnight(DateTime.now()) + AlarmHMS(Hr, Min, Sec);
DateTime.sync(time);
Serial.print("Clock set at ");
timeDisplay();
dtAlarms.enable(alarmA);
dtAlarms.enable(alarmC);
}
}
}
// return the value given by the next two characters in the serial buffer
int getNumber()
{
int val = Serial.read() - '0';
val = (val * 10) + Serial.read() - '0';
return val;
}
void timeDisplay()
{
DateTime.available();
Serial.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
Serial.println();
}
void printDigits(byte digits)
{
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits,DEC);
}
The first alarm will trigger at 8:30am and will turn off one hour 30 minutes later. The code that sets that is this:
alarmA = dtAlarms.createAlarm(AlarmHMS(8,30,0), &onAlarmA, false); // 8:30am
the timer code that will activate 1hr 30 minutes later is this:
timerB = dtAlarms.createTimer(AlarmHMS(1,30,0), &onAlarmA, false); // timer for 1 hour 30 minutes
The handler for these is the function:
void onAlarmA(AlarmID_t Sender)
this code checks to see if it was trigger by the alarm (at the turn on time) or by the timer (for the turn off time)
The triggers for the second start and stop times are similar:
alarmC = dtAlarms.createAlarm(AlarmHMS(17,45,0),&onAlarmC, false); // 5:45pm
timerD = dtAlarms.createTimer(AlarmHMS(0,0,10), &onAlarmC, false); // timer for 10 seconds
This is set to turn of in 10 seconds. Sending T17:44:55 will set the time to 5 seconds before the alarm and you should see what happens.