State machine implementation in C example
Learn how to implement state machines in C with examples using if else statement, switch statement and function pointers. Subscribe here to enable complete access to all technical articles, online tests and projects.
This article will help you to gain required knowledge to successfully complete online tests, technical projects which are required to complete online courses and earn course completion certificates for software jobs.
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
To Read Full Article
You need to subscribe & logged in
Subscribe
Exercises, Solutions
Want to contribute a new article? Write and upload your article information
here.