I would recommend using a timer and set the interval for X milliseconds. The X will depend on the minimum time slice your application requires. Then within the timer call back function, have a simple switch statement that will serve as the backbone of your statemachine. Every time interval X, the state machine code will execute. A code snippet is below:
switch(State)
{
case 0:
if (temp > tempsetpoint + hysteresis/2)
{
State++;
}
else
{
DIG_Out_Line (1, 0, 0, 1); //Turn heater on
}
break;
case 1: //Turn heater off
if (temp < tempsetpoint - hysteresis/2)
{
State--;
}
else
{
DIG_Out_Line (1, 0, 0, 0); //Turn heater off
}
break;
default :
break;
}
For this example, the state machine approach is just used to control
the 2 on/off state of a heater, but this idea can easily be extended to control a much larger system. I hope this helps. Good luck.
-Steve