Reset period value in software timers

I am wondering if there is a way to alter the period value for software timers.

I’ve tested the functioning of a basic timer on my photon and happy that is working.

Now I want to use timers for varying the duty cycle of a heater. In the code sample below I have set up a timer called timHeat prior to the setup loop. I am unsure if timers can be created in other parts of the code, or the best way to achieve the result I’m after.

The timer calls the checkHeat function, and I want to vary the period based on that function. In the code snippet below I’ve attempted this by disposing the original timer and then creating a new instance of the same timer. It doesn’t compile and the errors in Particle Dev seem a bit cryptic.

Any thoughts?

void checkHeat()  //called by timHeat
{
  int iPeriod=iHeatCycle; //timer period
  timHeat.dispose();  // dispose of old timer before arming new one

  if (iTempTarg>=0) //target is set when number is positive
  {
    if (iTempAct < iTempTarg) //if temp actual < temp target, apply Heat
    {
      //cycle on/off time according to power%
      if (iHeatMode==0 || iDutyPercent==100)  //apply power this heat cycle
      {
        iHeatMode=1;  //change heat mode indicator
        iPeriod=iDutyPercent*iHeatCycle/100;  //set duty cylce
      }
      else  //no power this heat cycle
      {
        iHeatMode=0;
        iPeriod=(100-iDutyPercent)*iHeatCycle/100;
      }
    }
    else  //target temperature has been reached
    {
      //turn heater off
      iHeatMode=0;
      iPeriod=iHeatCycle;
    }
  }
  else  // no target is set, ensure heater is off
  {
    iHeatMode=0;
    iPeriod=iHeatCycle;
  }
  Timer timHeat(iPeriod,checkHeat); //arm timer
}

I said that two days ago when 0.4.7 just came out.

I`ve used the software timers , just for D7 flash right now, but one feature which is missing that would have been very nice is a way to change the period. eg timer.period(300)

@arniew, @peter_a, FreeRTOS does support a “change period” capability but it has not been brought out to the user yet. I’ll ping @mdma and post an issue to see if it can be added :smile:

1 Like

Thanks, I’m sure there will be more than peter_a and myself who will use that feature.

In the meantime I can use a timer to count cycles and trigger things when count goes down to zero.

2 Likes

Is there an update to this?

I don’t know of any firmware update, but my workaround is to shorten the duty cycle of the timer and have an integer count down to zero. At zero it triggers the function I want. The duty cycle is varied by changing the value of the integer.

@arniew, the upcoming 4.8 release will include the changePeriod() function :sunglasses:

5 Likes