| e-textile
construction kit introduction part 2 introduction to programming in C for Arduino-textile basic e-textile kit programming steps The Arduino environment lets you program in the programming language called C, which is what we'll be using. Programming the e-textile patch and executing your program takes place in four stages: coding First, you write your program. You must obey the C language format for writing programs. We will explain this in a second. compiling Second, you compile your program, translating the code that you wrote to code that the computer chip on the e-textile patch 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. loading Third, you load your compiled code onto your e-textile patch, moving the code from your computer to the e-textile patch. executing Once it has been loaded, your program executes on your e-textile patch. basic code elements All programs execute in an orderly fashion, obeying your commands line by line as they are written. There are a few different types of things you write in a program. We will use comments, simple statements and conditional statements. comments Comments are pieces of code that are ignored by the computer. You can use them to make notes in your code, but they're only for reference. They don't affect your program. Anything written on a line after two slash characters // is a comment. Comments show up a light grayish brown in the Arduino window. An example comment:
A simple statement is a single instruction. It takes up a single line and is always followed by a semi-colon. For example, the following statement turns the LED on the e-textile patch on:
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. For example, the following conditional statement turns the LED on and then waits for 1 second when switch2 is pressed:
LEDOn(); //this statement tells the patch to turn the LED on. delay(1000); //this statement tells the patch to delay for 1000 milliseconds (1 second). } //finally, you have the closing bracket
LEDOn(); x=x+1; } arduino program structure Each Arduino program has three main parts: a section where you declare variables, a "setup" section and a "main" 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 an example program that blinks the LED on and off continuously:
//the variable declaration section takes place at the very top of your program int time; //the setup section takes place inside the setup function //inside the brackets { } after void setup() void setup() { //this is the setup section time = 1000; tab2Output(); } //the main section takes place inside the loop function //inside the brackets { } after void loop() void loop() { //this is the main section LEDOn(); delay(time); LEDOff(); delay(time); } Click here for the Arduino software reference Click here for my Arduino-textile library reference next step... Click here to continue to part 3: Getting started using the e-textile kit. Click here to return to the main introduction page This material is based upon work supported by the National Science Foundation under Grant No. 0326054. Any opinions, findings and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation (NSF). |
|