Garage door controller
Write the transition function of a garage door, including the obstacle safety reversal. An easy embedded C problem (State machines), run and graded in the…
easy · State machines · fsm · safety
A garage door has one button, a limit switch at the top (EV_TOP), one at the bottom (EV_BOTTOM), and a light-beam obstacle sensor.
door_next(state, event) returns the next state:
| State | Event | Next |
|---|---|---|
| CLOSED | button | OPENING |
| OPENING | top limit | OPEN |
| OPENING | button | CLOSING (reverse) |
| OPEN | button | CLOSING |
| CLOSING | bottom limit | CLOSED |
| CLOSING | button | OPENING (reverse) |
| CLOSING | obstacle | OPENING (safety) |
Every other event leaves the state unchanged.
Starter code
door_t door_next(door_t state, door_event_t event) {
return state;
}Example tests
Open and close
run("BTBD");Expected output:
OPENING OPEN CLOSING CLOSEDAn obstacle while closing
run("BTBO");Expected output:
OPENING OPEN CLOSING OPENINGLearn it first
- State machine patterns in C · Switch, transition tables and state-function pointers compared.
More State machines problems
- Traffic light with a fault mode · Write the transition function for a light that cycles, flashes on a fault and resets.
- Table-driven state machine · Look up a transition in a table, run its action and return the next state.
- Keypad code lock · Implement a door lock that takes a 4-digit code, re-locks, and locks out after three failures.
- Click, double-click and long press · Turn a sampled button into click, double-click and long-press events with a state machine.
- Fix: the door opens by itself · A door controller skips its unlocking state.
- Washing machine: the cycle and the door · Run a wash cycle as a state machine that pauses safely when the door opens.