Intro to Arduino

Basics

  • Like Processing, there are two functions that must be defined:
  • Void setup()
  • Void loop() – the equivalent to draw() in processing
  • Symbols 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 line comment
  • /* multiline comment */
  • usually used in the beginning, what your project does, what sensors you need and the pins, anywhere you get sources with a url to where you got it,  your name
  • HIGH – turns on a pin (sends high volts to a pin)
  • LOW – turns off a pin
  • INPUT – sets a pin to input mode
  • OUTPUT – sets a pin to output mode
  • From http://arduino.cc/en/Tutorial/Blink
  • Variable data types:
  • Boolean
  • True or false
  • Like a switch, only 2 options
  • Int, char primitive data types
  • No classes in arduino
  • Char
  • Single character stored as a number between -128 to 127
  • Byte
  • Number between 0 and 255
  • Int
  • Two bytes: -32,768 and 32, 767
  • A large number, when you need something bigger than a byte
  • Unsigned int
  • Two bytes, but positive: 0 to 65, 535
  • Long
  • Twice an int: -2,147,483,648 to 2,147,483,647
  • Unsigned long
  • Only positive values: 0 to 4, 294, 967, 295
  • Float
  • Holds a number with decimal points. Uses 4 bytes of ram so try not to use floats
  • Double
  • Like floats but uses 8 bytes. Make 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 string 1[] = “Arduino”;

  • Or

//7 chars plus 1 null char

char string 2[8] = “Arduino”

  • Array
  • An indexed list of variables. Eg:
  • Int brightness[5] = {0, 20, 40, 60, 80, 100};
  • The square brackets after the identifier indicates that it is an array. You don’t actually use the word “array”
  • If(condition)…else – branches your program based 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 specified number of times:
  • For(int i=0; 1<10; i++) {

Serial.print(“ciao”)”

}

  • Switch is a good replacement for long lists of if statements
  • Use it if you need more than 3 if statements
  • Switch(sensorValue) {

case 23;

digitalWrite(13, HIGH);

break;

case 46;

digitalWrite(12, HIGH);

break;

default

  • 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(12, HIGH);

delay(100);

sensorValue = analogRead(1);

}

  • Do while – a combination loop and if statement. As long as the condition remains true the code block will execute repeatedly. Same as a while except that the code inside the block will always run at least once.

do (sensorValue < 512) {

digitalWrite(13, HIGH);

delay(100);

digitalWrite(12, HIGH);

delay(100);

sensorValue = analogRead(1);

}

while (sensorValue < 512);

  • Comparison Operators:
  • The conditions for if, while and for statements must evaluate tp true or false. One way to accomplish this is to compare one value to another
  • You may combine more than one condition where ever you can use a condition. To do this you use the Boolean operators:
  • && — and (binary) (has to have 2 things to be compared)
  • || — or (binary)
  • ! – not (unary) (only one value)

//if both conditions are true, the whole thing is true

((sensor >= 5) && (sensor <= 10))

//if one condition is true, the whole thing is true

((sensor > 50) || (sensor < 20)

//if sensor doesnt exist or is null

(!sensor)

  • compound operators are shortcuts for very common operations
  • a++; — add one to the current value of a
  • a–; — subtract of from 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
  • a *= 5; — multiply the current value of a by 5
  • a/=5; — divide the current value of a by 5
  • input and output functions:
  • arduino comes with built in fuctions to handle input and output
  • pinMode(pin, mode);
  • reconfigures a digital pin to be either an INPUT or OUTPUT pin
  • pinMode(7, INPUT); //turns pin into an input, used to read from a pin
  • digitalWrite(pin,value);
  • turns a pin either on or off. The pin must have already had its pinMode set to OUTPUT for this to work. Either 0V or 5V.
  • 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 will 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 o appear to have an analog value
  • analogWrite (9, 128);//dim an led on pin 9 to 50% brightness
  • can write anything from 0V to 5V
  • 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 specified by value
  • delay(500);//pauses the program for half a second
  • math functions
  • 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
  • abs(x)
  • returns absolute value of x. turns negative numbers into positive ones
  • 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)
  • map 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);
  • arduino comes with some common math a 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 Ö2
  • randomSeed(seed)
  • resets the random number generator
  • “if you have an unconnected analog pin, it will pick up random noise from the surrounding environments (radio waves, cosmic rays, electromagnetic interference from cell phones and fourescent 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 specified the lower bound is 0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: