8. Arduino
Board
Microcontroller ATmega328
Opera-ng
Voltage 5
V
Input
Voltage
(recommended) 7-‐12
V
Input
Voltage
(limits) 6-‐20
V
Digital
I/O
Pins 14
(of
which
6
provide
PWM
output)
Analog
Input
Pins 6
DC
Current
per
I/O
Pin 40
mA
DC
Current
for
3.3V
Pin 50
mA
32
KB
(ATmega328)
of
which
2
KB
used
by
Flash
Memory
bootloader
SRAM 2
KB
(ATmega328)
EEPROM 1
KB
(ATmega328)
Clock
Speed 16
MHz
9. Arduino
IDE
• IDE:
– Integrated
– Development
– Environment
12. Code
pinMode(13, Output)!
prepare
pin
13
for
outputs
of
voltage
Digital
I/O
Func`ons:
pinMode
digitalWrite
digitalRead
12
13. Code
digitalWrite(13, HIGH)!
Sets
pin
13
to
a
voltage
that
means
“on”
Digital
I/O
Func`ons:
pinMode
digitalWrite
digitalRead
13
14. Code
delay(1000);!
Tells
microcontroller
to
do
nothing
for
1000
ms
=
1
s
Digital
I/O
Func`ons:
pinMode
digitalWrite
digitalRead
14
15. Code
digitalWrite(13, LOW)!
Sets
pin
13
to
voltage
that
means
“off”
Digital
I/O
Func`ons:
pinMode
digitalWrite
digitalRead
15
16. Basic
Programming
• The
pinMode()
func`on
configures
a
pin
as
either
an
input
or
an
output.
To
use
it:
– You
pass
it
the
number
of
the
pin
to
configure
and
the
constant
INPUT
or
OUTPUT.
• pinMode(11,
INPUT);
• pinMode(13,
OUTPUT);
– When
configured
as
an
input,
a
pin
can
detect
the
state
of
a
sensor
like
a
pushbujon.
– As
an
output,
it
can
drive
an
actuator
like
an
LED.
17. Basic
Programming
• The
digitalWrite()
func`ons
outputs
a
value
on
a
pin
(0
–
19).
• Possible
values
are:
– LOW
(0
V)
– HIGH
(5
V)
• For
example:
– digitalWrite(13,
HIGH);
– digitalWrite(11,
LOW);
18. Basic
Programming
• The
digitalRead()
func`on
reads
a
value
on
a
pin
(0
–
19).
• Possible
values
are:
– LOW
(0
V)
– HIGH
(5
V)
• For
example:
– Int
x
=
digitalRead(13);
– Boolean
value
=
digitalRead(11);
19. Basic
Programming
• The
analogWrite()
func`ons
outputs
a
PWM
value
on
a
pin
(3,5,6,9,10,11).
• Possible
values
are:
– 0
(0
V)
– 127
(2,5
V)
– 255
(5
V)
• For
example:
– analogWrite(3,
0);
– analogWrite(11,
200);
20. Basic
Programming
• The
analogRead()
func`ons
reads
analog
value
on
a
pin
(A0
–
A5).
• Possible
values
are:
– 0
(0
V)
– 512
(2,5
V)
– 1024
(5
V)
• For
example:
– Int
value
=
analogRead(0);
– Int
valueSensor
=
analogRead(5);
21. Glossary of Arduino Programming
Terms
Variable
Types:
int
Pos (32767) or neg (-32768) - 2 Bytes
long
Pos (2,147,483,647) or neg (-2,147,483,648) - 4B
float
Floating point math (0,0000001) – 4B
char
Character values: a , b , D , 1 – 1B
boolean
True or false values – 1 bit
21
22. Loops
• Loops allow code to be repeated
– Repeated code goes in a block, surrounded by { }
– for loops
• need a counter
– while loops
need an escape
int • i; // declare counter!
!
for ( i=0; i<=12; i++ ) { // standard structure!
!
Serial.println(i); // send value of i to serial monitor!
!
}!
22
23. Loops
Ini`al
value
of
counter
i=0
only
on
first
pass
through
the
loop
Stopping
test:
Con`nue
while
this
condi`on
is
true
int i; // declare counter!
!
for ( i=0; i<=12; i++ ) { // standard structure!
!
Serial.println(i); // send value of i to serial monitor!
!
}!
Increment:
How
to
change
i
on
each
pass
through
the
loop
23
24. Loops
Common
loop:
increment
by
one
for ( i=0; i<=12; i++ ) { // increment by one!
... code block goes here!
}!
Common
loop:
increment
by
two
for ( i=0; i<=12; i+=2 ) { // increment by two!
... code block goes here!
}!
Decrement
by
one
for ( i=12; i>=0; i-- ) { // decrement by one!
... code block goes here!
}!
24
25. Input
Bujon
Pin
2
void setup()
{
pinMode(13, OUTPUT);
// declare LED as output
pinMode(2, INPUT);
// declare switch as input
}
void loop()
{
if (digitalRead(2) == HIGH)
// check if input is HIGH
{
digitalWrite(13, HIGH);
// turns the LED on
delay(1000);
// pause for 1 second
digitalWrite(13, LOW);
// turns the LED off
delay(1000);
// pause for 1 second
}
}
26. hjp://processing.org/
Now
connect
with
Processing...
hjp://io.workspace.howest.be/Workshop/MM.zip
Ref:
GeJng
Started
with
Processing
–
Casey
Reas
&
Ben
Fry
(O’REILLY
–
2010)
27. What
is
Processing
?
Create
images,
anima`ons
and
interac`ons
through
“sketching”
with
code
36. Principle
for
game
interface
Sensors
READ
Your
Sketch
[C]
inputs
UPLOAD
ARDUINO
ATmega328
COMMUNICATE
outputs
Actuators
WRITE
37. Principle
for
game
interface
Sensors
READ
Bujon_
communi-‐
ca`on.pde
inputs
UPLOAD
ARDUINO
ATmega328
COMMUNICATE
outputs
Nintendo
NES
Processing
(emulator)