Self-Guided Activity Part 2: From Speech to Code

Self-Guided Activity Part 2: From Speech to Code

Max Vanatta
1 / 1

Welcome back for the second part of our introduction to Arduino!

In this section, we will discuss the process of coding, and while there are faster ways to get the code ready for our goal of blinking the LED, this process will be useful in your long career of more complex code projects.

Talk it out

When we start to code our project it is useful to first get our ideas down on paper.  Try to begin by saying what someone else would need to know to perform any tasks that are to come.  An example is: There is an LED connected to the board which we will be turning on and off.

Then go through and describe what you would like to happen.  This is the general behavior of your device such as: Turn the LED on for a second and then off for a second.

Pseudo-code it

Now that we have our talked out version:

There is an LED connected to the board which we will be turning on and off.

Turn the LED on for a second and then off for a second.

We are going to do our first layer of translation called pseudo-coding.  This is a term which refers to a simplified and clearer directions which are closer to coded language.  Here is where you will first begin to think like a computer.  So let's go through a few questions which can help us to define our pseudocode.

  1. What needs to be set up?  For each device:
    1. Where is this device? (Pin Location)
    2. What type of interaction is this? (Output or Input)
    3. What type of pin is being used and how? (Analog, Digital, Digital PWM)

      LED at Pin 13, Output
  2. What happens when you run the code? Some things to consider:
    1. The Arduino only performs one task at a time, so order matters a lot.
    2. The Arduino will loop through your code as fast as it processes the information meaning if you need to have something happen for a certain time, tell it.
    3. Are there decisions which need to be made during the loop? This could be important when you include sensors into your devices.

      LED on
      Wait a second
      LED off
      Wait a second
      Repeat

Putting it in Arduino

Now that we have our pseudocode written out, we will open up the Arduino interface and take our first step into the digital world.

When you first open Arduino, you will see and nearly empty sketch.  This is the name given to Arduino files.  It should look like this:

void setup() {

  // put your setup code here, to run once:


}


void loop() {

  // put your main code here, to run repeatedly:


}


Before moving forward we are going to figure out what we see here.  Just like when we talked through our code, there are two sections: Setup and Loop.  Setup runs once at the beginning of the program and Loop continues repeatedly until stopped.

These sections are bound by curly brackets, { } , which are very important.  These mark out the beginning and end of these two sections.  For example, anything which you would like to run repeatedly must be within the brackets of Loop.

There are also some words which are just word phrases.  These are commented out by putting // in front of the line.  This means the computer will not read these lines, but humans which are helping trouble shoot code or even you after not looking at the work in progress code can read these and know what you are planning.

So now, we will put our pseudocode into the Arduino sketch using the commenting function.  This turns into: 

void setup() {

  // LED at Pin 13, Output  

}


void loop() {

// LED on
// Wait a second
// LED off
// Wait a second
 // Repeat (this one is already included in the section title)

}

Coding it!

Now that we have our pseudo code outlining exactly what we are doing, we will start to translate from the spoken language that we are using to the Arduino coding language.

For out Setup, we will take LED at Pin 13, Output  and translate this to pinMode(13, OUTPUT);  Let's break this down a bit.  'pinMode' is what tells the Arduino what to expect to do and where, in this case it will OUTPUT at pin 13.  At the end of this phrase we put a semi-colon which is similar to a period at the end of a sentence.  Missing this will cause the most errors.

For our loop, LED on  turns into digitalWrite(13,HIGH); which again needs some explaining. 'digitalWrite' is telling a digital pin to do something.  In this case it is pin 13, and the output should be the high value.  Think back to our description of the pins and how the digital pins often are only dealing with High and Low.  In this case, high is on.

Wait a second becomes delay(1000); which is just telling the Arduino to delay the next process by 1000 milliseconds (1 sec).

NOTE: As some of you have probably noticed, the capitalization seems to be different than we use in English, and this is something that we should be careful of.  If for example, you were to type High instead of HIGH, the Arduino would not successfully run your code.

After our translating the new code will look like this: 

void setup() {

  // LED at Pin 13, Output
 pinMode(13, OUTPUT);

}


void loop() {

  // LED on
  digitalWrite(13, HIGH);
  // Wait a second

  delay(1000);
  // LED off

  digitalWrite(13, LOW);
  // Wait a second

  delay(1000);

}

Uploading the Code

Now that we have our code written, we should test it.  This is done using the verify button at the top which looks like a check-mark.  If this finds an error, it will alert you to the error at the bottom of the window in red text.  

If there was no error, then you should click the arrow directly next to the check-mark.  This is the upload button and will send the code to the board itself. 

NOTE: It is necessary to go to the drop-down menu tools, then port, and select your board from that list (which is often only a single option).  Many times you will only have to do this the first time you plug your Arduino in, but some computers and some boards need more selections.  If there is an upload error, and not a code error, this could be why.

If you have followed through to this point, your light should be blinking!  

Congratulations! 

There are more resources which will help you grow here: NuVu Toolbox 

There are also great tutorials through Arduino.cc and generally available through googling "Arduino code" with the name of your sensor or device.