State Machines (Nina Kowalke)

This week our activities focused on state machines; teaching us how to create a state diagram and then code the state machine.  After watching video tutorials about state machines we did an activity which involved looking at a state diagram that we were given, interpreting what it was trying to do, and then assembling the hardware and writing the software that would carry out the correct behavior.  The final product was made up of one LED and 2 buttons and it was carried out by 4 states.  The project allowed the user to push buttonA to turn on the led, then push buttonA again to turn the LED off.  The time for which the LED was on for that sequence was recorded using a timer function in Arduino and that time was then applied as the blink time when buttonB was pushed.  So, if someone pushed buttonA (turning the LED on) and then after 2 seconds pushed buttonA again (turning the LED off), if they were to push buttonB the LED would now blink in intervals of 2 seconds until the user proceeded to push buttonB again, turning the LED off.  Below is a picture of the final result.



Through this activity I learned not only what a state machine is, but also how to create a state diagram and then create it’s supporting code.  I worked alone on this project, and the major takeaways I had from the activity, were learning how to use enumerated states within Arduino code and also gaining familiarity with the timing functions.  I had a difficult time with the timers, but this activity definitely helped me to gain better understanding of how to implement them, which I have already strengthened even more since this activity last week.  I am excited about how much state machines make possible.  I feel like a new door has been opened with this function, and I am starting to think more naturally in the language of C++.  My most surprising outcome of this activity was the overall concept that this was something I was able to create (with help of course).  It amazes me how steep my learning curve has been in this class, and this project just added to that.


The four states that made up this state machine were BLINK_ON, BLINK_OFF, SOLID_ON, and SOLID_OFF.  The code for this project began with the inclusion of the bounce library and then contains the “const int” variable declarations for the led, both buttons, and the debounce features.  Next, the code uses the new function I learned in this activity which is “enum State {” in which the four states are listed.  Next, “currentState” is declared and the timer variables are declared as “long” types.  The code then proceeds into the “setup” portion in which I declared whether each pin would be an output or an input as well as giving some timer variables starting values.  Lastly, the code moves into the “loop” portion which I learned in this activity gets split up into three sections that can be referred to as “display state”,  “update/read inputs”, and “update state based on inputs” in state machines.  The “display state” portion simply lays out what functions are to be carried out within each state using “if” and “else if” statements.  Next, within the “update/read inputs” portion, the debounce functions are updated, and the Arduino reads whether or not either button is pushed and also sets the current time.  Lastly within the loop, “if” and “else if” statements are used to explain what is to happen with each possible action at each possible state.  This last portion is really where the state diagram is especially helpful in laying out how the program is to behave.  Pictured below is this last section of code.screen-shot-2017-02-06-at-9-45-43-pm


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s