1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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

Download StateMachine.bas
(1.5 KB , Aug. 26, 2008)