Es Arduino 4
Es Arduino 4
Arduino Programming
Language
1
Arduino Programming Language
• Arduino – Tutorials
– http://arduino.cc/en/Tutorial/HomePage
• Arduino - Reference
– http://arduino.cc/en/Reference/HomePage
• Arduino – Libraries
– http://arduino.cc/en/Reference/Libraries
2
Binary and Hexadecimal Numbers - 1
Microcontrollers are fundamentally digital
(as opposed to ‘analog’) and use binary
logic
Two states: high and low, 1 or 0, on or off
Often 5V or 0V
One binary digit is called a bit
It can take on two possible states: 1 or 0
Four binary digits are called a nibble
Eight binary digits are called a byte
A word is a larger grouping: usually 32 bits
3
Binary and Hexadecimal Numbers - 2
Byte and bits
1 1 0 0 1 1 0 1
Bit No. 7 6 5 4 3 2 1 0
Upper nibble Lower nibble
(4 bits) (4 bits)
MSB LSB
(Most Significant Bit) (Least Significant Bit)
4
Binary and Hexadecimal Numbers - 3
Place Value
Bit No. 3 2 1 0
1 1 0 1
(Base 2 or binary number )
1 23 1 22 0 21 1 20
8 4 0 1 13 (Base 10)
7
Structure
void setup()
{
void loop()
{
// get information from sensors
// send commands to motors
8
}
void setup()
• The setup section is used for assigning input
and outputs (Examples: motors, LED’s,
sensors etc) to ports on the Arduino
• It also specifies whether the device is
OUTPUT or INPUT
• To do this we use the command “pinMode”
• Setup is called once, when the sketch starts.
9
SETUP
void setup()
{
pinMode(9, OUTPUT);
}
10
void loop()
loop( ) runs over and over, until power is lost or a
new sketch is loaded.
void loop()
{ Port # from setup
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(1000);
Turn the LED on
} or off
Wait for 1 second
or 1000 milliseconds 12
void loop()
13
Structure - Control Structures
if
if...else
for
switch case
while
do... while
14
“if” condition
“if”, which is used in conjunction with a comparison
operator, tests whether a certain condition has been reached, such
as an input being above a certain number. The format for an “if”
test is:
if (someVariable > 50)
comparison
{ operator
// do something here
}
The program tests to see if someVariable is greater than 50. If
it is, the program takes a particular action.
If the statement in parentheses is true, the statements inside the
brackets are run. If not, the program skips over the code.
Example:
if (x > 120) digitalWrite(LEDpin, HIGH);
15
“if-else”
The “if-else” is the primary means of conditional branching.
To branch an execution of your program depending on the state of a
digital input, we can use the following structure:
if (inputPin == HIGH)
{
doThingA;
}
else
{
doThingB;
}
16
“if-else”
17
“for” statement
The “for” statement is used to repeat a block of statements
enclosed in curly braces. An increment counter is usually used to
increment and terminate the loop. The “for” statement is useful
for any repetitive operation:
for (initialization; condition; increment)
{
//statement(s);
}
The “initialization” happens first and exactly once. Each
time through the loop, the “condition” is tested; if it's true, the
“statement” block, and the “increment” is executed, then the
“condition” is tested again. When the “condition” becomes
false, the loop ends.
18
“for” statement
Example :
“Dim LED using a PWM pin”:
void setup()
{
// no setup needed initialization
} condition
22
“do … while” loop
The “do” loop works in the same manner as the “while” loop,
with the exception that the condition is tested at the end of the
loop, so the “do” loop will always run at least once.
do
{
doSomething;
}
while (someVariable ?? value);
Example:
do
{
x = readSensor();
delay(10);
}
while (x < 100); // loops if x < 100
23
Structure - Further Syntax
; (semicolon)
{} (curly braces)
// (single line comment)
/* */ (multi-line comment)
#define
#include
24
Structure - Arithmetic Operators
= (assignment operator)
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo)
25
Example
/* Math */
int a = 5;
int b = 10;
int c = 20;
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.print("a = ");
Serial.println(a);
Serial.print("b = ");
Serial.println(b);
Serial.print("c = ");
Serial.println(c);
26
Structure - Compound Operators
++ (increment)
-- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)
&= (compound bitwise and)
|= (compound bitwise or)
27
Compound Operators
Increment or decrement a variable
X ++ // same as x = x + 1, or increments x by +1
X -- // same as x = x – 1, or decrements x by -1
X += y // same as x = x + y, or increments x by +y
X -= y // same as x = x - y, or decrements x by -y
X *= y // same as x = x * y, or multiplies x by y
X /= y // same as x = x / y, or divides x by y
Example:
x = 2; // x = 2
x += 4; // x now contains 6
x -= 3; // x now contains 3
x *= 10; // x now contains 30
x /= 2; // x now contains 15
28
Structure - Comparison Operators
== (equal to)
!= (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
29
Example
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)
30
Structure - Boolean Operators
|| (or)
&& (and)
! (not)
31
Boolean Operators - OR
If we want either of the conditions to be
true we need to use ‘OR’ logic (OR gate)
We use the symbols ||
Example:
32
Boolean Operators - AND
If we want all of the conditions to be true
we need to use ‘AND’ logic (AND gate)
We use the symbols &&
Example
33
Structure - Bitwise Operators
& (bitwise and)
| (bitwise or)
^ (bitwise xor)
~ (bitwise not)
<< (bitshift left)
>> (bitshift right)
34
Bitwise Operators
const int outPin = 7;
cost byte myByte = B00101010;
void setup(void) {
pinMode(outPin, OUTPUT);
}
void loop(void) {
for (int ii = 0; ii < 7; ii++) {
if (myByte & (B10000000 >> ii)) {
digitalWrite(outPin, HIGH);
}
else {
digitalWrite(outPin, LOW);
}
35
}
Format of variables
value
A variable is like “bucket”
It holds numbers or other values temporarily
All variables have to be declared before they are
used.
Declaring a variable means defining its type, and
optionally, setting an initial value (initializing the
variable).
int val = 5;
assignment value
Type variable name 36
“becomes”
37
Variable Types
Variable Types:
void setup()
{
}
void loop()
{
40
Declaring/ Assigning Variables
Boolean
boolean variableName;
Boolean variableName = true;
41
char
42
A data type that takes up 1 byte of memory that
stores a character value. Character literals are
written in single quotes, like this: 'A' (for multiple
characters - strings - use double quotes:
"ABC").
Example
char myChar = 'A';
char myChar = 65; // both are equivalent
ASCII Table
43
byte
44
Example
int var = val;
unsigned int
46
Example
unsigned int var = val;
long
47
50
pinMode()
51
HIGH (5 V)
For example:
digitalWrite(13, HIGH);
digitalWrite(11, LOW);
digitalRead()
53
HIGH (5 V)
digitalRead(10);
Functions - Analog I/O
Analog inputs will come to the Arduino as a
range of numbers, based upon the electrical
characteristics of the circuit.
– 0 to 1023
– .0049 V per digit (4.9 mV)
– Read time is 100 microseconds (10,000 a
second)
analogRead()
analogWrite() - PWM
54
analogWrite()
Writes an analog value (PWM wave) to a pin.
For example:
int sensorValue = analogRead(A0);
56
Example
PWM. LED fading
/* Fading
This example shows how to fade an LED using the analogWrite() function.
The circuit: LED attached from digital pin 9 to ground.
*/
void setup()
{
// nothing happens in setup
}
void loop()
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue++) // fade in from min to max in
increments
{
analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255)
delay(2); // wait for x milliseconds to see the dimming effect
}
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue--) // fade out from max to min in
increments
{
analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255)
delay(2); // wait for x milliseconds to see the dimming effect
}
}
57
Functions - Time
millis(): get the current time
delay(ms): Pauses for a few milliseconds
delayMicroseconds(us): Pauses for a few
microseconds
58
Functions - External Interrupts
attachInterrupt()
detachInterrupt()
59
attachInterrupt()
On a standard Arduino board, two pins
can be used as interrupts: pins 2 and 3.
The interrupt is enabled through the
following line:
attachInterrupt(interrupt, function, mode)
attachInterrupt(0, doEncoder, FALLING);
60
attachInterrupt()
Interrupt: the number of the interrupt, 0 or 1,
corresponding to Arduino pins # 2 and 3
respectively
61
mode
62
Interrupt example
int led = 7;
int state = LOW;
void setup()
{
pinMode(led, OUTPUT);
attachInterrupt(1, blink, CHANGE);
}
void loop()
{
digitalWrite(led, state);
}
void blink()
{
state = !state;
}
63
Functions - Communication
• Serial
• Stream
64