How reset all alarms in TimeAlarm

How do you reset all alarms in the library TimeAlarms? For instans if I use alarmRepeat it seems to stay for ever. I have tried to reset H/W, upload again and disable the alarms but the alarms are somehow still there. Ther should be some master reset so you can start with a clean environment.

Ther should be some master reset so you can start with a clean environment.

There should be some sticky posts that describe how to post in a forum. Oh, wait, there are.

There should be some desire on your part to post a question that can be answered.

Where is your code?

Seeing your whole code would be helpful in answering your question but I may be able to point you along the way.

An alarm ID is created when you set an alarm using TimeAlarms. The ID number can be used to disable and enable the alarm if needed. To get the ID for an alarm do this when you create it

ID = Alarm.timerRepeat(23, 45, 56, TimerFunction);

and disable it like this

Alarm.disable(ID);

This works with any type of alarm supported by TimeAlarms
See the TimeAlarms readme for more details.

Here is the start of the code with the main loop:

/*
 * Plant styr ljus och bevattning
 */
#include <Time.h>
#include <TimeAlarms.h>
#include <EEPROM.h>
int Ljus = 9;
int Vatten = 8;
int VattenTid = 1;
int LjusTid = 2;
int VattenSec = 0;
int LjusTim =0;
int LjusMin = 0;
unsigned int LjusSec = 0;

int i;

char H[3] = "00";
char M[3] = "00";
char S[3] = "00";
char Y[3] = "00";
char Md[3] = "00";
char D[3] = "00";

void setup(){
  pinMode (Ljus, OUTPUT);
  pinMode (Vatten, OUTPUT); 
  Serial.begin(9600); 

//Ställ i tid och datum samt sätt starttider för
//lus och vatten
  uppdatTid();
  
}
//Huvudloop
void loop(){ 
   while(Serial.available() >= 1){
    char Cmd = Serial.read();
    switch (Cmd){
      case 'T':
        uppdatTid();
        break;
      case 'I':
        visaalarm();
        break;
      case 'L':
        lasLogg();
        break;
      case 'R':
        resetAlarm();
        break;
      default:
        Serial.println("Fel kommando"); 
    }
  }
  Alarm.delay(500);    
}

Here is the part that deal with alarmRepeat:

//Visa aktuell tid och hämta in eventuell ny tid
void uppdatTid(){ 
  Serial.print("Arduino visar tid = ");
  clockDisplay();
  Serial.print(" datum = ");
  dateDisplay();
  Serial.println(); 
  Serial.println("OK? (J/Ny tid)");    
  while(Serial.available() == 0){
  }
  Alarm.delay(500);
  H[0] = Serial.read();
  if(H[0] != 'J'){  
    H[1] = Serial.read();
    M[0] = Serial.read();
    M[1] = Serial.read();


    
    Y[0] = Serial.read();    
    Y[1] = Serial.read();
    Md[0] = Serial.read();
    Md[1] = Serial.read();
    D[0] = Serial.read();
    D[1] = Serial.read();
   
//Sätt tid och datum
    setTime(atoi(H), atoi(M), atoi(S), atoi(D), atoi(Md), atoi(Y)) ;
    clockDisplay();
    Serial.print("  ");
    dateDisplay();
    Serial.println(" ");
//Starttider för ljus och vatten måste initieras på nytt
    sattStart();
  } 
  else
    Serial.println("Ingen andring av tid");
}

//Sätt starttider för ljus och vatten
void sattStart(){
  Alarm.alarmRepeat(6,0,0,LjusPa1); 
  Alarm.alarmRepeat(8,30,0, VattenPa1);
  visaAlarm;
}

