programming arduino makeymakey

40
Workshop Makey Makey (Arduino) [email protected]

Upload: industrial-design-center

Post on 13-May-2015

2.525 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Programming arduino makeymakey

Workshop  Makey  Makey  (Arduino)  

[email protected]  

Page 2: Programming arduino makeymakey

Arduino  

Page 3: Programming arduino makeymakey

Principle  

ARDUINO  

inputs  

outputs  

Sensors  

Actuators  

READ  

WRITE  

ATmega328  UPLOAD  

COMMUNICATE  

Your  Sketch  [C]  

Page 4: Programming arduino makeymakey

Examples  

Page 5: Programming arduino makeymakey

Examples  

Page 6: Programming arduino makeymakey

Examples  

Page 7: Programming arduino makeymakey

Arduino  Board  

Page 8: Programming arduino makeymakey

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

Page 9: Programming arduino makeymakey

Arduino  IDE  

•  IDE:  –  Integrated  – Development  – Environment  

Page 10: Programming arduino makeymakey

10  

Code  Structure:  setup  func`on  

setup  func`on  is  executed  only  once  at  the  start  

Page 11: Programming arduino makeymakey

11  

Code  Structure:  loop  func`on  

loop  func`on  is  repeated  indefinitely  

Page 12: Programming arduino makeymakey

12  

Code  

Digital  I/O  Func`ons:  pinMode  digitalWrite  digitalRead  

pinMode(13, Output)!prepare  pin  13  for  outputs  of  voltage  

Page 13: Programming arduino makeymakey

13  

Code  

digitalWrite(13, HIGH)!Sets  pin  13  to  a  voltage  that  means  “on”  

Digital  I/O  Func`ons:    pinMode    digitalWrite    digitalRead  

Page 14: Programming arduino makeymakey

14  

Code  

Digital  I/O  Func`ons:    pinMode    digitalWrite    digitalRead  

delay(1000);!Tells  microcontroller  to  do  nothing  for  1000  ms  =  1  s  

Page 15: Programming arduino makeymakey

15  

Code  

digitalWrite(13, LOW)!Sets  pin  13  to  voltage  that  means  “off”  

Digital  I/O  Func`ons:    pinMode    digitalWrite    digitalRead  

Page 16: Programming arduino makeymakey

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.    

Page 17: Programming arduino makeymakey

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);      

Page 18: Programming arduino makeymakey

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);      

Page 19: Programming arduino makeymakey

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);      

Page 20: Programming arduino makeymakey

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);      

Page 21: Programming arduino makeymakey

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

Page 22: Programming arduino makeymakey

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!!}!

Page 23: Programming arduino makeymakey

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  

Page 24: Programming arduino makeymakey

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  

Page 25: Programming arduino makeymakey

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  

Page 26: Programming arduino makeymakey

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/  

Page 27: Programming arduino makeymakey

What  is  Processing  ?  

   

Create  images,  anima`ons  and  interac`ons  through  

 “sketching” with  code  

Page 28: Programming arduino makeymakey

What  is  Processing  ?  

 Software Prototyping_

Page 29: Programming arduino makeymakey

What  is  Processing  ?  

Page 30: Programming arduino makeymakey

Family  Tree  

Page 31: Programming arduino makeymakey

Assignment  for  today...  

DESIGN  YOUR  ENTERTAINMENT  SYSTEM  

Page 32: Programming arduino makeymakey

Example  running  zelda  

Page 33: Programming arduino makeymakey

Example  Kirby’s  flying  adventure  

Page 34: Programming arduino makeymakey

Example  threadmill  supermario  

Page 35: Programming arduino makeymakey

Example  Smoking  IR-­‐Gun  

Page 36: Programming arduino makeymakey

Principle  for  game  interface  

ARDUINO  

inputs  

outputs  

Sensors  

Actuators  

READ  

WRITE  

ATmega328  UPLOAD  

COMMUNICATE  

Your  Sketch  [C]  

Page 37: Programming arduino makeymakey

Principle  for  game  interface  

ARDUINO  

inputs  

outputs  

Sensors  

READ  

ATmega328  UPLOAD  

COMMUNICATE  

Bujon_  communi-­‐ca`on.pde  

Processing   Nintendo  NES  (emulator)  

Page 38: Programming arduino makeymakey

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); }

Page 39: Programming arduino makeymakey

Processing  Code  

•  basic_example_vNESp5_arduino.pde  

Page 40: Programming arduino makeymakey

Resources    

•  hjp://www.arduino.cc/  •  hjp://processing.org/  •  hjp://mcanet.info/vNESp5/  •  hjp://io.workspace.howest.be/Workshop/MM.zip