A short video and overly long post about my silly analogies for programming
But if you'd rather skip this week's sesh in the confessional...
Goldilocks and the Infinite Tutorials
Once I was actually able to use the Arduino IDE, I ventured out into the internets to look for a good-but-simple tutorial to get me started. There are a gazillion Arduino tutorials for people of all coding levels and interests. The hitch is that the vast majority of these are made for regular Arduino boards so, while they could teach me how to write programs for Uno or Leonardo, they were just far enough from the GEMMA to be annoying. The two that I liked best were this one on C++ programming and this one on Arduino For Beginners.
Oh The Hu-manatees!
I’m a total humanities person--as in a philosophy/theater double major who went to grad school for design writing--so I really like stories and things and stories told by things. Seriously, I think that we use our objects and environments as external brains and so starting out, this abstract and semi-arbitrary coding stuff did not gel. Until I found my first metaphor.
Metaphor 1: Sketch as Script
My first tactic was to think of the sketch as a comedy sketch or theater script. I used to work in the theater and it's a world I understand. So this line of thinking turned on a little light in my head. Scripts have weird conventions. They have a place at the beginning where they describe the characters, they have stage directions, and lines. But at base, a script is a set of instructions in the way that code is a set of instructions.
Admittedly, this is not the best analogy. But it was something that my novice brain could understand. And it was a way for me to get past the scary feeling of being totally incompetent. I mean, I was pretty good at understanding theater and if writing code is in any way like writing a play script, I know that I have the ability to eventually become pretty good at coding, too, right?
Baby Steps, People
So within each sketch, there are smaller chunks of code known as functions. These are the processes that run when you run a sketch. Following my sketch-as-script idea, I began to think of functions as the characters in a script. There are two kinds of functions: value-returning functions and void functions. I considered these as speaking and non-speaking roles, respectively. Value-returning functions perform an action and then tell you information, while void functions perform actions in silence.
Every Arduino sketch requires two non-speaking roles (aka functions) known as "void setup()" and "void loop ()."
So the most basic sketch program looks like this:
void setup()
{
}
void loop()
{
}
"Void setup()" sets the scene and raises the curtain. He appears just once at the beginning of every sketch. "Void loop()", on the other hand, is the central actor of every sketch. She moves all of the other actors into place, performing the same operations over and over and over in a continual loop.
Okay, so really, by the time I get to "functions are like characters" this analogy breaks down. I needed to adjust my paradigm to accommodate how functions actually function in a sketch.
A Dissent
At this point, I was explaining my analogy idea to my spouse and he was really not into this whole metaphor thing. Hunky Assistant pretty much thought it was a total waste of time. But then he was like, "You know, it is kind of like a recipe book? With a two-course menu and then procedures for making each item on the menu in a separate chapter that it names…." And thusly, my second metaphor was born…
Metaphor 2: Sketch As Recipe
Also an instruction set. But without the added theatrical weirdness of characters and actors and whatnot. So, let’s go ahead and think of each sketch is a two-course menu. First course: void setup. Second course: void loop. But at the very very beginning, there's a list of some key ingredients, libraries, and a few useful definitions.
Reading the Space Face Sketch
If there's a "#" in front of a line that means it's part of the pre-processor; it's prep before the computer starts making the recipe. Sometimes it brings in a library, as in "#include <Adafruit_NeoPixel.h>" (roughly meaning: go get the library named Adafruit_NeoPixel) or maybe it's a definition like "#define PIN 1" (roughly meaning: any time you read "PIN" in the recipe, just replace that with "1"), or it makes a new recipe page to reference later like with "Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);" (roughly meaning: make a new sub-recipe that we're going to come back to. The sub-recipe is named "strip" and it will make "Adafruit_NeoPixel," and it takes 3 ingredients. These ingredients are the number of pixels in the strip, the pin number, and the flags for each pixel type, and they are 5, PIN, NEO_GRB + NEO_KHZ800, respectively).
Then we get into the first course: "void setup()." This course is:
"{
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- }"
Which I interpret to mean: go to that sub-recipe we just made that's called "strip" and perform the procedure called "begin" (this is probably a pre-defined function in a library somewhere that sets up the NeoPixel?). Once that's done, go back to that recipe we just made that's called "strip" and perform the procedure called "show" (another pre-made function that Adafruit tells us turns all the pixels to "off").
Then we get to the second course: "void loop()." Which is:"{
- // all pixels show the same color:
- colorWipe(strip.Color(50, 0, 50), 50); // Purple
- }
- // Fill the dots one after the other with a color
- void colorWipe(uint32_t c, uint8_t wait) {
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- strip.show();
- delay(wait);
- }
- }"
So this I take to mean something like: Go to the sub-recipe called "colorWipe." ColorWipe needs two ingredients: strip.Color and and wait time. But strip.color needs three ingredients which are the the red hex value, the green hex value, and the blue hex value, respectively, and they’re 50,0,and 50.
That lower bit just after “Fill the dots one after the other with color” is the colorWipe recipe itself. This is where the sketch lays out how the ingredients given above come together. This is where we see that whatever comes out of “strip.Color(50,0,50)” is going to be an unsined 32 bit integer, also known as “c”. And that the final “50” in line 2 refers to an unsined 8 bit integer called “wait”.
Then We Get to the “for” section. This one really threw me for a loop (har har). Enter another helpful explanatory tutorial! So the “for loop” is a really useful, standard thing to use in C. Its structure is always the same:
for ( where we start from; keep doing the loop when this is true; modify something each time the loop runs ) {
Code to execute while that middle bit is true
}
So this part of the Space Face code roughly means: we have an unsined 16 bit integer named “i” that’s equal to zero when the loop starts; When “i” is less than the number of pixels in the strip, the loop continues; And add another to “i” each time we do a loop.
When we’re in a loop, we go down to strip.setPixelColor to display the “i” and “c” variables. Then on to strip.show to turn them on and then to delay(wait) to pause a bit.
Is that what it means?
Okay, so maybe that was super confusing to you. Maybe it's wrong. I'm still a baby coder so please feel free to correct my amateurish interpretations and analogies in the comments. This is a learning experience, after all. I'm going to keep reading tutorials and am taking a class on Intro to Arduino for Noncoders today. But if you have any pointers or tips, I'd love to hear 'em.