Alarm.timerRepeat(15, Repeats);

Anyone have succeded to implement Alarm.timerRepeat(15, Repeats); 
Its not working for me !! the function is never called each 15 secs and nothing is published

Could you give us a little more info, like what library you are using? Maybe post your entire code below like this:

```cpp
// replace this with your code
```
#include "TimeAlarms/TimeAlarms.h"

void setup() {
      Time.zone(-4);
    Alarm.timerRepeat(15, Repeats); 
}
void loop() {

}

void Repeats(){
    Spark.publish("myFunction","I");
}

// i am listening to events but nothing comes up

Try putting a led blink inside the repeat function, so you at least know the function is called, and there’s only a problem with the publish. Might help.

Ok to simplify it i did what you suggested

#include "TimeAlarms/TimeAlarms.h"
int led = D7; // This one is the built-in tiny one to the right of the USB jack
int status =1;

void setup() {
    Time.zone(4);
    pinMode(led, OUTPUT);     
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    Alarm.timerRepeat(5, Repeats);
}
void loop() {
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)

}
void Repeats(){
    // Spark.publish(“myFunction”,“I”);
if(status == 1){
     digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)
     status = 0;
}else{
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
     status = 1;   
}
}

After debuging when this line is executed all the following code will stop
    Alarm.timerRepeat(5, Repeats);

This function doesnt seem to work at all . Either bug in the library or bug in my brain :smiley:

Just tested this code and it blinks the D7 LED every 15 seconds:

#include "TimeAlarms/TimeAlarms.h"

void setup()
{
  pinMode(D7,OUTPUT);
  Time.zone(-5);
  Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds    
}

void  loop(){  
  Alarm.delay(100); // wait 100ms for no apparent reason but to pump the timers.
  // Alarms and Timers are only checks and their functions called when you use 
  // this delay function. You can pass 0 for minimal delay. This delay should be 
  // used instead of the normal Arduino delay(), for timely processing of alarms 
  // and timers.
}

void Repeats(){
  digitalWrite(D7, HIGH);
  delay(1000);
  digitalWrite(D7,LOW);
}

Notice the Alarm.delay(1000); in the loop() … I think that’s required to pump it along… doesn’t seem to work without it. Shorter times might be acceptable as well.

Actually here’s the official documentation from which this library was ported:

http://www.pjrc.com/teensy/td_libs_TimeAlarms.html

###Alarm.delay(milliseconds);

Alarms and Timers are only checks and their functions called when you use this delay function. You can pass 0 for minimal delay. This delay should be used instead of the normal Arduino delay(), for timely processing of alarms and timers.

1 Like

True !!! Good Notice !! Thanks a lot … i really was going crazy over here :smiley:

Hi There, I understand that you could use the below line of code to repeat the alarm every day. Is there any way that I could repeat the alarm every alternate day ?

Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
1 Like

Hi @Falcon

There are several ways you could accomplish this:

  • In your morning alarm function, you could just return every other time using a global variable that you update

  • You could set up an initial Alarm.alarmOnce(8,30,0,handler) and then inside that do Alarm.timerOnce( 22460*60, otherHandler) so that you repeat every 2 days as measured in seconds.

  • Similarly you could use the Alarm.alarmOnce( 8+24,30,0,handler) once you get started.

I would try the first idea since it is the easiest.

2 Likes

Thank you for your suggestions and your prompt reply.

There is an obvious flaw in my code below, where the motor would be turned on every day as well as every alternate day. I actually need the motor to be turned on every alternate day, could you please guide me in correcting this flaw.

int newAlarmTime;

void setup () {
        Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day  
 }

void loop () {
      Alarm.delay (1000);
}

void morningAlarm () {
    Serial.println(" Turned on motor ")
     // new alarm time in seconds
    newAlarmTime =  (((8*60)+ 30 + (24*60))*60);
    Alarm.timerOnce( newAlarmTime, onceOnly)
   
}

void onceOnly () {
     Serial.println("Turned on motor");
   }
1 Like

Hi @Falcon

Try this:

bool skipToday = false;

void setup () {
        Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day  
 }

void loop () {
      Alarm.delay (1000);
}

void morningAlarm () {
    if (skipToday) {
      skipToday = false; //come back tomorrow
    } else {
      Serial.println(" Turned on motor ")
      skipToday = true;  //not your day tomorrow
   }
}



1 Like

Thank you @bko will try this and let you know how it goes.

@bko The code works great !! You are awesome.

1 Like

@bko Is there any way that I could clear the alarms programatically (I couldn’t find any, in the documentation or community forum) ? As this would help me to set new times or remove an alarm altogether without having to do a factory reset.

For example, clearing the below alarm or setting a new time for the same alarm.

Alarm.alarmRepeat(8,30,0, MorningAlarm);

Hi @Falcon

You can do this:

  AlarmID_t myAlarm = Alarm.alarmRepeat(8,30,0, MorningAlarm);  // create and remember ID

  //Later on
  Alarm.free(myAlarm);  // free up ID

You have storage for 6 alarms by default.

You can also do low-level reads and writes but you will have figure out the alarm time in seconds, so the interface is not as nice.

1 Like

As you have mentioned a really nice way of freeing the alarm, is there one to enable the alarm for e.g.

Alarm.Enable(myAlarm);

Is this information documented any where ? for my future reference.

PS: You are super fast in your replies.

1 Like

Hi @Falcon

Yes you have Alarm.enable(myAlarm); and Alarm.disable(myAlarm); but these do not make them reusable, they just turn the alarm in question on or off.

This library is ported from here for Particle products. In general you should also read the .h file to see what all the public methods are.

Hi @bko,
In the below code, I am trying to free up all three alarms. I am successful at freeing up only alarm1. Can you please advice how I can free up the remaining two alarms (alarm2 & 3) as well (since I would like to re-use them).

// This #include statement was automatically added by the Particle IDE.
#include "TimeAlarms/TimeAlarms.h"

AlarmID_t alarm1;
AlarmID_t alarm2;
AlarmID_t alarm3;

void setup() {
    AlarmID_t alarm1 = Alarm.timerRepeat(5, morningAlarm);
    AlarmID_t alarm2 = Alarm.timerRepeat(5, eveningAlarm);   
    AlarmID_t alarm3 = Alarm.timerRepeat(5, nightAlarm);  
}

void loop() {
    Alarm.delay(1000);
}

void morningAlarm () {
    Serial.println("calling morningAlarm");
    Alarm.free(alarm1);  // free up ID
}

void eveningAlarm () {
    Serial.println("calling eveningAlarm");
    Alarm.free(alarm2);  // free up ID
}

void nightAlarm () {
    Serial.println("calling nightAlarm");
    Alarm.free(alarm3);  // free up ID
}

Hi @Falcon

Your problem is that you declared three global variables for the alarms but then you also declared local variables in setup() that mask the globals. This means none of the globals are set and the only reason you see one alarm freed is that zero is a valid alarm number and the globals are all initialized to zero.

Try this instead:

void setup() {
    alarm1 = Alarm.timerRepeat(5, morningAlarm);
    alarm2 = Alarm.timerRepeat(5, eveningAlarm);   
    alarm3 = Alarm.timerRepeat(5, nightAlarm);  
}

This worked for me when I tested it.

3 Likes