Main State Machine
The main state machine, referred to as the mainFSM, handles all user inputs (bell rings, button presses, bowl placements) into the "Cereal Killer". It sets controls for the LCD panel, a time lapse servo, a cereal servo, LEDs, and an active buzzer. This mainFSM receives events from three functions CheckMicInput, CheckButtons, CheckADEvent. The structure of mainFSM is illustrated in the following FSM figure. Additional currentstate-independent actions in response to events are shown on the bottom of the FSM figure. The "RandomState" is used to represent any of the 10 states in the mainFSM.
Button Debouncing State Machines
RightFSM, LatchFSM and LeftFSM are three state machine services conducting software debouncing and their implementations are highly similar, only differing in the index used when calling InitButtonStatus, the timer used, and the event posted when ES_TIMEOUT is received. All three FSMs take events posted from CheckButtons function in EventCheckers.c where the latch, right, and left button pins are routinely read and compared with previous values. In case of a change, an ES_ButtonDown or ES_ButtonUp is posted to the correct one of the three button FSMs where events such as ES_RightSelected, ES_LeftSelected are created and posted to the mainFSM.
Servo Codes
There are two different servos used in the machine. The smaller MG90D uses Timer2 and takes PWM controls from RPB15. It initializes two timers, one with time corresponding to potentiometer value that the user sets and the other with time of 1.5 seconds to control when the cereal servo should rotate the next compartment over to the front exit. The bigger HX5010 uses Timer3 and takes PWM controls from RPB11. The timer expires every one second after which the timer servo is instructed to go 1/60 of its maximum range and the timer is reinitialized so that passage of time can be displayed.
Event Checkers for Main PIC
The Event Checkers.c handles four types of sensor inputs. It routinely checks the value of pins connected to left, right, and latch buttons and post ES_ButtonDown to respective FSM in time of pin value changes. It handles microphone inputs and increments the MicCounter by 1 every time the value of the device has changed from 0->1 or 1->0. When the MicCounter has reached 1200, it posts ES_MIC_LOUD event to the Main FSM which transitions into WelcomeState and prompts for cereal type selections. It also configures analog auto scanning of two channels to take in inputs from Opto-reflective sensor and the rotary 10k potentiometer (used as dial). Software hysteresis is incorporated into handling optosensor readout so that when a bowl is removed, a lower distance threshold is needed for the system to recognize bowl placement, to reduce noisy event posting.
Driving LCD Panel QC1602A on Second PIC
The primary PIC has three pins connected to the second PIC and depending on which combination of pins are pulled high, the second PIC's event checker detects such changes and posts ES_NEW_DISPLAY event to LCDService with the correct EventParam. The LCDService contains private functions LCDcommand, LCDWriteCMD, LCDWriteChar, and LCDWriteString that are used to interface and send data to LCD panel through pulsing the eight data bit lines. The strings that are written to the LCD Panel are pre-stored and can vary among eight strings depending on the parameter of the event ES_NEW_DISPLAY.
Other Files