Lesson 1: Intro to Arduino Programming

Check out the Sew Electric Programming Tutorial for a more thorough and up-to-date introduction to programming your LilyPad. This page will give you a quick introduction to programming the LilyPad Arduino.

Basic Programming Steps

Programming the LilyPad and executing your program takes place in four stages:

1. Write a program

First, you write your program. The Arduino environment lets you program in the programming language called C, which is what we’ll be using. You must obey the C language format for writing programs.

2. Compile the program

Second, you compile your program, translating the code that you wrote to code that the computer chip on the LilyPad can understand. If you’ve made any mistakes in your code, they will be detected during this phase and you will have to correct them and recompile your code before you move on to the next step.

3. Load the program onto the LilyPad

Third, you load your compiled code onto your LilyPad, moving the code from your computer to the LilyPad.

4. The program executes on the LilyPad

Once it has been loaded, your program executes on your LilyPad. The LilyPad does what you told it to do.
 

Basic Code Elements

So, that’s the process you go through in writing and executing a program, but how do programs work? Well, all programs execute in an orderly fashion, obeying your written commands (the lines in your program) line by line as they are written. There are a few different types of commands or code elements in most programming languages. To start with, we will use comments, simple statements and conditional statements.

Since we will be using the C programming language, this section will describe what these elements look like in the C language, and we will be using the Arduino Language Reference to get more detailed information on how each code element works.

1. Comments

Comments are pieces of code that are ignored by computers. You can use them to make notes in your code, but they’re only for reference. They don’t affect the behavior of your program. Anything written on a line after two slash characters // is a comment. Comments show up a greyish brown in the Arduino window. Here’s are a couple example comments:

// This is a comment. It doesn’t do anything.
// Comments just make code easier for you and other people to understand

Anything written between “/*” and “*/” characters is also a comment. Comments that start with “//” can only be on one line, but comments between the “/*” and “*/” characters can take up several lines. These types of comments will show up in that same greyish brown in the Arduino window so that you can always spot them easily. Here’s how we’d rewrite the comments above in this style:

/* This is a comment. It doesn’t do anything.
Comments just make code easier for you and other people to understand */

Throughout the rest of this page, we will use comments as you would normally use them in a program, to explain what pieces of code are doing. Check out the Comment Page of the Arduino Language Reference for more information.

2. Simple statements

A simple statement is a single instruction. It takes up a single line and is always followed by a semi-colon. Here are some example statements:

int ledPin = 13; // This statement declares a variable called “ledPin” and sets its initial value to 13.
// We’ll use “ledPin” later to control the LED that is on the LilyPad
int switchPin = 2; // This creates a variable called “switchPin” and sets its initial value to 2
// We’ll use “switchPin” later to read data from a switch attached to petal 2.
int switchValue; // This creates a variable called “switchValue”, but does not initialize it
delay(1000); // This tells the LilyPad to do nothing for 1000 milliseconds (1 second).
digitalWrite(ledPin, HIGH); // This sets ledPin (pin 13 on the LilyPad as defined above) to +5 volts
// Setting pin 13 to +5 volts/HIGH will turn the LED on the LilyPad on.
digitalWrite(ledPin, LOW); // This sets ledPin to 0 volts
// Setting pin 13 to 0 volts/LOW will turn the LED on the LilyPad off.

 

Notice how each statement is followed by a comment that describes what the statement is doing.
To get a more thorough explanation of what each statement is doing, we would turn again to the Arduino Language Reference. The reference contains an entry for every simple statement form in the Arduino language. To understand the “int ledPin = 13;” statement better, see the variable declaration section of the Arduino reference. To understand the “delay(1000);” statement better, see the delay page of the Arduino reference. To understand the “digitalWrite(ledPin, HIGH);” statement better, see the digitalWrite page of the Arduino reference.

3. Conditional statements

Conditional statements are statements that consist of a condition and then a series of statements in brackets like this { } that execute when the condition is met. See the Control Structures section of the Arduino Language Reference for more information. Here are some example conditional statements:

if (switchValue == LOW) { // This is the condition followed by the first bracket
      digitalWrite(ledPin, HIGH); // This tells the LED on the LilyPad to turn on
} // Finally, you have the closing bracket

 

Notice how the condition above consists of the word “if” and then a condition in brackets like this ( ). This is the basic form that a conditional statement always takes. To make sense of conditional statements, read through them as though they were regular english. In the case above, you would read it “if the variable switchValue is equal to LOW then turn the LilyPad’s LED on.” Here’s another example:

while (switchValue == LOW) { // This line is the condition followed by the first bracket
      digitalWrite(ledPin, HIGH); // This tells the LED on the LilyPad to turn on
      delay(100); // This tells the LilyPad to wait for 100 milliseconds
      digitalWrite(ledPin, LOW); // This tells the LED on the LilyPad to turn off
      delay(100);
      switchValue = digitalRead(switchPin); // This stores a new value into the variable switchValue
}

 
So, this statement would be read like this “as long as the variable switchValue is equal to LOW, do the following: turn the LilyPad’s LED on, then do nothing for 100 milliseconds (1/10th of a second), then turn the LilyPad’s LED off, then do nothing for 100 milliseconds, then store a new value into the variable switchValue”.

Arduino Program Structure

The previous section looked at the different code elements of the Arduino language. Now let’s see how to put these elements together into a complete program.

Each Arduino program has three main parts: a section where you declare variables, a “setup” section and a “loop” section. When your program executes, it will first define your variables, will then execute the setup section once and will then execute the loop section over and over. Here is a full example program in the Arduino window:

Now, let’s see what this program actually does when we send it to the LilyPad…

Up Next

Lesson 2: Light (LEDs)