arduino lecture 4 - interactive media cs4062 semester 2 2009

11
Programming the Arduino CS4062 - Eoin Brazil - Semester 2 - 2009 http://www.flickr.com/photos/collinmel/2317520331/

Upload: eoin-brazil

Post on 31-Aug-2014

6.979 views

Category:

Technology


0 download

DESCRIPTION

CS4062 Masters in Interactive Media - Fourth Arduino Lecture - March 18th 2009 - University of Limerick. This lecture presents a short review and introduction to programming concepts relevant to Arduino. This was aimed at a digital media / music technology masters student audience.

TRANSCRIPT

Page 2: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

Programming an Arduino

Write program

Compile (check for errors)

Reset board

Upload to board

Page 3: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

An Arduino “Sketch” Declare variables at top

Initialize setup() – run once at beginning, set pins

Running loop() – run repeatedly, after setup()

Page 4: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

An Arduino “Sketch”

int ledPin = 13; – led connected to control pin 13

Global Variablesint aSensor = 0; – setup sensor 'aSensor' on analog pin 0

int statePin = LOW; – use this to hold the state of a pin

Page 5: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

An Arduino “Sketch”

setup()

pinMode() – set a pin as input or output

serial.Begin() – setup to `talk' to the computer

Page 6: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

An Arduino “Sketch”

setup()

pinMode(ledPin, Output); – set the pin `ledPin' as an output

serial.Begin(9600); – talk to the computer at 9600 baud rate

Page 7: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

An Arduino “Sketch”

loop()

digitalWrite() – set a digital pin high/low

digitalRead() – read a digital pin’s state

analogRead() – read an analog pin

analogWrite() – write an “analog” PWM value

delay() – wait an amount of time

millis() – get the current tim

Page 8: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

char - ascii character, 8 bits

short - short integer, 16 bits, -32768 to 32767

int - default integer, 16 or 32 bits

long - large integer, at least 32 bits

float - 32 bit floating point (e.g. 3.13)

double, long double - 64 bit or greater

`C’ language

Page 9: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

`A’ - upper case A

`\n’ - newline character

`\t’ - tab character

`\0’ - null character (it is digit not char)

`\012’ - character with octal value of 12 which is decimal 10

Character constants

Page 10: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

`//’ - single line comment

`/* .... */’ - multiline comment

+ Addition - Subtraction * Multiplication

/ Division % Remander (mod)

== != <= >= < >

= is not ==

Commenting and Operators

Page 11: Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

Boolean operators ! - not && - and || - or

if (<statement>) { <statement/s> }

if - else

while (<statement>) { <statements/s> }

Essential C - http://cslibrary.stanford.edu/101/

More Operators