Snippets for a simple C state machine

While developing firmware for an LED-based user feedback system, I needed to implement a very simple state machine in C. After spending a few confused hours sweating in black and white, trying to keep track of state variables in my head “the old way”, I found this blog entry which proposes convenient and concise macro definitions for a simple state machine. Suddenly I was coding in full color, and the structure of the program fell into place quickly. Thanks, Jim!

The way a program like this works is by keeping track of the “previous”, “current”, and “next” states in variables. The code inside one of the states is continuously executed, until the “next” state changes. Each time a state is entered or exited, the variables are updated. But by moving the state variable housekeeping to the header file, our switch statement in main() is much more straightforward.

Here’s the code for the macros in the header file:
#define STATE_ENTRY_ACTION if ( currentState != previousState ) {
#define STATE_TRANSITION_TEST previousState = currentState; } if ( nextState == currentState ) {
#define STATE_TRANSITION_TEST_EXCLUSIVE } else if ( nextState == currentState ) {
#define STATE_EXIT_ACTION } if ( nextState != currentState ) {
#define STATE_EXIT_ACTION_EXCLUSIVE } else if ( nextState != currentState ) {
#define STATE_END currentState = nextState; } break;

The EXCLUSIVE macros are used if you want to ensure that the state machine is run at least three times per state, making the timing of the function call more predictable.

Now adding a state here or there, or inserting functions at the right point in the routine is easy:

void main() {
   while( true ) {
      switch ( state ) {
         case state_n:

I did not include a default case in this example, but for a real application it’s a good idea to always include one.

2 thoughts on “Snippets for a simple C state machine

  1. Hi, thanks for this post. I am trying to implement an small state machine at work and the implementation shown in this page sounds easy to do. However, I went to the original post where you get the idea, but seems the link to StateMachine.c file shown under that page does not work anymore. Do you have by any chance that file so you can share it with anyone?, It seems the original creator of that files moved his blog to wordpress and left the old blog to fall in the oblivion.

  2. Raul,
    I don’t have that file. However, as I recall there isn’t really much more to the code than what you see above. The macros go in the .h file, and then you have void main() in the .c file that uses the macros.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.