| introduction to
programming the LilyPad Arduino This page will give you a really quick introduction to programming the LilyPad Arduino. For a much more thorough introduction to electronics, programming, and the regular (non-LilyPad) Arduino, see ladyada's excellent tutorial. If you are new to electronics and programming you will probably want to jump back and forth between my tutorial pages (for LilyPad specific information) and ladyada's (for general information about programming and electronics). Click on one of the links below to jump to a section. basic programming steps basic code elements Arduino program structure |
|||||||||||||||||||||||||||||||||||||||||||
| basic
programming steps Programming the LilyPad and executing your program takes place in four stages:
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. 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.
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:
// 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:
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:
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:
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:
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... |
|||||||||||||||||||||||||||||||||||||||||||