Module level variable: LastButtonState, LastPinState FUNCTION InitButtonDebounce Initialize the port line to monitor the button Sample the button port pin and use it to initialize LastPinState Set CurrentState to be DEBOUNCING Start debounce timer (timer posts to ButtonDebounceSM) post the initial transition event END InitButtonDebounce FUNCTION RunButtonDebounce(ES_Event_t ThisEvent) Local Variables: ReturnEvent, NextButtonState Set ReturnEvent to ES_NO_EVENT If our current state is debouncing If EventType is ES_TIMEOUT Set NextButtonState to Ready2Sample; End If End If If our current state is Ready2Sample If our event is START_BUTTON_UP Start debounce timer Set the next state to Debouncing; End If If our event is START_BUTTON_UP Start debounce timer Set next state Post Debounced start button event up to master SM_ End If End If Update LastButtonState Return ReturnEvent END RunButtonDebounce FUNCTION CheckStartButton Local Variables: CurrentPinState, ReturnVal Set ReturnVal to false Set CurrentPinState to state read from port pin If the CurrentPinState is different from the LastButtonState If the CurrentPinState is LOW (Button down) PostEvent ButtonDown to ButtonDebounce queue Otherwise PostEvent ButtonUp to ButtonDebounce queue End If Set ReturnVal to true End If Set LastPinState to CurrentPinState Return ReturnVal END CheckStartButton