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
}