At the end of last week, I spent some time with my CS30 students getting them used to the idea of a ‘state variable’, in which we keep track of the state of a program with a simple variable, and transition to appropriate states when certain conditions are met. To introduce this to them, I like to show them the following animation, which is generated by an example from Daniel Shiffman’s Learning Processing book:
I then have them work in partners (pair programming style) to try to come up with a way to replicate the functionality they see. Generally speaking, most of the pairs come up with a means of doing this, though the solutions vary wildly in elegance. After they have all come up with something, I show them a solution using a state variable, similar to the code that Daniel Shiffman uses in his example.
Then over the weekend, I read Alfred Thompson’s Traffic Light Project in Several Acts post, which is a riff on Dawn DuPriest’s Event-Based Programming post. This seemed like a lovely idea, so I used it today with my students to reinforce the idea of state variables.
A few minutes before the first class started, I quickly created some starter code for the students, which looked like this:
This code generates the following image:
##Timer in Processing
Since we hadn’t worked with the millis() function in Processing yet, if student’s struggled, it was on figuring out how to keep track of time. After watching them flounder for awhile, I helped out a bit by working through an example of changing the background color of a sketch to a random color every 2 seconds. This seemed to provide enough scaffolding that most students could then complete the task.
Once we get to OOP later in the semester, we’ll be creating a nice Timer class, but for now, the timer logic is just included in the rest of the code.
After the students had finished creating their solutions, I had them discuss their method of solution with a neighbour. Following that, I demonstrated a reasonable solution, with a focus on creating simple functions to make easy to understand code.
The students seemed to enjoy the exercise. I’ll be doing it again next semester.