State Machines can be represented like this:
To the right is an example of a state machine for a bomb. To the bottom is the exact same state machine, but represented in C code. C code is great for representing state machines because of the enumeration function. We can define a new type with all of the states and another for all the events.
typedef enum { NO_EVENT, PUSHED_ARM, PUSHED_DISARM} event_ttypedef enum { INITIAL_STATE, DISARMED, COUNTING_DOWN, WE_GO_BOOM} state_tvoid RunStateMachine (event_t currentEvent){ static int count = 0 static state_t currentState = INITIAL_STATE; switch (currentState){ case INITIAL_STATE:{ currentState = DISARMED; break; case DISARMED:{ switch (currentEvent){ case PUSHED_ARM: TIMERS_InitTimer(BOMB_TIMER, BOMB_INTERVAL); count = 12 currentState = COUNTING_DOWN; break; } case COUNTING_DOWN:{ switch (currentEvent){ case TIMEOUT: count--; //update LEDs TIMERS_InitTimer(BOMB_TIMER, BOMB_INTERVAL); if (count == 0){ currentState = WE_GO_BOOM; break; case PUSHED_DISARM: currentState = DISARMED; TIMER_Stop_Timer(BOMB_TIMER) } case WE_GO_BOOM: printf("Boom!");