Skip to content

Control actions with state machine

In this tutorial you will learn how to control your device with well organized code using state machine approach instead of hundred's of if-s embeded in each other.

Table of contents:

State machine

State machine is defined by few elements:

  • set of states,
  • set of actions,
  • set of transitions.

A state is a description of the current status of a system.

When one of a predefined conditions is fulfilled or when an event is received an action is generated.

In result a transition from current state to a next state occures.

A set of transitions can be defined in one of possible tabular form.

Tabular form 1:

Tabular form 2:

In practical application it is much more handy to have transition rules in different form where in each line one transition is defined:

I call a transition given in this form a route.

It is usefull to represent a state machine as a graph with states as nodes and actions as arrows:

In most cases one of states is distinguished from others: this is an initial state, the first state you start your system. There can be also a set of other special states -- every time you reach one of them, something unusual must have happened. For example STOP state may mean a contoled stop of your system, while TERMINATE may mean an uncontrolled stop of your system being a result of some unexpected data or component malfunction. I classify STOP as an unusual state because typically every system, when started is expected to run forever.

If you hava a definition of a state machine (you know a set of sates, actions and all possible transitions) you can easily "execute" it with te code similar to the follwing:

Simple example number 1

In this example you will work with a very simple circuit:

As you can see, there are:

  • Arduino Uno board to control all the interactions.
  • LED controlled by Arduino Uno in response to button press action.
  • Button ON connected with green wire to the Arduino. You will use this button to turn LED ON.
  • Button OFF connected with orange wire to the Arduino. You will use this button to turn LED OFF.

In the "classical" approach probably you will write code more o less similar to the following:

This system can be also control with the following state machine:

State machine depicetd on the above image is implemented below:

At this moment this may seem like an unnecessary complication of simple things, but I hope you will appreciate this approach in subsequent, more and more complex versions of this system.

Simple example number 2

In this example you will use sligtly modfied version of circuit from example 1: there is only one button to turn led on or off.

Susprisingly, one button simplifies the design a lot:

However I propose you to work with the state machine given below. There are two reasons for that:

  • You will see how to pass control from one state to another with variables.
  • It would be easier to extend this state machine in next modiffication of my example.

Simple example number 3

In this example you will use sligtly modfied version of circuit from example 2: you add one more led and second button to select the number of active LEDs -- one or two.

Simple example number 4

In this example you will use exactly the same circuit as in example 3 but will chane LEDs behaviour from constant ligt to blinking.