Arduino Programming Language PDF
Arduino Programming Language PDF
Allison M. Okamura
Stanford University
Programming Guidance
Potential resources:
Online courses (e.g., EdX, Udacity)
Web tutorials (Java or C programming languages are most appropriate)
Arduino-specific tutorials
In this class:
You will start from existing programs (sketches) and modify them
The complexity of the programming you will do is low
Debugging can be difficult because of the real-time nature of haptic
interaction
You should learn by doing. There is little you can do to damage your
Hapkit through programming mistakes!
Stanford University Introduction to Haptics Allison M. Okamura, 2014
We will start by going through some examples at
http://www.learn-c.org/
Arduino Programming Language Components
Structure Variables Functions
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Arduino Programming Language Components
Structure Variables Functions
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Structure: Basic Syntax
; Each statement ends in a semicolon. For example: int a = 13;
{} Curly braces always come in pairs; they are used to define the start
and end of functions, loops, and conditional statements. For example:
while (boolean expression)
{
statement(s)
}
#define Used to give a name to a constant value. For example: #define ledPin 3
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Structure: Arithmetic operators
= Assignment operator stores the value to the right of the equal sign in the
variable to the left of the equal sign: sensorVal = analogRead(FSRPin);
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Structure: Control structures
switch case Allows you to specify different code that should be executed
in various conditions. For example:
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
}
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Structure: Comparison Operators
The result of a statement with a comparison operator is
either TRUE (1) or FALSE (2)
x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
Tips:
Be careful not to accidentally use the assignment operator = instead of ==.
Cannot use statements such as 0 < x < 1; need to do each comparison separately
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Structure: Boolean Operators
&& Logical AND. True only if both operands are true, e.g.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) {
// do this only if both inputs are high
}
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Variables: Constants
HIGH When reading or writing to a digital pin, there are only two possible
LOW values a pin can take (or be set to): HIGH and LOW
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Variables: Data types
void Used in function delcarations to indicate that the function returns no
information. For example:
void setup() void loop()
{ {
// ... // ...
} }
boolean A boolean holds one of two values, true or false. For example:
boolean running = false;
if (running) {
// do something
}
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Variables: Data types
char A data type that stores a character value. For example:
char myChar = 'A';
char myChar = 65; // both are equivalent
Coding is in this ASCII chart: http://arduino.cc/en/Reference/ASCIIchart
The static keyword is used to create variables that are visible to only one function.
However unlike local variables that get created and destroyed every time a function
is called, static variables persist beyond the function call, preserving their data
between function calls. For example: static int a;
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Arduino Programming Language Components
Structure Variables Functions
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Functions: Digital I/O
For a description of the roles of different pins on the Arduino/Hapkit, see
http://arduino.cc/en/Tutorial/DigitalPins
pinMode(pin, mode) Configures the specified pin to behave either as
an input or an output. pin is the pin number.
Arduino reference materials obtained from http://arduino.cc under a Commons Attribution-ShareAlike 3.0 License.
Stanford University Introduction to Haptics Allison M. Okamura, 2014
Functions: Analog I/O
analogReference(type) The default reference voltage is 5V. This can be changed
to a different type and different resolution using this
function.
analogRead(pin) Reads the value from the specified analog pin and returns
a value between 0 and 1023 to represent a voltage
between 0 and 5 volts (for default). It takes about 0.0001
seconds to read an analog pin.