You might be able to play around with microcontrollers and drive all the peripherals, but when you’re asked to write a complete code set, you lack logic and a framework. This indicates your programming skills are still at a relatively low level. You need to learn a good programming framework or a programming philosophy! Examples include modular programming, state machine programming, and layered programming. This article will discuss state machine programming.
1. What is a state machine?
A state machine has five elements:
- State: The stable operating state of a system at a given moment. A system may have multiple states throughout its operating cycle. For example, an electric motor has three states: forward, reverse, and stopped.
- Transition: Transitions don’t occur automatically; they require external influence on the system. A stopped motor won’t start rotating on its own; it must be powered on to start it.
- Event: Something meaningful to the system occurs at a given moment. State machine transitions occur because of events. For a motor, applying positive voltage, applying negative voltage, and disconnecting the power supply are events.
- Actions: During the transition process of a state machine, the state machine may perform other actions. These actions are the state machine’s response to events. Applying positive voltage to a stalled motor causes the motor to transition from stalled to forward rotation and simultaneously start the motor. This startup process can be considered an action, a response to the power-on event.
- Conditions: A state machine doesn’t always respond to events. Even with an event, the state machine must meet certain conditions before a state transition occurs. Let’s take a stalled motor as an example. Even though the circuit breaker is on, if there’s a problem with the power supply circuit, the motor won’t start.
2. Example: Problem to be solved
The circuit is shown below:
- The components include a microcontroller (MCU), a button (K0), and LEDs (L1 and L2).
Functional Description:
- L1L2 state transition sequence: OFF/OFF —> ON/OFF —> ON/ON —> OFF/ON —> OFF/OFF.
- L1L2 states are controlled by buttons. Each state transition requires five consecutive button presses.
- L1L2’s initial state is OFF/OFF.

State Transition Diagram
In state machine programming, the correct sequence is to create the state transition diagram first, followed by the program. The program should be written based on the designed state diagram.
The following state transition diagram for a button-controlled running light is drawn using UML (Unified Modeling Language) syntax elements. The syntax isn’t entirely standard, but it’s sufficient for explaining the problem.

In the diagram above, rounded rectangles represent the various states of the state machine, with the state names labeled inside.
Lines or arcs with arrows represent state transitions, starting from the initial state and ending at the next state.
The text in the diagram describes the transitions, using the format: event [condition] / action list (the last two items are optional).
The phrase “Event [Condition] / Action List” means: if an “event” occurs in a certain state and the state machine satisfies the “[Condition],” then this state transition is executed, and a series of “actions” are generated in response to the event. In this example, I use “KEY” to represent a keystroke event.
The diagram shows a solid black dot, representing an unpredictable state in which the state machine is before operation. Before execution, the state machine must forcibly transition from this state to the initial state. This transition can have an action list (as shown in Figure 1), but does not require an event trigger.
The diagram also contains a circle containing a solid black dot, representing the end of the state machine’s lifecycle. Since the state machine in this example is endless, no state points to this circle.