Arduino Programming Part1 Notes
Arduino Programming Part1 Notes
Arduino Programming Part1 Notes
Part 1
EAS 199A, Fall 2010, Lecture 5
Gerald Recktenwald
Portland State University
gerry@me.pdx.edu
Arduino Programming: EAS 199A
Overview
Discuss details, now that you had a taste
Arduino Environment
Basic code components
!
Two required functions: startup() and loop()
!
Variables
!
Calling built-in functions
2
Arduino Programming: EAS 199A
References
These notes borrow from
!
Arduino web site
! http://arduino.cc/en/Guide/Environment
! http://arduino.cc/en/Tutorial/HomePage
!
Adafruit tutorial #1 and 2
! http://www.ladyada.net/learn/arduino/lesson2.html
!
Leah Buechleys Introduction to Arduino
! http://web.media.mit.edu/~leah/LilyPad/03_arduino_intro.html
3
Arduino Programming: EAS 199A
Basic Process
Design the circuit:
!
What are electrical requirements of the sensors or actuators?
!
Identify inputs (analog inputs)
!
Identify digital outputs
Write the code
!
Build incrementally
! Get the simplest piece to work rst
! Add complexity and test at each stage
! Save and Backup frequently
!
Use variables, not constants
!
Comment liberally
4
Arduino Programming: EAS 199A
Writing and Downloading Code
5
A
A
Write sketch on PC
Download sketch to Arduino
Arduino Programming: EAS 199A
Running Code While Tethered
6
Arduino interacts
with its environment
A
Run sketch on Arduino
and send data back to PC
Serial communication
back to host
Arduino Programming: EAS 199A
Running Code Stand-Alone
7
A
Run Arduino in stand alone mode
Arduino interacts with
its environment and
runs on battery power
Arduino Programming: EAS 199A
Arduino IDE
8
IDE =
Integrated
Development
Environment
Message pane
Code pane
Stop serial monitor
New sketch
Open sketch
Save sketch
Upload sketch
Open Serial monitor
Tab
controls
Verify/Compile
http://www.arduino.cc/en/Guide/Environment
Arduino Programming: EAS 199A
Arduino IDE
9
Message pane
Code pane
Stop serial monitor
New sketch
Open sketch
Save sketch
Upload sketch
Open Serial monitor
Tab
controls
Verify/Compile
http://www.arduino.cc/en/Guide/Environment
Arduino Programming: EAS 199A
Arduino Web Site References
10
Overview of the development environment
!
http://www.arduino.cc/en/Guide/Environment
Language reference
!
http://arduino.cc/en/Reference/HomePage
Code tutorials
!
http://arduino.cc/en/Tutorial/HomePage
Arduino Programming: EAS 199A
Code Structure: Header
11
Header provides information.
Later, it will also contain code
Arduino Programming: EAS 199A
Code Structure: setup function
12
setup function is executed
only once at the start
Arduino Programming: EAS 199A
Code Structure: loop function
13
loop function is
repeated indenitely
Arduino Programming: EAS 199A
Code
14
Digital I/O Functions:
pinMode
digitalWrite
digitalRead
pinMode(13, Output)
prepare pin 13 for
outputs of voltage
Arduino Programming: EAS 199A
Code
15
Digital I/O Functions:
pinMode
digitalWrite
digitalRead
digitalWrite(13, HIGH)
Sets pin 13 to voltage
that means on
Arduino Programming: EAS 199A
Code
16
Digital I/O Functions:
pinMode
digitalWrite
digitalRead
delay(1000);
tells microcontroller to do
nothing for 1000 ms = 1 s
Arduino Programming: EAS 199A
Code
17
Digital I/O Functions:
pinMode
digitalWrite
digitalRead
digitalWrite(13, LOW)
Sets pin 13 to voltage
that means off
Arduino Programming: EAS 199A
Arduino Variable Types
18
int Integer values: 1, 2, 3, -4, 7234
oat Values with non-zero fractional part, 7 digits
double Currently the same as a oat. Normally a
double stores values with non-zero fractional
part, 15 digits
char Character values: a, b, D, 1
boolean True or false values
Arduino Programming: EAS 199A
Using variables and functions
19
Assigning values:
int red_LED_pin = 5;
pinMode( red_LED_pin, OUTPUT );
digitalWrite( red_LED_pin, HIGH );
Denes the variable type as an integer
Denes the variable name as red_LED_pin
calls the function named pinMode
Uses the value stored in red_LED_pin
calls the function named digitalWrite
HIGH and OUTPUT are pre-dened constants