NOTE: This paragraph is important to understand the principles of this implementation of a finite state machine. When understood and used in the right way, a stable finite state machine is created. All example projects published on this website are made according to this implementation. 

To keep record of Finite state status, the following state modes are defined:  

ModeDescriptionBehaviour of State
HIBHibernateState is not active
COM[1]ComputingState was executed in the current cycle and will be in the next
RUNRunningState will be executed in the current cycle
PENPending to runState will be executed in the next cycle of the main loop

It is important to understand the meaning of a “cycle”. The loop part of the sketch will be repeated endlessly.  Part of the loop is the execution of the FSM cycle. In every cycle all active FSM states will be executed. The handling of the states are according to the following: The cycle is running through all defined states. When a state is in running mode (RUN) the code of this state will be executed and at the end the state’s mode is put on computing (COM) or when TransistionToState is called (once or more times) in the code of the running state, the state is put on hibernate (HIB) and the state to goto is set on pending (PEN).  At the end of the cycle -when the sketch has run trough all defined states- all computing (COM) and pending (PEN) states are put on mode RUN (running). And now a new cycle is started again.  

With the standard sketch it is also possible to have more than one FSM sequence running. So it is possible to run more sequences in parallel (multitasking). The start of an extra FSM sequence is possible from any other state (for example direct from the “START” state). See also function TransitionToState. When working with more than one sequence, it is of course possible to “communicate” between these sequences. When communicating between sequences, exchange only control information. Exchanging data between states is not recommended. 

For handling the states, every state keeps a record for administration purposes, e.g. the name, the state number (PID, Process IDentification), total run time, the actual state and the thread- or FSM sequence number. The state of the records can be viewed in the serial monitor (see chapter 6). The only thing to do is call the function TransitionToState(“<statename>”) to go to another state. TransitionToState called one time from the current state takes place in the same thread or FSM sequence. When TransitionToState is called n times from a finite state, the sketch starts n-1 extra FSM sequences.  A transisition to the standard “END” state, puts the state on HIB; not starting another state.

Remark:
Some history: The defined states of a Finite State Machine state, is an inheritance of the digital VAX operating system, called VMS. VMS was the first OS with virtual memory.