Arduino Programming: Jon Flanders @jonflanders
Arduino Programming: Jon Flanders @jonflanders
Jon Flanders
@jonflanders
What you will learn?
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
+-/%
Flow Control
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
SetSS
Setup
eSet
1801801
Loop
80
Arduino constructs
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
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