Updating timeAlarm integers via particle cloud function

Hi

Im developing an alarm/timer that can be programmed and updated via the cloud functions. To get the hang of it I wrote the below that sets a repeat timer and the value is changed and updated via a cloud function.
Even though the print shows the value is upated my timer is not behaving as expected. The time programmed does not always match what was sent through the cloud function. Do I need to do something like cancel the old timer before loading the new value or should it just update

thanks in advance

#include "Particle.h"
#include "TimeAlarms.h"
#include <string.h>

AlarmId ALARM_ID;//create alarm id for repeat alarms
bool getTime=1; //flag to get local time when connected

int ShutterRepeatValue=2;//holds the repeat value from the cloud function.
int ShutterInitialRepeatValue=2;//initial repeat valued

/********************************************************************************************
*********************************************************************************************
*********************************************************************************************/

void setup()
{
  Time.zone(+2);
  Particle.keepAlive(20);
  Serial.begin(115200);
  Particle.function("SetRepeatTriggers", setShutterRepeatTriggers);//set shutter close from cloud
  ALARM_ID = Alarm.timerRepeat(ShutterInitialRepeatValue, Repeat);            // timer for every 15 seconds    
 
}

void  loop(){  

  //get the local time from cloud
  if((Particle.connected())&&(getTime==1)){
    Particle.process();  // allow for all the data to filter throug
    Particle.publish("time",Time.timeStr(),PRIVATE);
    getTime=0;
  }
   // Serial.println(Time.format(TIME_FORMAT_ISO8601_FULL));
   digitalClockDisplay();
   Alarm.delay(1000); // wait one second between clock display
   if ((ShutterInitialRepeatValue!=ShutterRepeatValue)&&(ShutterRepeatValue>0)){
      ShutterInitialRepeatValue = ShutterRepeatValue;
      Alarm.timerRepeat(ShutterInitialRepeatValue, Repeat);  
    }

    if (ShutterRepeatValue==0) Alarm.disable(ALARM_ID);//disable

}
//repeat alarm function
void Repeat(){
  Serial.printf("%ds repeat timer\n",ShutterRepeatValue); // printer the value sent from the cloud  
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(Time.hour());
  printDigits(Time.minute());
  printDigits(Time.second());
  Serial.println(); 
}

// cloud function
int setShutterRepeatTriggers(String param){
  ShutterRepeatValue = param.toInt();
  Serial.printf("updated timer value %d\n",ShutterRepeatValue);
  return ShutterRepeatValue;
}

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

Thanks in advance for any assistance.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.