programming arduino makeymakey
TRANSCRIPT
Workshop Makey Makey (Arduino)
Arduino
Principle
ARDUINO
inputs
outputs
Sensors
Actuators
READ
WRITE
ATmega328 UPLOAD
COMMUNICATE
Your Sketch [C]
Examples
Examples
Examples
Arduino Board
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
Flash Memory 32 KB (ATmega328) of which 2 KB used by bootloader
SRAM 2 KB (ATmega328) EEPROM 1 KB (ATmega328) Clock Speed 16 MHz
Arduino IDE
• IDE: – Integrated – Development – Environment
10
Code Structure: setup func`on
setup func`on is executed only once at the start
11
Code Structure: loop func`on
loop func`on is repeated indefinitely
12
Code
Digital I/O Func`ons: pinMode digitalWrite digitalRead
pinMode(13, Output)!prepare pin 13 for outputs of voltage
13
Code
digitalWrite(13, HIGH)!Sets pin 13 to a voltage that means “on”
Digital I/O Func`ons: pinMode digitalWrite digitalRead
14
Code
Digital I/O Func`ons: pinMode digitalWrite digitalRead
delay(1000);!Tells microcontroller to do nothing for 1000 ms = 1 s
15
Code
digitalWrite(13, LOW)!Sets pin 13 to voltage that means “off”
Digital I/O Func`ons: pinMode digitalWrite digitalRead
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.
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);
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);
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);
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
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!!}!
23
Loops
int i; // declare counter!!for ( i=0; i<=12; i++ ) { // standard structure!! Serial.println(i); // send value of i to serial monitor!!}!
Ini`al value of counter i=0 only on first pass through the loop
Stopping test: Con`nue while this condi`on is true
Increment: How to change i on each pass through the loop
24
Loops
for ( i=0; i<=12; i++ ) { // increment by one! ... code block goes here!}!
Common loop: increment by one
for ( i=0; i<=12; i+=2 ) { // increment by two! ... code block goes here!}!
Common loop: increment by two
for ( i=12; i>=0; i-- ) { // decrement by one! ... code block goes here!}!
Decrement by one
Input Bujon
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
} }
Pin 2
Now connect with Processing...
Ref: GeJng Started with Processing – Casey Reas & Ben Fry (O’REILLY – 2010)
hjp://io.workspace.howest.be/Workshop/MM.zip
hjp://processing.org/
What is Processing ?
Create images, anima`ons and interac`ons through
“sketching” with code
What is Processing ?
Software Prototyping_
What is Processing ?
Family Tree
Assignment for today...
DESIGN YOUR ENTERTAINMENT SYSTEM
Example running zelda
Example Kirby’s flying adventure
Example threadmill supermario
Example Smoking IR-‐Gun
Principle for game interface
ARDUINO
inputs
outputs
Sensors
Actuators
READ
WRITE
ATmega328 UPLOAD
COMMUNICATE
Your Sketch [C]
Principle for game interface
ARDUINO
inputs
outputs
Sensors
READ
ATmega328 UPLOAD
COMMUNICATE
Bujon_ communi-‐ca`on.pde
Processing Nintendo NES (emulator)
Bujon_communica`on.pde • hjp://www.arduino.cc/en/Tutorial/Bujon
void setup() { Serial.begin(9600);
pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT);
} void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) { Serial.print(‘LEFT', BYTE);
digitalWrite(ledPin, HIGH); } else {
Serial.print(‘RIGHT', BYTE); digitalWrite(ledPin, LOW); }
delay(15); }
Processing Code
• basic_example_vNESp5_arduino.pde
Resources
• hjp://www.arduino.cc/ • hjp://processing.org/ • hjp://mcanet.info/vNESp5/ • hjp://io.workspace.howest.be/Workshop/MM.zip