State machine implementation in C example


Advertisements

Share



Learn how to implement state machines in C with examples using if else statement, switch statement and function pointers.

What is state machine in C?

A state machine stores the status of something at a given time. The status changes based on inputs and the state machine logic provides the required output for a given input. The diagram that defines each of the state and the transition from one state to another for a given input is called a state diagram. When there is a change in input it is called an event. An input event causes a change of state depending on the rules of the current state.

State machine using if else statement

  To begin with, let's write a simple c program to change the state of an led. Led has two states LED_ON state and LED_OFF state. When LED is in off state we want to turn it ON and when the LED is in ON state we want to turn it OFF using an if else statement.


#include "stdio.h"
#include "unistd.h"
enum states
{
  LED_ON,
  LED_OFF
};

int main()
{
  enum states state = LED_OFF;
  while(1)
  {
    if(state == LED_OFF)
    {
      printf("Led on function to be called\n");
      state = LED_ON;
    } else {
      printf("Led off function to be called\n");
      state = LED_OFF;
    }
    sleep(1); // sleep for a second
  }
    return 0;
}

Output:


# ./a.out
Led on function to be called
Led off function to be called
Led on function to be called
Led off function to be called
Led on function to be called
Led off function to be called
Led on function to be called
Led off function to be called

State machine using switch case

  Let's write another state machine program that will have 4 states and each state changes the led state to immediate next state. Each state has a different sleeping time as shown in program below. This time to switch the led state to the immediate next state we will use a switch statement.



#include "stdio.h"
#include "unistd.h"
enum states
{
  LED_ON1S,
  LED_OFF1S,
  LED_ON3S,
  LED_OFF3S
};

int main()
{
  enum states state = LED_OFF1S;
  while(1)
  {
    switch(state)
    {
      case LED_ON1S:
        state = LED_OFF1S;
        printf("Setting Led state OFF for 1S\n");
        break;
      case LED_OFF1S:
        state = LED_ON3S;
        printf("Setting Led state ON for 3S\n");
        sleep(1);
        sleep(1);
        break;
      case LED_ON3S:
        state = LED_OFF3S;
        printf("Setting Led state OFF for 3S\n");
        sleep(1);
        sleep(1);
        break;
      case LED_OFF3S:
        state = LED_ON1S;
        printf("Setting Led state ON for 1S\n");
        break;
      default:
        state = LED_OFF1S;
        printf("Setting Led default state OFF for 1S\n");
    }
    sleep(1); // sleep for a second
  }
    return 0;
}

Output:

  Let's compile the above program we wrote in the previous step. So we find that led state changes between the four led states as defined in the enum with different sleeping time.



# ./a.out
Setting Led state ON for 3S
Setting Led state OFF for 3S
Setting Led state ON for 1S
Setting Led state OFF for 1S
Setting Led state ON for 3S
Setting Led state OFF for 3S
Setting Led state ON for 1S
Setting Led state OFF for 1S
Setting Led state ON for 3S
Setting Led state OFF for 3S
    

State machine using function pointers

  Let's write another state machine program that will have 3 states such as Red, Green and blue. Each state changes the led colour to next colour. The program is as shown below. This time to switch the led colour to next we will use a function pointer.
We have declared a global variable as task_id. We ask user to enter input on which state to switch next. The function pointer f_ptr is declared and initialized to function LED_BLUE. To get started the function pointer is called in the main function.



#include "stdio.h"
#include "unistd.h"

unsigned int task_id;

void LED_BLUE();
void LED_RED();
void LED_GREEN();

void (*f_ptr)() = LED_BLUE;

void LED_BLUE()
{
  sleep(1);
  printf("sleeping for 1blue second\n");
  switch(task_id)
  {
  case 0:
    f_ptr = LED_BLUE;
    break;
  case 1:
    f_ptr = LED_RED;
    break;
  case 2:
    f_ptr = LED_GREEN;
    break;
  }
}

void LED_GREEN()
{
  sleep(1);
  printf("sleeping for 1green second\n");
  switch(task_id)
  {
  case 0:
    f_ptr = LED_GREEN;
    break;
  case 1:
    f_ptr = LED_BLUE;
    break;
  case 2:
    f_ptr = LED_RED;
    break;
  }
}

void LED_RED()
{
  sleep(1);
  printf("sleeping for 1red second\n");
  switch(task_id)
  {
  case 0:
    f_ptr = LED_RED;
    break;
  case 1:
    f_ptr = LED_GREEN;
    break;
  case 2:
    f_ptr = LED_BLUE;
    break;
  }
}

int main()
{
  while(1)
  {
    printf("Enter the choice: 0,1,2 or > 2 for exit\n");
    scanf("%d",&task_id);
    if(task_id > 2)
    {
      return 0;
    }
    (*f_ptr)();
  }
  return 0;
}

Output:

  Let's compile the program sm3.c we wrote in the previous step. So we find that led state changes between the four led states as defined in the enum with different sleeping time.



# ./a.out
Enter the choice: 0,1,2 or > 2 for exit
1
sleeping for 1blue second
Enter the choice: 0,1,2 or > 2 for exit
0
sleeping for 1red second
Enter the choice: 0,1,2 or > 2 for exit
2
sleeping for 1red second
Enter the choice: 0,1,2 or > 2 for exit
0
sleeping for 1blue second
Enter the choice: 0,1,2 or > 2 for exit
2
sleeping for 1blue second
Enter the choice: 0,1,2 or > 2 for exit
3
#

Exercises, Solutions

High
High
High
Medium
High
High
Medium
High

Want to contribute a new article? Write and upload your article information .
Share

 Articles

 C Programming

 Linux Software

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

 Embedded System Software

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs