0
0
Embedded Cprogramming~5 mins

Why state machines are used in embedded in Embedded C

Choose your learning style9 modes available
Introduction

State machines help organize how a device behaves step-by-step. They make it easier to control what happens next based on current conditions.

When controlling a device that has different modes, like a washing machine with wash, rinse, and spin steps.
When handling inputs that change over time, like buttons pressed in a certain order.
When you want to keep track of what the device did before to decide what to do next.
When you need clear and simple code to manage complex behaviors in small devices.
When debugging is easier if the device's state is known at any time.
Syntax
Embedded C
typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_STOPPED
} State;

State currentState = STATE_IDLE;

switch(currentState) {
    case STATE_IDLE:
        // do idle tasks
        break;
    case STATE_RUNNING:
        // do running tasks
        break;
    case STATE_PAUSED:
        // do paused tasks
        break;
    case STATE_STOPPED:
        // do stopped tasks
        break;
}

States are usually defined as named constants using enums for clarity.

A switch-case structure is commonly used to decide what to do based on the current state.

Examples
A simple state machine with two states controlling a light.
Embedded C
typedef enum { OFF, ON } LightState;
LightState light = OFF;

if (light == OFF) {
    // turn light on
    light = ON;
} else {
    // turn light off
    light = OFF;
}
Using switch-case to handle door lock states.
Embedded C
typedef enum { LOCKED, UNLOCKED } DoorState;
DoorState door = LOCKED;

switch(door) {
    case LOCKED:
        // wait for code input
        break;
    case UNLOCKED:
        // open door
        break;
}
Sample Program

This program shows a simple state machine changing states and printing the current state each time.

Embedded C
#include <stdio.h>

typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_STOPPED
} State;

int main() {
    State currentState = STATE_IDLE;

    // Simulate events changing state
    printf("Current state: IDLE\n");

    currentState = STATE_RUNNING;
    if (currentState == STATE_RUNNING) {
        printf("Current state: RUNNING\n");
    }

    currentState = STATE_PAUSED;
    if (currentState == STATE_PAUSED) {
        printf("Current state: PAUSED\n");
    }

    currentState = STATE_STOPPED;
    if (currentState == STATE_STOPPED) {
        printf("Current state: STOPPED\n");
    }

    return 0;
}
OutputSuccess
Important Notes

State machines make embedded code easier to read and maintain.

They help avoid complicated if-else chains by clearly separating states.

Using enums for states improves code clarity and reduces errors.

Summary

State machines organize device behavior into clear steps called states.

They help manage complex actions in embedded systems simply.

Using states makes code easier to understand and debug.