Just as an exercise, this is an example of typical co-operative scheduling. As C does not have co-routines there is a trick needed to split the time consuming phases of tasks into sections, by switching into thenext section through action codes.
This example can easily be extended by more specific return codes, as for priorities, or synchronization to time marks...
// Simplified co-operative scheduling for Arduino
// 2010 by DeSilva v1.0
byte (*action[])(byte,long) ={pA, pB};
const int nmbActions =sizeof(action)/sizeof(&pA);
byte actionCode[nmbActions] ;
byte nextAction =0;
void setup() {
}
void loop() {
actionCode[nextAction] = (*action[nextAction])(actionCode[nextAction], millis());
if (++nextAction>=nmbActions) nextAction = 0;
}
byte pA(byte code, long theTime){
switch (code) {
case 0:
return 1;
case 1:
return 2;
default:
return 0;
}
}
byte pB(byte code, long theTime){
return 0;
}