4.3 Arduino Language Reference
4.3 Arduino Language Reference
setup() is called once, then loop() is called repeatedly (until you reset your
board).
An Arduino program run in two parts:
void setup(): Is preparation, and loop() is execution.
In the setup section, always at the top of your program, you would set
pinModes, initialize serial communication, etc.
void loop(): Is the code to be executed -- reading inputs,
triggering outputs, etc.
Variable Declaration
Function Declaration
void
SETUP
void setup() {
port #
pinMode(9, OUTPUT);
Input or Output
}
http://www.arduino.cc/en/Reference/HomePage
LOOP
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
Turn the LED on
or off
} Wait for 1 second
or 1000 milliseconds
7
Basic Commands in Arduino IDE
delay( value ) : waits a noted amount of time before moving on to following
commands
value : a number denoting time in milliseconds (1000 milliseconds = 1
second)
digitalWrite( variable , setting ) : sets a component to on ( HIGH ) or off ( LOW )
variable : name of the component
setting : Either HIGH or LOW
digitalRead( variable ) : returns the value of a noted component, either HIGH or
LOW
variable : name of the component
Basic Commands in Arduino IDE
analogWrite( variable , value ) : sets a component to a certain value
from 0-255
variable : name of the component
value : A number 0-255
NOTE: can only be used when a component is plugged into a pin with
~symbol
analogRead( variable ) : returns the value of a noted component, a
number 0-255
variable : name of the component
NOTE: can only be used when a component is plugged into a pin with
~symbol
Variables & Functions
int val = 5;
assignment
Type “becomes”
value
variable name
11
USING VARIABLES
int delayTime = 2000;
int greenLED = 9;
void setup() {
Declare delayTime
pinMode(greenLED, OUTPUT); Variable
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
Use delayTime
digitalWrite(greenLED, LOW);
Variable
delay(delayTime);
}
12
Using Variables
int delayTime = 2000;
int greenLED = 9;
void setup() {
pinMode(greenLED, OUTPUT);
}
void loop() {
digitalWrite(greenLED, HIGH);
delay(delayTime);
digitalWrite(greenLED, LOW);
delayTime = delayTime - 100;
delay(delayTime);
} subtract 100 from
delayTime to gradually
increase LED’s blinking
speed
13
Variables & Functions
void function (){} : defines a function as the commands found
between the curly braces that will not return a value
function : name of the function, written in camelCase
To call the function, simply write the function name and
a set of parentheses, ie. myFunction();
Parameters can be included in the parentheses
Variables & Functions
int function (){} : defines a function as the commands found
between the curly braces that will return an integer
function : name of the function, written in camelCase
To call the function, simply write the function name and
a set of parentheses, ie. myFunction();
Parameters can be included in the parentheses
Control Structure
if
if...else
IF Condition
if(true)
{
asdfadsf
“perform some action”
}
Sensor Conditions to use in if/else statements
digitalRead( component ) : returns the digital value of a
component as 1 (on) or 0 (off)
component : name of the component to be checked
analogRead( component ) : returns the analog value of a
component
component : name of the component to be checked
IF Condition
if(true)
{
asdfadsf
“perform some action”
}
Control Structures
for
IF - ELSE Condition
&& (and)
|| (or)
! (not)
Pointer Access Operators
* dereference operator
& reference operator
Bitwise Operators
& (bitwise and)
| (bitwise or)
^ (bitwise xor)
~ (bitwise not)
<< (bitshift left)
>> (bitshift right)
Port Manipulation
Compound Operators
++ (increment)
-- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)
&= (compound bitwise and)
|= (compound bitwise or)
Variables and Constants
Variables are expressions that you can use in programs to
store values, such as a sensor reading from an analog pin.
Data Types
Variables can have various types, which are described below.
STRING
Arrays
ADC in Arduino
The Arduino Uno board contains 16 pins for ADC
analogWrite(2,128);
Quantanization the signal
Converting Analog Value to Digital
Variable Scope & Qualifiers
static
volatile
const
PROGMEM
Serial Communication
Used for communication between the Arduino board and a computer or
other devices.
This communication happens via the Arduino board's serial or USB
connection and on digital pins 0 (RX) and 1 (TX). Thus, if you use these
functions, you cannot also use pins 0 and 1 for digital i/o.
Serial.begin(speed)
int Serial.available()
int Serial.read()
Serial.flush()
Serial.print(data)
Serial.println(data)
Input & Output
• Transferring data from the computer to an Arduino is done using
Serial Transmission
• To setup Serial communication we use the following
void setup() {
Serial.begin(9600);
}
61 61
Writing to the Console
void setup() {
Serial.begin(9600);
Serial.println(“Hello World!”);
void loop() {}
62
Functions
Digital I/O
pinMode(pin, mode)
digitalWrite(pin, value)
int digitalRead(pin)
Analog I/O
analogReference(type)
int analogRead(pin)
analogWrite(pin, value) - PWM
Advanced I/O
shiftOut(dataPin, clockPin, bitOrder, value)
unsigned long pulseIn(pin, value)
Time
unsigned long millis()
delay(ms)
delayMicroseconds(us)
Math
min(x, y)
max(x, y)
abs(x)
constrain(x, a, b)
map(value, fromLow, fromHigh, toLow, toHigh)
pow(base, exponent)
sqrt(x)
Trigonometry
sin(rad)
cos(rad)
tan(rad)
Random Numbers
randomSeed(seed)
long random(max)
long random(min, max)
External Interrupts
attachInterrupt(interrupt, function, mode)
detachInterrupt(interrupt)
Interrupts
interrupts()
noInterrupts()
Utilities