0
0
Embedded Cprogramming~20 mins

Why state machines are used in embedded in Embedded C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Embedded State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use state machines in embedded systems?

Which of the following best explains why state machines are commonly used in embedded programming?

AThey replace the need for interrupts and timers in embedded systems.
BThey automatically optimize the code for faster execution without programmer intervention.
CThey allow embedded devices to run multiple operating systems simultaneously.
DThey help organize complex behavior into clear states and transitions, making code easier to understand and maintain.
Attempts:
2 left
💡 Hint

Think about how breaking down tasks into steps helps manage complexity.

Predict Output
intermediate
2:00remaining
Output of a simple embedded state machine code

What will be the output of this embedded C code simulating a state machine?

Embedded C
typedef enum {IDLE, RUNNING, ERROR} State;

State current_state = IDLE;

void run_state_machine() {
    switch(current_state) {
        case IDLE:
            printf("Idle state\n");
            current_state = RUNNING;
            break;
        case RUNNING:
            printf("Running state\n");
            current_state = ERROR;
            break;
        case ERROR:
            printf("Error state\n");
            current_state = IDLE;
            break;
    }
}

int main() {
    for(int i=0; i<3; i++) {
        run_state_machine();
    }
    return 0;
}
A
Idle state
Idle state
Idle state
B
Idle state
Running state
Error state
C
Running state
Error state
Idle state
D
Error state
Running state
Idle state
Attempts:
2 left
💡 Hint

Look at how the state changes after each print.

🔧 Debug
advanced
2:00remaining
Identify the bug in this embedded state machine code

What error will this embedded C code cause when run?

Embedded C
typedef enum {START, PROCESS, STOP} State;

State current_state = START;

void run_state_machine() {
    switch(current_state) {
        case START:
            printf("Start\n");
            current_state = PROCESS;
            break;
        case PROCESS:
            printf("Process\n");
            // Missing break here
        case STOP:
            printf("Stop\n");
            current_state = START;
            break;
    }
}

int main() {
    for(int i=0; i<4; i++) {
        run_state_machine();
    }
    return 0;
}
A
The output will be:
Start
Process
Stop
Start
Process
Stop
B
The output will be:
Start
Process
Process
Start
Process
Process
CThe program will crash with a segmentation fault.
D
The output will be:
Start
Stop
Start
Stop
Attempts:
2 left
💡 Hint

Check what happens when a break is missing in a switch-case.

📝 Syntax
advanced
2:00remaining
Which option has correct syntax for a state machine transition?

Which of the following code snippets correctly updates the state variable in an embedded C state machine?

Acurrent_state == RUNNING;
Bcurrent_state => RUNNING;
Ccurrent_state = RUNNING;
Dcurrent_state := RUNNING;
Attempts:
2 left
💡 Hint

Remember the assignment operator in C.

🚀 Application
expert
2:00remaining
How many states are in this embedded state machine?

Given this embedded C code, how many unique states does the state machine have?

Embedded C
typedef enum {
    INIT = 0,
    WAIT = 1,
    RUN = 2,
    PAUSE = 3,
    STOP = 4
} State;

State current_state = INIT;

void run_state_machine() {
    switch(current_state) {
        case INIT:
            current_state = WAIT;
            break;
        case WAIT:
            current_state = RUN;
            break;
        case RUN:
            current_state = PAUSE;
            break;
        case PAUSE:
            current_state = STOP;
            break;
        case STOP:
            current_state = INIT;
            break;
    }
}
A5
B4
C6
D3
Attempts:
2 left
💡 Hint

Count all the enum values representing states.