rem StateMachine.bas rem rem last revision on 21 Nov 2002 rem rem simple example on how a state machine works // define constant values for each state CONST WAIT = 0 CONST INIT = 1 CONST OP_MODE1 = 2 CONST OP_MODE2 = 3 CONST PRINT_RESULT = 4 // define and initialize a state variable in the reset macro RESET_MACRO: #state = WAIT END MAIN_MACRO: // use a select statement in the main macro to switch the handling accordingly select #state case WAIT: // start operation with F1 if |F1_BUTTON = on then #state = INIT endif case INIT: gosub do_init case OP_MODE1: gosub do_op1 case OP_MODE2: gosub do_op2 case PRINT_RESULT: gosub do_output #state = WAIT endsel END do_init: // initialize variables // ... // switch to operation mode one #state = OP_MODE1 RETURN do_op1: // change #state according to trigger event if |CAPTURE_PIN = on then #state = OP_MODE2 else write "OP_1" endif RETURN do_op2: // change #state according to trigger event if |CAPTURE_PIN = off then #state = PRINT_RESULT else write "OP_2" endif RETURN do_output: // output results to display/printer/serial device write " Result = " + &RESULT + " " RETURN