element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Hats Off Design Challenge
  • Challenges & Projects
  • Design Challenges
  • Hats Off Design Challenge
  • More
  • Cancel
Hats Off Design Challenge
Blog Hats Off Fascinator 5: Metaphorical Programming
  • Blog
  • Forum
  • Documents
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: brenyc13
  • Date Created: 30 Aug 2014 12:14 PM Date Created
  • Views 1019 views
  • Likes 0 likes
  • Comments 4 comments
  • jaunty_fascinator
  • for_beginners
  • programming
  • hats_off
  • challenge
  • adafruit
  • arduino
Related
Recommended

Hats Off Fascinator 5: Metaphorical Programming

brenyc13
brenyc13
30 Aug 2014

A short video and overly long post about my silly analogies for programming


You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

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.


image

 

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…



image


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:

"{

  1. strip.begin();
  2. strip.show(); // Initialize all pixels to 'off'
  3. }"


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:"{

  1. // all pixels show the same color:
  2. colorWipe(strip.Color(50, 0, 50), 50); // Purple
  3. }
  4. // Fill the dots one after the other with a color
  5. void colorWipe(uint32_t c, uint8_t wait) {
  6. for(uint16_t i=0; i<strip.numPixels(); i++) {
  7. strip.setPixelColor(i, c);
  8. strip.show();
  9. delay(wait);
  10. }
  11. }"


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.



image


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.


  • Sign in to reply
  • fearless
    fearless over 11 years ago

    Barbara don't ever regret your post. You're being honest and I guarantee there are a lot of others out there that struggle with programming. They will find comfort in your confusion. I loved your comment in the video that "you're not a programmer YET". You have an open mind and a great attitude. If you keep at it, one day it will make sense, I promise. The super cool thing about programming languages is that they are very similar. Once you learn one, the next will be so much easier.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 11 years ago in reply to brenyc13

    Perhaps, but you explained  the idea very well and described it simply.

     

    There are a lot of "professional" software developers who say they understand OOP, but could not describe it as simply as you did.

     

    I like working with new people because they do not know what they cannot do yet.  I used the technique for all of the companies I worked at and had very good results awakening people from just knowing engineering to finally understanding engineering.

     

    I am always looking for people who I might help along the way.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • brenyc13
    brenyc13 over 11 years ago in reply to DAB

    Thank you DAB! I really appreciate your encouragement, though I suspect you're giving me too much credit.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 11 years ago

    Very good post.

     

    In just a few minutes of discussion you got right to the heart of Object Oriented Programming (OOP).

    The goal of OOP was to keep the domain discussions in terminology natural for the user.

     

    You quickly established your thinking domain and translated it into the application of "programming" steps that enabled you to easily see how to put the pieces together.

     

    Well Done.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube