Вы находитесь на странице: 1из 6

Introduction To Arduino Programming

Based on the appendix C of Getting Started With Arduino The Project: Must work, it must use physical computing and it must have a concept 50% Concept, 50% Work Like Processing there are two functions that must be dened: void setup() void loop() Arduino uses the Processing IDE note that it is called loop() not draw() Symbols that are commonly used: ; -- the semicolon must terminate every statement (line of code) {} -- curly braces are used to mark blocks of code Two types of comments: // -- single lined comment /* */ -- multiline comment (list any sort of variables that you use, and your name Predened constants: HIGH -- turns on a pin (will send 5 volts to a digital pin) LOW -- turns off a pin (will send 0 volts to a digital pin) INPUT -- sets a pin to input mode OUTPUT -- sets a pin to output mode Reference http://arduino/cc/en/Tutorial/Blink Basics: Variable Data Types PROCESSING a class and data type are the same thing ARDUINO has primitive data types, there are no classes (eg. a string is an array or chars) boolean true or false exactly the equivalent of a switch (on/off, up/down) char a single character, stored as a number between -128 to 127 byte number between 0 and 255 int two bytes --> -32, 768 and 32, 767 unsigned int two bytes, but positive: 0 to 65, 535 (these numbers are weird because they are numerical conversions of binary numbers) long

Everything in here is referring to a physical thing, addressing computational things

" " " "

"

twice as much as an int --- -2, 147, 483, 648 to 2, 147, 483, 647 you shouldn!t ever need this it hold huge numbers unsigned long only positive values: 0 to 4, 294, 967, 295 oat holds number with decimal points. Uses 4bytes of ram so try not to use oats double like oats but uses 8bytes of ram. Makes sure you have a very good reason to use a double. Otherwise stay away from them string basically an array of chars eg: " //7 chars plus 1 null char " char string1[] = Arduino; or //7 chars plus 1 null char char string2[8] = Arduino; these two are equivalent, they do the same thing. In the second example you dont actually need to ll in the array, its just saving the data spots. The rst is to dene the array and populate the array array an indexed list of variables eg: int brightness[5] = {0, 20, 40, 60, 80, 100}; the square brackets after the identier indicates that it is an array. You don!t actually use the word array

Basics: Control Structures if(condition)...else -- branches your program on the boolean state of the included condition " if(val == 1) { " " digitalWrite(warningLight, HIGH); " } else { " " digitalWrite(warningLight, LOW); " } for(counter; condition; newcounter) --lets you repeat a block of code a specied number of times: " for(int i = 0; i<10; i++) { " " Serial.print(ciao); " } switch is a good replacement for long lists of is statements the way the switch works is that you do switch and then a value " " if the sensor value is 30..do this, " " if the sensor value is 40..do that while -- a combination loop and if statement. As long as the condition remains true the code block will execute repeatedly. " "

" " " " " " " "

" sensorValue = analogRead(1); " while (sensorValue < 512) { " digitalWrite(13, HIGH); " delay(100); " digitalWrite(13, HIGH); " delay(100); " sensorValue = analogRead(1); " } if the information is false the rst time, the program will jump right out and ignore the rest of the code. There may be a time when you will want the code to run at least once before it preforms the while. use this:

do while -- a combination loop and if statement. As long as the condition remains true the code block will execute repeatedly. Same as while except the code inside the block will always run at least once. The check happens at the bottom, not the top " " do{ " " " digitalWrite(13, HIGH); " " " delay(100); " " " digitalWrite(13, HIGH); " " " delay(100); " " " sensorValue = analogRead(1); " " } " " while (sensorValue < 512); Basics: Comparison Operators the conditions for if, while, and for statements must evaluate to true or false. One way to accomplish this is to compare one value to another. //is val1 equal to val2 (val1 == val2) etc You may combine more that one condition where ever you can use a condition. To do this you use the boolean operators: && -- and (binary) need to have something on either side of it || -- or (binary) need to have something on either side of it ! -- not (unary) only needs something on one side //if both conditions are true, the whole thing is true ((sensor >= 5) && (sensor <= 10)) if either one of these is false, it jumps right out. If the rst is false, it will never test the second //if one condition is true, the whole thing is true ((sensor > 50) || (sensor < 20))

//if sensor doesn!t exist or is null (!sensor) Basics: Compound Operators compound operators are shortcuts for very common operations a++; -- add one to the current value of a a--; -- subtract one from the the current value of a a = a + 5; -- add 5 to the current value of a a += 5; -- add 5 to the current value of a a -= 5; -- subtract 5 from the current value of a by 5 a *=5; -- multiply the current value of a by 5 a/=5; --divide the current value of a by 5 it always looks at the other side of the equals sign rst Basics: Input and Output Functions Arduino comes with built in functions to handle input and output " pinMode(pin, mode) " recongures a digital pin to be either an INPUT or OUTPUT pin " pinMode(7, INPUT); //turns pin 7 into an input ! " " " " " " " " digitalWrite(pin, value) turns a digital pin either on or off. The pin must have already had it!s pinMode set to OUTPUT for this to work " digitalWrite(7, HIGH); //turns on digital pin 7

digitalRead(pin) reads the state of an input pin. pinMode must have already been set to INPUT for this to work. The value of the pin with either be HIGH or LOW depending if there is any voltage detected at the pin " val = digitalRead(7); // reads the value of pin 7 into val

Arduino comes with built in functions to handle input and output (continued) " analogRead(pin) ! reads the voltage applied to an analog input. Returns a value between 0 and " 1023 that represents a voltage between 0V and 5V " " " val = analogRead(0); reads analog input pin 0 into val

" ! " " " "

analogWrite(pin, value) changes the PWM rate on any pin marked PWM. value may be a number between 0 and 255 which represents the scale between 0V and 5V. PWM stands for pulse width modulation. A technique that allows digital pins to appear to have an analog value. Must be from the PWM pins marked on the arduino " analogWrite(9, 128); // dim an led on pin 9 to 50% brightness

0-13 Each pin with numbers is digital read and digital write Basics: Time Functions Arduino comes with built in functions to measure elapsed time and for pausing the sketch millis() ! returns the number of milliseconds since the sketch started " " unsigned long lastTime = millis(); " " unsigned long duration ; " " //some code that does stuff " " duration = millis() - lastTime; " " //computes the time elapsed since lastTime delay(value) ! pauses the program for the number of milliseconds specied by value. " " delay(500);//pauses the program by half a second Arduino comes with some common math and trig functions " min(x, y) " " returns the smaller of x and y " max(x, y) " " returns the larger of x and y ! ! ! they have to be numbers " abs(x) ! ! returns absolute value of x. Turns the negative numbers into positive " " numbers " constrain(x, a, b) ! ! if x is less than a it will return a ! ! if x is greater than b it will return b " " if x is between a and b it will return x " map(value, fromLow, fromHigh, toLow, toHigh) ! ! maps a value that will fall within one range of numbers so that the value " " will proportionally change to fall within another range of numbers " " val = map(analogRead(0), 0, 1023, 0, 100); " " 0 would map to 0 and 1023 maps to 100 for this variable"

Basics: Math Functions ! Arduino comes with some common math and trig functions ! ! " " ! " " ! " " " " " " " pow(base, exponent) returns a double result of raising a number (base) to an (exponent) value " double x = pow(y,22) //sets x to y raised to the 22nd power sqrt(x) returns the square root of a number " double a = sqrt(2) //returns square root 2 randomSeed(seed) resets the random number generator If you have an unconnected analog pin, it will pick up random noise from the surrounding environment (radio waves, cosmic rays, electromagnetic interference from cell phones and uorescent lights, and so on) - Getting Started with Arduino page 108 randomSeed(analogRead(5)); //randomize using noise from pin 5

random(max), random(min, max) " returns a random number between min and max - 1. if min is not specied the " lower bound is 0 ~RESOURCES~ see documents on BlackBoard " "