User`s manual

Dynamic C Users Manual digi.com 55
5.2.1 Solving the Real-Time Problem with a State Machine
Here is what a state machine solution might look like.
If there are other tasks to be run, this control problem can be solved better by creating a loop that processes
a number of tasks. Now each task can relinquish control when it is waiting, thereby allowing other tasks to
proceed. Each task then does its work in the idle time of the other tasks.
task1state = 1; // initialization:
while(1){
switch(task1state){
case 1:
if( buttonpushed() ){
task1state=2; turnondevice1();
timer1 = time; // time incremented every second
}
break;
case 2:
if( (time-timer1) >= 60L){
task1state=3; turnondevice2();
timer2=time;
}
break;
case 3:
if( (time-timer2) >= 60L){
task1state=1; turnoffdevice1();
turnoffdevice2();
}
break;
}
/* other tasks or state machines */
}