0
0
Embedded Cprogramming~10 mins

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the initial state of the state machine.

Embedded C
enum State { INIT, RUNNING, ERROR };
State currentState = [1];
Drag options to blanks, or click blank then click option'
ARUNNING
BINIT
CERROR
DSTOPPED
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a state that is not defined like STOPPED.
Starting directly in ERROR or RUNNING state.
2fill in blank
medium

Complete the code to transition from INIT to RUNNING state.

Embedded C
if (currentState == INIT) {
    currentState = [1];
}
Drag options to blanks, or click blank then click option'
ARUNNING
BERROR
CINIT
DSTOPPED
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the state back to INIT causing a loop.
Using an undefined state like STOPPED.
3fill in blank
hard

Fix the error in the switch statement to handle the ERROR state.

Embedded C
switch (currentState) {
    case INIT:
        // initialization code
        break;
    case RUNNING:
        // running code
        break;
    case [1]:
        // error handling code
        break;
    default:
        break;
}
Drag options to blanks, or click blank then click option'
ASTOPPED
BINIT
CRUNNING
DERROR
Attempts:
3 left
💡 Hint
Common Mistakes
Using a state that does not exist like STOPPED.
Repeating INIT or RUNNING in the error case.
4fill in blank
hard

Fill both blanks to check if the state machine is in RUNNING state and then set it to ERROR.

Embedded C
if (currentState == [1]) {
    currentState = [2];
}
Drag options to blanks, or click blank then click option'
ARUNNING
BINIT
CERROR
DSTOPPED
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the states in condition and assignment.
Using undefined states like STOPPED.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps states to their string names.

Embedded C
const char* stateNames[] = { [1], [2], [3] };
Drag options to blanks, or click blank then click option'
A"INIT"
B"RUNNING"
C"ERROR"
D"STOPPED"
Attempts:
3 left
💡 Hint
Common Mistakes
Including undefined states like STOPPED.
Mixing the order of state names.