State Machines & Timing (Will Knowlton)

In an effort to make more complex sketches and programs, this week we were taught the usefulness and structure of State Machines. State Machines become imperative in coding structure for what we are making because they allow for more rigorous computing and processing to occur. In previous sketches that didn’t utilize State Machines, we were sort of fooling the computer into what we were doing by using the delay() function in the Arduino program. State Machines help the creator of the desired program lay out all possible options, and see which inputs lead to which outputs. In the sketches covered this week, we were taught to organize State Machines in three separate sections for efficient coding. After you’ve included libraries, variables, inputs, outputs, and declared the different states,  (enum State), you move onto the loop. Inside the loop, you (1st) write out all of the different display states,  (2nd) update or read the inputs received (like a button), and (3rd) update the states based on the inputs received.

This week our breadboard remained unmodified from the 2 button input, 5 L.E.D. output. The task was to use a State Machine to organize code in getting the L.E.D.s to blink and alternate blinking by the pushing, or pressing of a button.

The timing of the blinking L.E.D.s gave Graham and I the biggest headache of them all. It seemed like that would be a simple thing to add. But, setting up timers and learning how the computer keeps track of the time elapsed, resets the time, and otherwise modifies the time relating to the blink function was a struggle! One culprit of our repeated faults was actually the tip of the day provided by Janosch and Spencer, “==” doesn’t mean the same thing as “=”. Thank you guys for saving us. Once Graham and I had the two alternating L.E.D.s working in the program, we then moved onto the separate uniformly flashing 3 L.E.D.s.

We first tried setting up a separate state machine, by incorporating one inside the original. We added 2 more states, 2 display states, and then created a new IF statement outside of the larger IF statement used for the two alternating L.E.D.s. Our new IF statement began to get suspiciously complex so we consulted Dexter in the lab. He showed Graham and I a great way to organize the separate flashing L.E.D.s. He showed us that you can shorten the IF statement for our update-state section by moving the button press input to a separate loop.  We made a global variable called pause set at 500 milliseconds, and the new B button loop took that pause variable, and modified it to 200 milliseconds. It did this by simply changing the debouncer to ‘read’ instead of ‘rose’. The only function the new update state was to check for current state and to turn on the timer.

Below: The final outcome of the program.


Will Knowlton

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s