int[] sequence = generate_sequence(); for (int round = 0; round < 10; round++) { for (int position = 0; position <= round; position++) { short pause show green LED pair for sequence[position] long pause turn off green LED pair } for (int position = 0; position <= round; position++) { while a button is not pressed { do nothing } show green LED pair for sequence[position] if the wrong button was pressed { display "lose" while a button is still pressed { display green LED pair } turn off green LED pair while (true) { continue to display "lose" } } else { while a button is still pressed { display green LED pair } turn off green LED pair } } } while true { display "win" }However, this is a hardware class. So why look at this program? Well, a flow chart of this program's execution (its control flow) can be converted into a finite state machine! Let's see how this works by examining the conversion of both 'if' and 'for' statements. Note that in a state machine you cannot call a function but you can output signals that cause things to happen in other modules.
Procedural Code | Flow Chart | State Diagram |
if (a) { do_this(); } else { do_that(); } |
||
for(i=0; i < 10; i++); |