Em 17/02/2014 04:31, Ruben J=F6nsson escreveu: >> I also sometimes find the same steps in a loop, and I might have: >> case 3: >> case 12: >> lift_cylinder_up(); >> // other things >> extend_step++; >> break; // back to main >> >> Kerry >> > Yes, the default and IDLE or INIT state usually shares the same code > so that a runaway state variable ends up at a known state. > > default: > case STATE_INIT: > state=3DSTATE_IDLE; > // init other variables here > // Fall through to idle=09 > case STATE_IDLE: > if (startCondition){ > state=3DSTATE_MEANINGFUL_NAME_HERE; > // Do first step of active states here > stateTimer=3D20; // times x us until next state > } > break; > case STATE_MEANINGFUL_NAME_HERE: > if (!stateTimer){ > state++; > // Do next step of active states here > } > break; > > =20 > /Ruben The same can be done for co-routines, just modify the macro "TaskStart()" to add the "default:" clause after (or before) the "case 0:" Now, the two different 'cases' sharing the same code is simply solved using a 'goto'. At the point equivalent to "case 3:" insert a "goto case_12;" and at the point equivalent to "case 12:" create a label "case_12:". I would like to note that the co-routine style yields code that can be used directly with a full-blown RTOS, it is just a matter of changing the macros. The macros "TaskStart()" and "TaskEnd()" are defined as empty macros, and the macros "Yield()" and "Sleep(t)" are changed to invoke whatever function names the RTOS uses for them. Isaac --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .