Software

Software

We left off last week in the industrial revolution where your circuit was a machine built to do one thing.  The  Arduino is an expensive power supply so far.  We want the promised universal machine that can change programs.  Software is what is going to give this circuit the malleability to change programs or to evolve based on input. To prove this we will have the the microcontroller flash an LED when you throw your switch.  Blinking is not much of a change in the program but it will make the point and after you accomplish it you will feel an irresistible need to do the hokey pokey.

OUTPUT
outputMAAB copy

INPUT

inputMAAB copy

Only 3 Things (Variables, Repeat Loops, If Statements)

saw hammer pliers

You can combine these three tools into a million projects. Likewise we will only show you three programming tools: variables; if statements; and repeat loops.   There are an infinite number of recipes or plans for sequencing these tools to make almost any computer program. These recipes are called algorithms.  This week we are going to hit on two of these, repeat loops and if statements.  Until we get to variables next week it may feel a little like a two legged stool but you will survive.

Oh Sorry actually 3 more (Arrays, Objects and Functions)

Laundry Room Area cleanup 021

Actually there are three more things arrays (shelves), objects (containers) and functions (spray bottles).  These storage and organizational tools are less critical than repeat loops, variables and if statement .  You will need them if your project gets complicated or scales up (as software will do) but we won’t need them yet with the microcontroller (Arduino) because the programs are so small and simple.

Which Programming Language

People really waste a lot of time arguing about which programming language is best.  While some languages are better at certain tasks than others most people just argue in favor of the language that they know.  You are lucky not to have to get involved in these distracting discussions because I am telling you which language to use which is mostly Java.  The reality of  programming is going repeatedly through the four steps below.  Of them only “Syntax”  is very language specific.  Most of the skills you learn in any language are reusable in any other.

  • Algorithms — Figuring out a process that solves your problem
  • Pseudo-code  — Translating that process into repeat loops, variables and if statements.
  • Syntax  — Changing it into the particular programming language you are using.  This is the only thing that is not reusable.
  • Debugging  — This is 90% of your time.  Imagining ways that you are wrong is difficult for humans.

It is worth knowing a little more about other programming languages and we will go into this more when we get to programming on your deskt0p machine in Processing.

Different from Computer Engineering Class

This class is different from a Computer Engineering class because we do not feel obliged to show you all the ways to combine those tools, to do the standard things.  In a computer science class you would start with all the classic algorithms whether you need them or not.   Instead we will show you the 3 basic tools (really 6) and let you learn just the algorithms for your project.  We think you will learn better when you see directly why you need it.   Plus we don’t want you doing the standard things.  With this approach you will go deeper in your interests but come out with less comprehensive knowledge.

The Hard Parts

Pedagogy of this style of project based learning is a little chicken and egg.  To some degree you have to know what is possible before you can think of what you want to do.  We give you some basic project ideas at the beginning like making an LED blink or making this sphere bounce off the edge of the screen.  Making something that you care about (at least a little) that also uses these computational techniques is the challenging at the beginning.

People are think the name computational media has to do with math.  You will see a bunch of numbers in the code but it is more like arithmetic than math.  Being able  to abstract the world into repeating forms and systems will help.  Mathematician do that but you also do it when you cook dinner.     Expression needs skin and bones, empathizers and systematizers.  The work in this class is in just a little more on the side seeing the bone structure than the skin but you will need both eventually.

Microcontroller: Connecting Atoms and Bits All at a Great Price

These are computers that specialize in making broad contact to and from the physical world.  They will help you get beyond the notion that all the world’s expression has to be able to pass through a keyboard mouse and monitor to engage the power of computing.   The pins of the Arduino can read energy,  or more commonly bounce energy off of almost anything instead of just your fingers on the keyboard.  The pins can also send out signals to actuate light, heat and movement to change the environment beyond the a flat display.

But wait there is more.  They are tiny and they cheap.  The Arduino module is for prototyping but if your invention went to manufacture this circuit would be the size of your pinky nail and cost one or two dollars.   That extends the context of where you can embed computing way beyond a desktop.

uno

The Arduino

We are using the Arduino  brand of microcontroller that has come to almost become synonymous with DIY microcontrollers and the entire field of physical computing .  It’s great popularity is probably due to the fact that is was produced in a novel “open source hardware” way, capitalizing, as open source software did, on a community of users who feel some sense of ownership.  You might have heard of other brand like “Basic Stamp”  which was popular before Arduino.  With the growth of maker movement (thanks to Arduino) many competitors are popping up.  In particular you will hear about “Rasberry Pi” and “Beagle Bone.”  The are related to microcontrollers in that they are small cheap computers but their powers are more comperable to your laptop.  They have actual operating systems that are better at connecting to standard computer interface like the monitors, keyboards and the internet than they are at reaching out in the physical world.

Arduino Environment

An “environment” is basically the text editor for writing and then running your code.   Unlike programming languages it might be worth arguing about which one is better.  Once again you are saved from this argument because I am picking Arduino environment for you.

 

Arduino1Blink

You have to do these things.  Here is the guide or doing them.

  1. Download Arduino programming application.
  2. Connect it to your computer.
  3. Tell the application which Arduino model you are using.
  4. Tell the application which serial port you are using.

After you have it set up you have to do these two things:

  1. Write your program.
  2. Press play to send the program to your Arduino board and run it.

The simplicity of this environment is a beautiful thing for beginners.

What Happens When You Press Play

Usually when you write software on laptop it runs on your laptop.  our laptop it may feel like to program runs on your laptop.  In fact when you press play the program goes down the USB cable, gets installed in your Arduino and runs there.  It is then independent of your Arduino and will continue to run when you disconnect your USB cable.  Sadly you can’t suck the program back up the USB cable.  You will have this same experience when you do server side programming for the internet.  You will edit on your laptop but it may run in the cloud.

Calling Functions

These are magical incantations from Arduino  that you are going to have to learn this week.

These are called function calls and they get the Arduino to do something.  Function calls take the form of the name of the function followed by some parenthesis filled with input instruction,  known as parameters, for how to tweak the function.  Some functions just do something.  For instance “digitalWrite” puts 5V out to a pin when you  use the “HIGH” parameter and zero volts when you use the “LOW” parameter.  Other functions not only do something but they return something to you, for instance “digitalRead” gives you the state of a pin (5 volts or zero volts).  Here is the complete reference for all the Arduino incantations.

Code Punctuation

Curly Brackets and Semi Colons

Like your prose your code will have punctuation to make it easier for the machine to read.  Instead of periods at the end of sentences you need to put semicolons at the end of each line.  Instead of paragraphs you put curly brackets at the beginning and end of each group of lines.  Humans will probably get the idea if you miss the odd bit of  punctuation in your prose but computers will grind to a halt if you miss a single thing.  Sorry.

punctuation

White Space

Conversely computers are much less fussy about “white space” between line and words.  A human       might be

put

off by this      .But not a computer.  The way you see the lines of a code block indented is not important to the machine.  It will help you keep your code straight especially as you put code blocks within code blocks( as we are going to do in a minute by putting an if statement code block inside a function code block.)  There is an auto format feature under the “tools” menu in the Arduino Environment that does the indenting for you.

Comments

One last thing about punctuation.  Two slashes like this // makes anything following them ignored by the computer.  This is useful for leaving human language clues as to your thinking in making the program right in the code.  It is also a great debugging tool.  When your program doesn’t work you disable lines until it does work and then they you know which one was the problem.  You can comment out many lines of code with by surrounding  them  like this /* code; code; */

Anatomy of Functions (Code Block that Can Be Called By Name)

You can make one of your code blocks into a callable function by giving it a label an output and an input.   In this case we are making a function that gets called with the word setup.   In this case we have declined to take advantage of the input or output.  The empty parenthesis ( ) after the word setup mean that you are not taking any extra instruction (parameters) before you perform the code inside this function.  The word void means that this function does not return anything after it runs.  For now put the void before the name and the  ( ) after the name by rote and they will make more sense later.

functionAnatomy

When you make a function you add to the vocabulary of your program.  Now you can make up your own names in addition to the store bought functions like digitalRead and pinMode (someone somewhere wrote something with curly brackets and voids and parenthesis for those but they are in the background and you don’t see them).

You have now taken the radical step into nonlinearity.  Unlike paragraphs in your prose which pretty much stuck with their place on the page,  your functions are callable by name in any sequence.  Later on when you avail yourself more of the opportunity for input into these functions they won’t even be stuck with exactly how you wrote them.

Famous Functions: “setup” and “loop”

In truth you will not get much into writing your own functions for a while.  Maybe not until we get to Processing.  Instead you will probably just write the two functions that Arduino insists you write, setup and loop.  Arduino calls out these functions names for you and expects you to have some function ready to receive those calls.   “setup” is called once at the very beginning and “loop” is called over and over again until your program dies.

void setup(){
put in all the things that should be set at the birth of your program here
}

void loop(){
put all the things that should happen over and over in your program here
}

Remember that there are three things you are going to learn about programming?  You just learned one of them.  The repeat loop.  Because Arduino promises to call this loop function over and over again you have your first repeat loop.  It is like the lungs or heart of your program.

Divide and Conquer

Our goal here is to have the microcontroller flash and LED (OUTPUT) when you close your switch into microcontroller (INPUT). Whenever possible you should test and isolate separable tasks and combine them after you understand them.  So we suggest that you first work on the output, getting the  microcontroller to blink the LED.  When you figure that out we will combine it with the INPUT part.

Output Circuit

You already know most of the circuit for output to an LED.  Basically instead getting the 5 volts of power directly from the power supply of the microcontroller as we did last week , you will get it from one of the i/o pins that can be controlled by software.  Take note of which pin you plugged the LED into because you will have to use in the digitalWrite function later in software.  (I just noticed that in this picture plugged it into pin 7 but in example code I used pin 13.  I guess that will be a small challenge for you to switch the code or the circuit.

digout

dout

Output Software

Type this into your Arduino environment and press play.  After the program downloads to the Arduino you should see the LED flashing.  This is the point you many uncontrollably gyrate.

Input Circuit

The input circuit is a a little harder to understand.  It would make more sense if you got rid of the part that connects to ground via a 10K (brown, black, orange) resistor.  Then you would have a switch which either sends 5 volts into the microcontroller pin or not.  The problem is that when you put the microcontroller pin in input mode it is just kind of waiving around in the breeze and random electricity in the air may send it up or down.  So we tie it down to ground for when the switch is open. When the switch is closed,  electricity will take the path of least resistance and ignore that route to ground with the resistor in the way.

digin

IMG_20140130_172318

 

If Statement

Remember how we only needed to learn three things and we already did a repeat loop with the loop function. Well now the second thing is the “if statement” and it is pretty easy.  Basically it is a code block with the curly brackets that either happens or not depending on what is in the parenthesis after the word if.

if (some condition ){
stuff to do
}

This might be what is looks like in practice.  Notice the double equal sign in the condition.  This is the main thing that people screw up.  Computers need you to use the double equal when you are comparing two things.  (The single is equal is for setting something equal to something which uses the same word but is a different operation).

 

There are million forms of the if statement the most common of which uses the word else.  As you start to combine if statement the logic can get a little tangles.  But in general they same language you use in human language to describe the logic translates pretty well into code.  So if you can say it right you can probably code it right.

With the if statement you really starts starts to think that this microcontroller might finally earn its keep.  If you can off load some of the tedious rules in your mind for it to enforce life would be better.  For instance if it is summer and it is afternoon close blind else if it is winder open the blinds.  Before you get too excited enough if statements don’t easily add up to artificial intelligence but they certainly can extend your reach.

&&  ||  (AND OR)

You can probably listen to your human language description of our project to know when to employ and if statement.  As mentions earlier you have to be care to substitute “==” when you say “=”  You also have to substitute && for AND, || for OR.

Or you could "nest" one if statement inside another

 

Input Software

 

Type this into your Arduino environment and press play.  After the program downloads to the Arduino you should see the LED flashing only when the switch is pressed.  Hopefully you are too distracted by all the possibilities of machines enforcing your decisions to even remember to do a hokey pokey. 

Assignment

Make your project do more than one thing.  Maybe if your switch is pressed it blinks one way and if not another way.  Or maybe different LED’s come for different switches or maybe you have to hit a combination of switches before the light comes on.

 

Leave a Reply