0
0
Embedded Cprogramming~20 mins

Event-driven state machine in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event-driven State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple event-driven state machine

What is the output of this embedded C code simulating a simple event-driven state machine?

Embedded C
#include <stdio.h>

typedef enum {STATE_IDLE, STATE_RUN, STATE_STOP} State;
typedef enum {EVENT_START, EVENT_STOP, EVENT_RESET} Event;

State current_state = STATE_IDLE;

void handle_event(Event e) {
    switch(current_state) {
        case STATE_IDLE:
            if (e == EVENT_START) {
                current_state = STATE_RUN;
                printf("Started\n");
            }
            break;
        case STATE_RUN:
            if (e == EVENT_STOP) {
                current_state = STATE_STOP;
                printf("Stopped\n");
            } else if (e == EVENT_RESET) {
                current_state = STATE_IDLE;
                printf("Reset to Idle\n");
            }
            break;
        case STATE_STOP:
            if (e == EVENT_RESET) {
                current_state = STATE_IDLE;
                printf("Reset to Idle\n");
            }
            break;
    }
}

int main() {
    handle_event(EVENT_START);
    handle_event(EVENT_STOP);
    handle_event(EVENT_RESET);
    return 0;
}
A
Started
Reset to Idle
Stopped
B
Reset to Idle
Started
Stopped
C
Started
Stopped
Reset to Idle
D
Stopped
Started
Reset to Idle
Attempts:
2 left
💡 Hint

Trace the state changes step by step and note the printed messages.

🧠 Conceptual
intermediate
1:30remaining
Understanding event-driven state machine transitions

In an event-driven state machine, what best describes the role of the event handler function?

AIt directly modifies hardware registers without checking states.
BIt resets the state machine to the initial state on every event.
CIt only logs events without affecting the state machine.
DIt changes the current state based on the received event and current state.
Attempts:
2 left
💡 Hint

Think about how the state machine reacts to events.

🔧 Debug
advanced
2:00remaining
Identify the error in this event-driven state machine code

What error will this code produce when compiled or run?

Embedded C
#include <stdio.h>

typedef enum {IDLE, RUNNING} State;
typedef enum {START=1, STOP=2} Event;

State current_state = IDLE;

void handle_event(Event e) {
    switch(current_state) {
        case IDLE:
            if (e = START) {
                current_state = RUNNING;
                printf("Running\n");
            }
            break;
        case RUNNING:
            if (e == STOP) {
                current_state = IDLE;
                printf("Idle\n");
            }
            break;
    }
}

int main() {
    handle_event(START);
    handle_event(STOP);
    return 0;
}
ALogical error: the condition 'if (e = START)' assigns instead of compares, causing always true
BCompilation error due to missing semicolon
CRuntime error due to null pointer dereference
DNo error, outputs 'Running' then 'Idle'
Attempts:
2 left
💡 Hint

Look carefully at the if condition inside the IDLE case.

📝 Syntax
advanced
1:30remaining
Syntax error in event-driven state machine code

Which option contains the correct syntax to define an event-driven state machine handler function in embedded C?

A
void handle_event(Event e) {
    switch current_state {
        case IDLE:
            if (e == START) {
                current_state = RUNNING;
            }
            break;
    }
}
B
void handle_event(Event e) {
    switch(current_state) {
        case IDLE:
            if (e == START) {
                current_state = RUNNING;
            }
            break;
    }
}
C
void handle_event(Event e) {
    switch(current_state) 
        case IDLE:
            if (e == START) {
                current_state = RUNNING;
            }
            break;
    }
D
void handle_event(Event e) {
    switch(current_state) {
        case IDLE
            if (e == START) {
                current_state = RUNNING;
            }
            break;
    }
}
Attempts:
2 left
💡 Hint

Remember the syntax for switch statements in C.

🚀 Application
expert
2:30remaining
Number of states after processing events in a complex state machine

Consider a state machine with states: INIT, READY, PROCESSING, ERROR, DONE. It starts in INIT. The events are: START, FAIL, COMPLETE, RESET. The transitions are:

  • INIT + START -> READY
  • READY + START -> PROCESSING
  • PROCESSING + COMPLETE -> DONE
  • PROCESSING + FAIL -> ERROR
  • ERROR + RESET -> INIT
  • DONE + RESET -> INIT

If the event sequence is START, START, FAIL, RESET, START, COMPLETE, how many times does the state machine enter the READY state?

A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Trace the states step by step for each event.