Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
147 views

Arduino Programming: EAS 199A, Fall 2010, Lecture 5 Gerald Recktenwald Portland State University Gerry@me - Pdx.edu

This document provides an overview and introduction to Arduino programming. It discusses the Arduino development environment, basic code components like the setup() and loop() functions, and how to write, compile, upload and run code on an Arduino board both while tethered to a computer and in stand-alone mode. It also references tutorials and documentation about the Arduino language and lists common variable types and digital input/output functions used in Arduino code.

Uploaded by

Aishwary Angadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Arduino Programming: EAS 199A, Fall 2010, Lecture 5 Gerald Recktenwald Portland State University Gerry@me - Pdx.edu

This document provides an overview and introduction to Arduino programming. It discusses the Arduino development environment, basic code components like the setup() and loop() functions, and how to write, compile, upload and run code on an Arduino board both while tethered to a computer and in stand-alone mode. It also references tutorials and documentation about the Arduino language and lists common variable types and digital input/output functions used in Arduino code.

Uploaded by

Aishwary Angadi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Arduino Programming

Part 1
EAS 199A, Fall 2010, Lecture 5

Gerald Recktenwald
Portland State University
gerry@me.pdx.edu
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

Arduino Programming: EAS 199A 2


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 Buechley’s Introduction to Arduino
‣ http://web.media.mit.edu/~leah/LilyPad/03_arduino_intro.html

Arduino Programming: EAS 199A 3


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 first
‣ Add complexity and test at each stage
‣ Save and Backup frequently
❖ Use variables, not constants
❖ Comment liberally

Arduino Programming: EAS 199A 4


Writing and Downloading Code

Write sketch on PC

Download sketch to Arduino

Arduino Programming: EAS 199A 5


Running Code While Tethered

Run sketch on Arduino


and send data back to PC
Arduino interacts
with its environment

Serial communication
back to host

Arduino Programming: EAS 199A 6


Running Code Stand-Alone

Run Arduino in stand alone mode

Arduino interacts with


A its environment and
runs on battery power

Arduino Programming: EAS 199A 7


Open sketch
Arduino IDE New sketch Save sketch
Upload sketch
Stop serial monitor
Open Serial monitor
Verify/Compile

IDE =
Integrated
Development Tab
controls
Environment
http://www.arduino.cc/en/Guide/Environment

Code pane

Message pane

Arduino Programming: EAS 199A 8


Arduino IDE
Open sketch
New sketch Save sketch
Upload sketch
Stop serial monitor
Open Serial monitor
Verify/Compile

Tab
controls

http://www.arduino.cc/en/Guide/Environment
Arduino Programming: EAS 199A 9
Arduino Web Site References

• 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 10


Code Structure: Header

Header provides information.


Later, it will also contain code

Arduino Programming: EAS 199A 11


Code Structure: setup function

setup function is executed


only once at the start

Arduino Programming: EAS 199A 12


Code Structure: loop function

loop function is
repeated indefinitely

Arduino Programming: EAS 199A 13


Code

pinMode(13, Output)
prepare pin 13 for
outputs of voltage

Digital I/O Functions:


pinMode
digitalWrite
digitalRead
Arduino Programming: EAS 199A 14
Code

digitalWrite(13, HIGH)
Sets pin 13 to voltage
that means “on”

Digital I/O Functions:


pinMode
digitalWrite
digitalRead
Arduino Programming: EAS 199A 15
Code

delay(1000);
tells microcontroller to do
nothing for 1000 ms = 1 s

Digital I/O Functions:


pinMode
digitalWrite
digitalRead
Arduino Programming: EAS 199A 16
Code

digitalWrite(13, LOW)
Sets pin 13 to voltage
that means “off”

Digital I/O Functions:


pinMode
digitalWrite
digitalRead
Arduino Programming: EAS 199A 17
Arduino Variable Types

int Integer values: 1, 2, 3, -4, 7234


float Values with non-zero fractional part, 7 digits
double Currently the same as a float. 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 18


Using variables and functions

Assigning values: Defines the variable name as red_LED_pin

int red_LED_pin = 5;
Defines the variable type as an integer
Uses the value stored in red_LED_pin

pinMode( red_LED_pin, OUTPUT );


calls the function named “pinMode”
HIGH and OUTPUT are pre-defined constants

digitalWrite( red_LED_pin, HIGH );


calls the function named “digitalWrite”

Arduino Programming: EAS 199A 19

You might also like