Arduino Programming
Jon Flanders
@jonflanders
What you will learn?
How to use the Arduino programming language.
The ins and outs of the Arduino IDE.
The Arduino programming language
Is a simplified version of C
Most of the “hard” parts of C are removed
You don’t need to be a hardcore C programmer to program Arduino
You will need to be if you decide to dive deeper
Basic syntax
Not surprisingly the language contains the basic syntax you’d expect
Comments
//single line
/* multi-line*/
Curly braces define scope of functions and control structures
{}
Each line of code must end with a semi-colon
#include to bring in additional header file for libraries (more on this
later)
#define to define a constant
replaced when found in code during pre-processing
Data types
Again, no surprises
void Basic data types you’d expect
in a modern C derived language
boolean (true/false)
char
unsigned char
byte
int
unsigned int
word
long
unsigned long
short
float
double
array
string (char array)
String
In addition to string (note - lowercase) the language includes a String
object (note - upper case S)
String is an object more like a Java or .NET String type
functions added to make it easier to use than an array of chars
things like concat, indexOf, toLowerCase etc.
char charString[] = “This is an ‘old’ style string”;
String strObj = “This is a ‘new’ style
string”;
String lower = strObj.toLowerCase();
Operators
Operators you’d expect (not an inclusive list)
equality (==)
assignment (=)
and (&&), or(||), not (!)
Does include reference and dereference operators
& and *
Generally only used in very advanced scenarios - unless you are building shields or
the like you won’t need to use
+-/%
Flow Control
In many ways loop is the main flow control construct in a sketch
Language contains the typical flow control constructs you’d expect
if, if..else, for, switch case, while, do...while, break, continue, return, goto
Also includes time functions
delay and delayMicroseconds to pause execution
millis and micros report time since last reset
The Arduino IDE
Environment based on the Processing (http://www.processing.org/)
environment
To ease transition between the two environments
Targeted specifically for Arduino
Programs are known as “sketches”
generally a single file with no extension
no notion of “project” or “workspace/solution” in the IDE
Runs on OSX, Windows, and Linux
Built using Java
IDE Preferences
Normal preferences dialog on each platform
You can set font size etc
These (plus more) preferences stored in text file
/Users/<USERNAME>/Library/Arduino/preferences.txt (Mac)
c:\Documents and Settings\<USERNAME>\Application Data\Arduino\preferences.txt
(Windows)
~/.arduino/preferences.txt (Linux)
Writing a sketch
Generally a sketch follows a typical pattern
#includes for any libraries you are using
variables and constants for global use
the setup function to initialize the board
the loop function to execute your main logic
Tabs
You can organize your code by using tabs
Click on the tab button in the IDE
down arrow on far right
Can add, rename, delete, and move between tabs
All tabs are files without extension
All these files are added together as part of your sketch automatically
Sketch “building” (or how a sketch becomes an
executable)
Arduino takes your sketch and pre-processes it
turns it from “Arduino C” into legal “C”
Compiles it using avr-gcc into an object file (binary)
Links it against the Arduino binary libraries
includes the appropriate “main”
Output is a single Intel hex file
Your object Hex
Sketch files File
Sketch uploading
Once the sketch has been compiled the IDE connects to the board
Serial connection whether USB or serial
uses avrdude to connect
The hex file is written to the flash memory of the chip
The Arduino bootloader loads the program and causes it to execute
Bootloader takes up some of the flash memory
For example the Uno has 32k - .5k is used by the bootloader
You can (advanced) remove the bootloader and using an external chip programmer
Sketch runs continuously until reset button is hit on the Arduino
Once reset - the bootloader loads the program again and it starts over again
Nice to have a “clear” (empty setup, empty loop) sketch around
Execution of a sketch
setup function called first
loop function called over and over
Sketches never stop execution
SetSS
Setup
eSet
1801801
Loop
80
Arduino constructs
To simplify programming against the board and common accessories
the language includes constants and functions for specific functionality
No need to write code directly against the hardware
Functions & Constants
pinMode - configures one of the digital pins for either input or output
pinMode(pin,mode)
Mode is one of three constants
INPUT - pin accepts input
OUTPUT - pin output
INPUT_PULLUP - pin accepts input, but the result is inverted from INPUT
digitalWrite will change the voltage of a pin set to OUTPUT mode
digitalRead reads the voltage of a pin set to INPUT or INPUT_PULLUP
HIGH and LOW constants are used to write or compare read values
HIGH == 5v
LOW == 0V
Other objects
Stream base class for reading and writing binary and character streams
not used directly
Serial object used to read and write messages to the board over the
serial (again usually serial over USB) connection
Can be used for diagnostics
Using libraries
A number of standard libraries come with the Arduino IDE
Ethernet,
GSM
Servo
WiFI
LiquidCrystal (LCD)
Often a shield vendor will ship a library with a shield
Using a library is as simple as adding the #include to the header file
#include <Ethernet.h>
You can create your own re-usable libraries as well
Building a library
You might want a library for common functionality you use in multiple
sketches
Steps
create header file with class definition
create a source file (C++) for that class
Make a directory with same name as header and source file, put that inside of the
libraries subdirectory
#include header file in your sketch
Summary
The Arduino language is a simple but powerful language for controlling
boards, shields, and devices
The IDE is a useful program for writing, organizing, and uploading your
sketches