Skip to main content

How-to articles about designing safe, reliable, and secure embedded systems.

State Machines for Event-Driven Systems

Posted on: May 04, 2016

State machines are perhaps the most effective method for developing robust event-driven code for embedded systems.

If you looked closely at just about any computer system around you, you'd probably find out that at any given time it is doing... nothing useful. That is, most of the time the CPU is either hibernating in one of its power-saving modes or busily "twiddling its thumbs" by executing an idle loop. (Modern PCs are particularly good at the latter form of doing nothing—they can "twiddle their thumbs" a billion times per second all while driving a screen saver.)

How to Code a State Machine in C or C++

Posted on: May 04, 2016

A state machine is any object that behaves different based on its history and current inputs.  Many embedded systems consist of a collection of state machines at various levels of the electronics or software.

This article provides an introduction to state machines fundamentals plus practical advice for implementing them in C or C++. I hope that these simple techniques can become more common, so that we can more often easily see the state-machine structure right from the source code.

Barr Group's engineers wrote these resources for working developers. When firmware and electronics end up in litigation, we also serve as software expert witnesses.