For all of my professional career, I have been working with state machines in one form or another, and I used to hate implementing them. Since most of the code in my early career was done on a true microcontroller in a single thread, implementing a state machine meant having a state variable that indicated the current state and a giant case statement for each state. The state machine was easy to draw, but quite painful to implement and even worse to change.
Still, for some things, state machines really are the right way to go—especially when you're implementing a control system.
A finite-state machine (or state machine for short) is a model of a system with a finite set of conditions (states) and rules or events that cause a transition between conditions. I'll explain them more in the next section.
In this section, we will implement...