//Visa alarmtid och intervall för ljus och vatten
//Visa inställd tid för ljus
void visaAlarm(){
  int t;
  int ID;
  String AlarmTid = " ";
  
  ID = Alarm.timerRepeat(6,0,0,LjusPa);
  t = Alarm.read(ID);
  Serial.print("Ljus slas pa ");
  Serial.print(hour(t));
  Serial.print(":");
  Serial.print(minute(t));
  Serial.print(":");
  Serial.print(second(t));
  LjusMin = analogRead(LjusTid)%60;
  LjusTim = analogRead(LjusTid)/60;
  Serial.print(" och ar pa i ");
  Serial.print(LjusTim);
  Serial.print(" timmar och ");
  Serial.print(LjusMin);
  Serial.println(" minuter");
  
//Visa inställd tid för vatten
  ID = Alarm.timerRepeat(8,30,0,VattenPa);
  t = Alarm.read(ID);
  Serial.print("Vatten slas pa ");
  Serial.print(hour(t));
  Serial.print(":");
  Serial.print(minute(t));
  Serial.print(":");
  Serial.print(second(t));
  VattenSec = analogRead(VattenTid)/3;
  Serial.print(" och ar pa i ");
  Serial.print(VattenSec);
  Serial.println(" sekunder"); 
}

The function "uppdatTid" request and set new time to the clock and initiate two alarmRepeat's. The function "visaAlarm" send information about the armed alarmRepeat's.

The problem is that when time is set by "uppdatTid" the alarms are fired immediately and the value read by "visaAlarm" is 0. However if the Arduino is powered off and started again or reset the function "visaAlarm" display the originally set alarmRepeat's. You can not get rid of them. I guess the alarms are stored in the EEPROM and then somehow picked up after restart of the program by the library routines in "TimeAlarms" or is it another problem?

while(Serial.available() == 0){
  }
  Alarm.delay(500);
  H[0] = Serial.read();
  if(H[0] != 'J'){  
    H[1] = Serial.read();
    M[0] = Serial.read();
    M[1] = Serial.read();

Do nothing until there is at least one byte of serial data to read. Then, potentially, immediately read all 4 of the one that arrived.

Hmmm, not the way I would have written this.

I'm in the process of researching the TimeAlarms library to see if it'll do what I want, and I had a question similar to that of the OP.

Regarding UKHeliBob's suggestion above, it might be better to use the Alarm.free() function, which reads:

void TimeAlarmsClass::free(AlarmID_t ID)
{
  if (isAllocated(ID)) {
    Alarm[ID].Mode.isEnabled = false;
    Alarm[ID].Mode.alarmType = dtNotAllocated;
    Alarm[ID].onTickHandler = NULL;
    Alarm[ID].value = 0;
    Alarm[ID].nextTrigger = 0;
  }
}

The key thing here is that the Alarm.free() function sets the alarmType to dtNotAllocated. When you create a new alarm, this variable is set to alarmType, in the following code:

// attempt to create an alarm and return true if successful
AlarmID_t TimeAlarmsClass::create(time_t value, OnTick_t onTickHandler, uint8_t isOneShot, dtAlarmPeriod_t alarmType)
{
  if ( ! ( (dtIsAlarm(alarmType) && now() < SECS_PER_YEAR) || (dtUseAbsoluteValue(alarmType) && (value == 0)) ) ) {
    // only create alarm ids if the time is at least Jan 1 1971
    for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
      if (Alarm[id].Mode.alarmType == dtNotAllocated) {
        // here if there is an Alarm id that is not allocated
        Alarm[id].onTickHandler = onTickHandler;
        Alarm[id].Mode.isOneShot = isOneShot;
        Alarm[id].Mode.alarmType = alarmType;
        Alarm[id].value = value;
        enable(id);
        return id;  // alarm created ok
      }
    }
  }
  return dtINVALID_ALARM_ID; // no IDs available or time is invalid
}

Note that in the above code fragment for the Alarm.create() function, the function loops through the available alarms to find the first one that is not allocated, using the following for loop:

for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
}

So one could theoretically write a loop that clears all the alarms, for example:

for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
  Alarm.free(id);
}

But I confess that I haven't tried it. Yet.

I note that the free() function is declared as a public method in TimeAlarms.h:

  void free(AlarmID_t ID);                  // free the id to allow its reuse

and dtNBR_ALARMS is defined at the top of TimeAlarms.h.