I would do something like this:
None con 0
DoingAction1 con 1
DoingAction2 con 2
DoingAction3 con 3
ProgramState var byte
'Initialization
ProgramState = None
MainLoop:
'---------- Serin Command - Get Data from Master uP -------------
Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p1_x,PB_mode,p2_y,p1_y ]
. . .
if ProgramState = None then
if switch1 <> 0 then ProgramState = DoingAction1
if switch2 <> 0 then ProgramState = DoingAction2
if switch3 <> 0 then ProgramState = DoingAction3
endif
if ProgramState = DoingAction1 then gosub DoAction1
if ProgramState = DoingAction2 then gosub DoAction2
if ProgramState = DoingAction3 then gosub DoAction3
. . .
Serout USERCPU, OUTBAUD, [255,255,127,relayA,127,127,pwm2,pwm1,127,127,127,1 27,pwm9,127,127,127,127,127,127,127]
Goto MainLoop:
'-------- SUBROUTINES ----------------------------------------
DoAction1:
*Do some stuff...*]
if Done then ProgramState = None
return
DoAction2:
*Do some other stuff...*]
if Done then ProgramState = None
return
DoAction3:
*Do some fluff...*]
if Done then ProgramState = None
return
Stop
(Standard disclamer)
[edit]
Note 1: As Joe mentioned, more than one button could be pressed at the same time. The way I’ve coded it, button 3 would have the highest priority, and button 1 the lowest (because of the order of the if statements.)
Note 2: I didn’t indicate how the variable “Done” gets set… Or even that it’s a variable, for that matter. It could be a logical expression like “elapsedProgramCycles > 80” or “proximitySensor = 1”.
Note 3: Make sure your subroutines return quickly. They should only calculate or set outputs. It won’t work to do something like this:
for i = 1 to 80
pwm1 = 255
next i
return
Instead, it should be something like:
' Make sure JJ gets initialized somewhere. (Like when you set the ProgramState variable.)
pwm1 = 255
JJ = JJ+1
if JJ = 80 then ProgramState = None
return
[/edit]