HELP PLEASE using TimeAlarm Library

Hello All,

Could someone please help I want to use the alarm library to trigger some specific functions in my project. The below code is just a simplified example to highlight the issues im having. For starters what I want to do is trigger an alarm to execute some code I also want to.

  • Identify the alarm ID that is triggered can’t get it to work with Alarm. getTriggeredAlarmId()
  • Disable the alarm at will with code. then enable or reset it to trigger again when called again

Below is a poor attempt at doing that (doesn’t work). any direction, help or examples would be greatly appreciated.

#include <Time.h>
#include <TimeAlarms.h>
  int nowSecondAlarm ;
 const int PB = 2;
  int LED13 = 13;


 AlarmID_t myTimer;
 
 
void setup()

{ 
  pinMode(LED13, OUTPUT);
  pinMode(PB, INPUT);
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  
myTimer = Alarm.timerRepeat(15, Blinker);            // timer for every 15 seconds    
   Alarm.timerOnce(30,  OnceOnly);             // called once after 10 seconds 
}


void  loop(){  
  digitalClockDisplay();
  AlarmId ID = Alarm. getTriggeredAlarmId();
  Serial.println(ID);
  Alarm.delay(1000); 
  Alarm.delay(1000); // wait one second between clock display
}

void Repeats(){
  Serial.println("15 second timer");         
}

void OnceOnly(){
  Serial.println("This timer only triggers once"); 
 Alarm.disable( myTimer); 
}

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);
}
void Blinker() {
  Serial.print("alarm triggered");
digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

All your code needs to be in a function.

I changed a little bit your code to try to show you what you are doing wrong:

#include <Time.h>
#include <TimeAlarms.h>
int nowSecondAlarm ;
const int PB = 2;
int LED13 = 13;


AlarmID_t timerBliker;
AlarmID_t timerOnceOnly;

void setup()
{
  pinMode(LED13, OUTPUT);
  pinMode(PB, INPUT);
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  timerBliker = Alarm.timerRepeat(15, Blinker);            // timer for every 15 seconds   
  timerOnceOnly = Alarm.timerOnce(30,  OnceOnly);             // called once after 10 seconds

  Serial.print("timerBliker: ");
  Serial.println(timerBliker);
  Serial.print("timerOnceOnly: ");
  Serial.println(timerOnceOnly);

}


void  loop(){ 
  digitalClockDisplay();
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.println(ID);
  Alarm.delay(1000);
  Alarm.delay(1000); // wait one second between clock display
}


void Repeats(){
  Serial.println("15 second timer");         
}


void OnceOnly(){
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.println(ID);
  
  Serial.println("This timer only triggers once");
  Alarm.disable(timerBliker);
}


void digitalClockDisplay()
{
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.println(ID);
  // 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);
}


void Blinker() {
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.println(ID);
  Serial.println("alarm triggered");
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
}

Now, you can see the correct AlarmID (when the alarm is created and when the the alarm is triggered - inside the function that is called after the alarm is triggered). And you can see the “blinker alarm” being disable after the line:

 Alarm.disable(timerBliker);

Awesome response luisilva Very helpful. I played with the code a bit more trying to understand how this library works. Would you or anyone else know how to reset a “once only” alarm I get a repeat alarm to reset with a different cycle time but the once only is a little more stubborn it seems.
Alarm.enable(timerOnceOnly);
Alarm.write(1, 8 ) ; // - write the value (and enable) the alarm for the given ID
is not working out. :slightly_frowning_face:

#include <Time.h>
#include <TimeAlarms.h>
int nowSecondAlarm ;
const int PB = 2;
int LED13 = 13;


AlarmID_t timerBliker;
AlarmID_t timerOnceOnly;
AlarmID_t timerOnceReset;

void setup()
{
  pinMode(LED13, OUTPUT);
  pinMode(PB, INPUT);
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  timerBliker = Alarm.timerRepeat(5, Blinker);            // timer for every 15 seconds   
  timerOnceOnly = Alarm.timerOnce(9,  OnceOnly);             // called once after 10 seconds
  timerOnceReset = Alarm.timerRepeat(14, Reset);            // timer for every 15 seconds 

  Serial.print("timerBliker: ");
  Serial.println(timerBliker);
  Serial.print("timerOnceOnly: ");
  Serial.println(timerOnceOnly);
  Serial.print("timerOnceReset: ");
  Serial.println(timerOnceReset);

}


void  loop(){ 
  digitalClockDisplay();
//AlarmID_t ID = Alarm. getTriggeredAlarmId();
 // Serial.println(ID);
 // Alarm.delay(1000);

  Alarm.delay(1000); // wait one second between clock display
}


void Reset(){
  Serial.println("reset blinker alarm"); 
   Alarm.write(0,  5); // -  write the value (and enable) the alarm for the given ID 
   Serial.println("disable once alarm"); 
   Alarm.delay (100);   
   Alarm.disable(timerOnceOnly);  
   Serial.println("Reset once alarm"); 
   Alarm.delay (100);
   Alarm.enable(timerOnceOnly);  
   Alarm.write(1,  8); // -  write the value (and enable) the alarm for the given ID  
    
}


void OnceOnly(){
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.print(ID);
  Serial.println("...ID OnceOnly");
  Serial.println("This timer only triggers once and Blinker Diable");
  Alarm.disable(timerBliker);
  digitalWrite(13, LOW);   // turn the LED OFF 
 
}


void digitalClockDisplay()
{
 // AlarmID_t ID = Alarm. getTriggeredAlarmId();
  //Serial.print(ID);
//Serial.println("...IDa");
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
  Alarm.delay(0);
}


void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


void Blinker() {
  Serial.println("timerBliker alarm triggered");
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.print(ID);
  Serial.println("...ID Blinker");
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  Alarm.delay (500);
   digitalWrite(13, LOW);   
   Alarm.delay (500);
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
}

For what I see you are trying to "enable" a timer that only repeat once ad that was already triggered. So, as it was already triggered it wouldn't not trigger again. I don't know if it make sense to you. Try to do that to a "timerRepeat" or create another "timerOnce".

Thanks Again!!! I got what I needed to work in the below. Now I can apply what I learned to something useful. Cheers.

#include <Time.h>
#include <TimeAlarms.h>
int nowSecondAlarm ;
const int buttonPin = 2; 
const int LED13 = 13;
int buttonState = 0;

AlarmID_t timerBliker;
AlarmID_t timerOnceOnly;
AlarmID_t timerOnceReset;

void setup()
{
  pinMode(LED13, OUTPUT);
  pinMode(buttonPin, INPUT);
 
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  timerBliker = Alarm.timerRepeat(3, Blinker);            // timer for every 15 seconds   
  timerOnceOnly = Alarm.timerOnce(9,  OnceOnly);             // called once after 10 seconds
  timerOnceReset = Alarm.timerRepeat(14, Reset);            // timer for every 15 seconds 

  Serial.print("timerBliker: ");
  Serial.println(timerBliker);
  Serial.print("timerOnceOnly: ");
  Serial.println(timerOnceOnly);
  Serial.print("timerOnceReset: ");
  Serial.println(timerOnceReset);

}


void  loop(){ 
  buttonState = digitalRead(buttonPin);
  digitalClockDisplay();
//AlarmID_t ID = Alarm. getTriggeredAlarmId();
 // Serial.println(ID);
 // Alarm.delay(1000);
 
 Alarm.delay(1000); // wait one second between clock display
if (buttonState == HIGH){
  Serial.println("*******ALARMS DISABLED****************"); 
  Alarm.enable(timerBliker);
  Alarm.disable(timerOnceOnly);
  Alarm.disable(timerOnceReset);
}


  Alarm.delay(1000); // wait one second between clock display
}



void Reset(){
  Serial.println("reset blinker alarm"); 
   Alarm.write(0,  5); // -  write the value (and enable) the alarm for the given ID 
   Serial.println("disable once alarm"); 
   Alarm.delay (100);   
  
   Serial.println("Reset once alarm"); 
   Alarm.delay (100);
   
  int period = random(3,10);             // get a new random period 
  Alarm.timerOnce(period, OnceOnly);  // trigger for another random period 

   
}


void OnceOnly(){
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.print(ID);
  Serial.println("...ID OnceOnly");
  Serial.println("This timer only triggers once and Blinker Diable");
  Alarm.disable(timerBliker);
  digitalWrite(13, LOW);   // turn the LED OFF 
 
}


void digitalClockDisplay()
{
 // AlarmID_t ID = Alarm. getTriggeredAlarmId();
  //Serial.print(ID);
//Serial.println("...IDa");
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
  Alarm.delay(0);
}


void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


void Blinker() {
  Serial.println("timerBliker alarm triggered");
  AlarmID_t ID = Alarm. getTriggeredAlarmId();
  Serial.print(ID);
  Serial.println("...ID Blinker");
  Alarm.enable (timerOnceOnly);
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  Alarm.delay (500);
   digitalWrite(13, LOW);   // turn the LED on (HIGH is the voltage level)
   Alarm.delay (500);
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
}

One more thing. The manes of the variables (and functions) must be something meaningful. So, for things like:

  timerOnceReset = Alarm.timerRepeat(14, Reset);            // timer for every 15 seconds

you must create names like:

  timerReset = Alarm.timerRepeat(14, Reset);            // timer for every 15 seconds

or:

  timerRepeatReset = Alarm.timerRepeat(14, Reset);            // timer for every 15 seconds

Fair point, Bit messy on my part for sure. Also doesn't help with future understanding of the code for me or any one else.

Thanks again.