Anda di halaman 1dari 11

Intro to Coding with Arduino

Sunday, February 12, 2012

Basic concepts
Sketch: an Arduino program you copy, modify or create Compiling: Behind-the-scenes translation of Arduino code into C-language code void Setup ( ): A function Arduino needs dened so that it knows, before it gets started doing its tricks, how you are using the input & output pins. It runs before the Loop function. void Loop ( ): Another Arduino function; this one describes the looping (repeated) actions you want the program to perform. It runs until the power is turned off. NOTE: neither Setup nor Loop return a value, so you have to tell the computer that they will be void

Sunday, February 12, 2012

Syntax and Punctuation


Like it or not, you have to be precise with syntax, punctuation and spelling because Arduino cannot read your mind!!
Arduino vocabulary is made up of single words with no spaces; they are often compound words (e.g. digitalWrite), but if you put in a space you will get an error message. Arduino is written in what is called camelcase: the rst letter of each word (except the rst word) is capitalized. This is a pretty standard programming convention. Each line of code must end with a semi-colon. For example: digitalWrite (13,HIGH); Blocks of code must be placed within curly brackets. For example: void setup ( ) { pinMode (13, OUTPUT); } Blank spaces are generally ignored by Arduino (except in strings of text) but it is case-sensitive. digitalWrite is how it spells that function; if you type in DigitalWrite you will get an error message. Comment a line (or less) of text by typing // before the non-code words. To comment more than a single line of text, type /* before the non-code segment and */ after it.

Sunday, February 12, 2012

Basic Arduino Vocabulary


Arduino is a language with its own words. You can make up words for Arduino, for example when declaring variables, but you cant use their words. Theyre already taken! Essential terms:
int Used to declare a variable that will be a whole number (an integer) oat Used for working with decimals, or if you will be performing math operations that will result in numbers with decimals (e.g., converting centigrade to farenheit) char a single character, such as a letter or number, used in a string of text, a space between words in a string is also a character boolean something that is either TRUE or FALSE = this makes something equal to something else, e.g., initialCount = 200 = = used for comparison, often in an if else statement; e.g., if (count = = 20) { count = 0; } This checks to see if the count (perhaps the number of blinks) has reached 20, in which case it starts counting again at 0 ++ in a repeat for loop, this is just a shortcut for adding 1 to the value of the variable; thus, count ++ is the same as count = count +1

Sunday, February 12, 2012

Basic Arduino Vocabulary, continued HIGH, LOW basically on and off; HIGH sends 5 volts to the output; LOW sends none. < less than > more than ! = not equal to + addition / * subtraction division multiplication

To avoid confusion, numbers are often placed within parentheses so the computer knows which math function to do rst. For example, 9 + 5 * 7 might mean to add 9 and 5 together and then multiple the total by 7. Or, it might mean to add 9 to the product of 5 times 7. With parentheses, those two very different calculations could be written as follows: (9 + 5) * 7 or 9 + (5 * 7)
Sunday, February 12, 2012

Variables
A variable is a way of storing information (in the form of a value) efciently for later use. It can make editing code much easier. A variable stores a value, but this value can vary (thus its name). This makes it ideal for interaction. It can count, it can keep score, it can remember what you did, etc. Variables need to be declared before you use them. For example, the rst line declares (sets up) the integer variable delayTime and the second line sets it to 200 (milliseconds): int delayTime; delayTime = 200; This could be written more efciently as: int delayTime = 200; BTW, notice that I used camelCase writing for my variable. I didnt have to I could have used any single, non-Arduino word, with whatever combo of upperand lower-case I wanted, but this is easier to read, and other programmers will get it.
Sunday, February 12, 2012

Why is this easier?


int ledPin = 13; int delayTime = 200; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(delayTime); digitalWrite(ledPin, LOW); delay(delayTime); }

Because.... this is a very simple script, but ledPin appears three times. What if we want to change the pin were using? All we have to do is change the value of the variable from 13 to something else. No big deal here either way, but with a more complex script it will make life easier and help you keep track of what youre trying to do.

Sunday, February 12, 2012

Counting
Instead of blinking an LED at a xed rate, what if we want it to speed up or slow down? All we need is a variable that we can add to with each loop. If we declare the variable delayTime and start it at 200, we can add to it with each loop, which will make it slower and slower (because we are delaying more): int ledPin = 13; int delayTime = 200; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(delayTime); digitalWrite(ledPin, LOW); delay(delayTime); delayTime = delayTime + 100; }

The key is in the last line, which you could translate as take the old delayTime and create a new delayTime that is 100 milliseconds longer. Before you know it, the blinks would be very far apart.

Sunday, February 12, 2012

If statements
Using a variable to count allowed us to slow down the blinking, but that LEDs just going to get slower... and slower....... and slower..... What if at a certain point we want it to start over, start blinking fast again? For that, we need an if statement. We want to tell the computer if the delayTime gets to a certain number, reset it back to 200. Heres the syntax:
int ledPin = 13; int delayTime = 200; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(delayTime); digitalWrite(ledPin, LOW); delay(delayTime); delayTime = delayTime + 100; if (delayTime > 3000) { delayTime = 100; } }
Sunday, February 12, 2012

Random numbers
Sometimes its more fun to randomize the patterns you are creating! The random function looks like this: random (1,7); This returns a random number between 1 and 6 (go gure). But what if we want to randomize the speed of our blinking? We merely plug in a random number for our delayTime.
int delayTime = 100; int ledPin = 9;} void setup() { pinMode(ledPin, OUTPUT); Serial.begin (9600); } void loop() { digitalWrite(ledPin, HIGH); delay(delayTime); digitalWrite(ledPin, LOW); delay(delayTime); int crazyNumber = random (30,1500); delayTime = crazyNumber; Serial.println (crazyNumber); }

The serial printer is a test window where you can follow your code. In this instance it is keeping track of the variable crazyNumber.
Sunday, February 12, 2012

Repeat Loops (the for command)


Lets say we want to turn a series of LEDs on and then off in a loop. We could do that with a lot of copying and pasting, but there is a faster way: the for command, which can cycle through anything consecutive, in this case a row of LEDs. It uses this syntax: for (int i = 0; i < 8; i++ ) This translates as: declare a variable i to be equal to 0, but every time we loop back to this line of code, add 1 to the value of i, but it only does this so long as i remains less than 8. In other words, it is counting form 0 to 8. Can you gure out how we would use it to
light up multiple LEDs in sequence? int timer = 100; // The higher the number, the slower the timing.

void setup() { // use a for loop to initialize each pin as an output: for (int thisPin = 1; thisPin < 5; thisPin++) { pinMode(thisPin, OUTPUT); } } void loop() { // loop from the lowest pin to the highest: for (int thisPin = 1; thisPin < 5; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } // loop from the highest pin to the lowest: for (int thisPin = 5; thisPin >= 1; thisPin--) { turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); }

}
Sunday, February 12, 2012

Anda mungkin juga menyukai