updating Alarms with TimeAlrams library

Hi!

I’m trying to to change the time when an alarm is called of a repeting Alarm function (Alarm.alarmRepeat()).
Unfortunately, the next alarm is set to the following day (1293957000) , instead to now()+2 seconds (1293870602). The readme of the TimeAlarms library says that you can change Alarms using write(), but it doest not seem to work :frowning:
I tried to use Alarm.alarmOnce(onTime, MorningAlarm); instead of alarmRepeat, then the output of Alarm.getNextTrigger() is 0, meaning no alarms are scheduled.

My code:

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

AlarmId o;

time_t onTime = AlarmHMS(8,30,0) ; // 8:30 am


void setup()
{
  Serial.begin(9600);
  setTime(8,29,58,1,1,11); // set time to Saturday 8:29:58am Jan 1 2011
  // create the alarms 
  o = Alarm.alarmRepeat(onTime, MorningAlarm);  // 8:30am every day
  Serial.print("Alarm-ID o: ");
  Serial.println(o);

}

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"); 
  time_t newOnTime = now()+2;
  Alarm.write(o,newOnTime);
  
  Serial.print("now: ");
  Serial.println(now());
  
  Serial.print("newOnTime: ");
  Serial.println(newOnTime);
  
  Serial.print("next: ");
  Serial.println(Alarm.getNextTrigger());
}


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

serial output:

Alarm-ID o: 0
8:29:58
8:29:59
Alarm: - turn lights off
now: 1293870600
newOnTime: 1293870602
next: 1293957000
8:30:00
8:30:01
8:30:02
8:30:03
8:30:04
8:30:05
8:30:06

I'm trying to to change the time when an alarm is called of a repeting Alarm function (Alarm.alarmOnce()).

You don't have an alarmOnce() in your program and even if you did it would not, by definition, be a repeating alarm.

What happens if you disable the repeating alarm once it has triggered and set it again ?

You don't have an alarmOnce() in your program and even if you did it would not, by definition, be a repeating alarm.

I'm sorry, my fault. I meant "Alarm.alarmRepeat()" instead "Alarm.alarmOnce()". corrected it in my post. ;)

What happens if you disable the repeating alarm once it has triggered and set it again ?

Good idea! Tried it out, but it doesn't seem to work either. Alarm.getNextTrigger() returns 0, meaning no Trigger / alarm is scheduled.

I inserted "#define USE_SPECIALIST_METHODS" at the biginning of my code to make the Alarm.free() function public. I'm a little confused, because it compiles even without this definition. Normally it should output an error, because it doesnt know the Alarm.free()...

void MorningAlarm(){
  Serial.println("Alarm: - turn lights off"); 
  time_t newOnTime = now()+2;
  Alarm.free(o);
  o = Alarm.alarmRepeat(newOnTime, MorningAlarm);
  
  Serial.print("now: ");
  Serial.println(now());
  
  Serial.print("newOnTime: ");
  Serial.println(newOnTime);
  
  Serial.print("next: ");
  Serial.println(Alarm.getNextTrigger());
}