Alarm.alarmRepeat() don't trigger when I "re-setTime()". In TimeAlarms.h


I got this problem, I’m trying to do a sketch to turn ON/OFF a lights in a specific time with the Alarm.alarmRepeat() function. I set the time in the “void setup()” but when its about 2 o 3 days running I want to change the time with a keyboard matrix (because the clock is slow and loses the current time) and after that, the Alarm.alarmRepeat() don’t trigger again and that is my problem. I attempted to call back the Alarm.alarmRepeat() function but didn´t work. have you any idea about that?

what I want is that if the Alarm.alarmRepead() is scheduled at 8am and I “re-setTime” at 7:50am, in 10 minutes call alarm function.

everything works fine until I “re-setTime”.I change the time and waited to see if it was activated, but nothing happened.
I apologize for my English… XD

a summary of my code…

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

void setup()
Alarm.alarmRepeat(19,30,00, AuntoCalleOn);
Alarm.alarmRepeat(06,45,00, AuntoCalleOff);
// … other setup here

void loop()
// with a switch I chose the function SetReloj() to re-set time
// …

case ‘3’:
else if(Lee() == ‘A’){

void SetReloj()

//… code for read the keyboard and set the variables: HORA MINUTO… and the others
HORA = hora[0]*10 + hora[1];
MINUTO = minutos[0]*10 + minutos[1];
DIA = dia[0]*10 + dia[1];
MES = mes[0]*10 + mes[1];

Look at what the setTime() function is doing. It may clear any alarms.

The simple solution seems to be to re-setup the alarms in the function that resets the time.

The better solution is a real time clock that keeps better time.

Thanks for your help. I really do not know what the setTime function its doing ,I tryed with re-set alarms, but it didn't work.

(something like this)

//... HORA = hora[0]*10 + hora[1]; MINUTO = minutos[0]*10 + minutos[1]; DIA = dia[0]*10 + dia[1]; MES = mes[0]*10 + mes[1]; setTime(HORA,MINUTO,0,MES,DIA,2013); Alarm.alarmRepeat(19,30,00, AuntoCalleOn); Alarm.alarmRepeat(06,45,00, AuntoCalleOff); Alarm.delay(0); //...

If you have any other ideas, I appreciate it!

Are you sure that the parameters that you use to set the time in the SetReloj functionsetTime(HORA,MINUTO,0,MES,DIA,2013);actually have the values that they should ?

Try printing them out in the function to confirm that they are correct.

Also, the setTime function:

void    setTime(int hr,int min,int sec,int day, int month, int yr);

I'm not sure, but I think you have day and month reversed.

UKHeliBob: Thanks for your help, I printed them on an LCD and comprove time changes every setTime. But I’ll try again because I only prints the hour, minute, and second, but not the date…

PaulS: I appreciate your observation, I’ll change the position of that value.

I read this text in the Arduino CookBook by Margolis Michael, maybe can help accurately interpret meaning.
as I said “everything works fine until I “re-setTime”.I change the time and waited to see if it was activated (two days at least), but nothing happened”

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 set Time), 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).

ArduinoCookBook.rtf (1.51 KB)

Hey there!

For anyone finding this thread, I had the same issue and found a solution!

I am setting the time using the serial monitor with code taken from the 'setSerial' example.

I found that when you do this, any alarms that were set up, are no longer enabled.

To fix it, I declared my alarms using the following function and then call it both from setup() and when I have just set the time:

void initialiseAlarms() {
  // N.B by default the Alarms library only supports up to 6 alarms.
  // This limit can be extended by editing the library (apparently).
  static AlarmID_t lightsOnAlarm = Alarm.alarmRepeat(LIGHTS_ON_HOUR, LIGHTS_ON_MINUTE, 0, lightsOn);
  static AlarmID_t lightsOffAlarm = Alarm.alarmRepeat(LIGHTS_OFF_HOUR, LIGHTS_OFF_MINUTE, 0, lightsOff);
  static AlarmID_t airOnAlarm = Alarm.alarmRepeat(AIR_ON_HOUR, AIR_ON_MINUTE, 0, airOn);
  static AlarmID_t airOffAlarm = Alarm.alarmRepeat(AIR_OFF_HOUR, AIR_OFF_MINUTE, 0, airOff);
  static AlarmID_t co2OnAlarm = Alarm.alarmRepeat(CO2_ON_HOUR, CO2_ON_MINUTE, 0, co2On);
  static AlarmID_t co2OffAlarm = Alarm.alarmRepeat(CO2_OFF_HOUR, CO2_OFF_MINUTE, 0, co2Off);

  // We have to re-enable the existing alarms every time we set the time for some reason.

This way, the alarms are all re-enabled every time the time is set. This seems to work reliably. You do have to call Alarm.enable(..) for it to work, so you have to store the alarm IDs in static variables.

Hope this helps someone!