Programming arduino makeymakey

3,443 views
3,091 views

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,443
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
110
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Programming arduino makeymakey

  1. 1. Workshop  Makey  Makey   (Arduino)   Jelle.saldien@howest.be  
  2. 2. Arduino  
  3. 3. Principle   Sensors   READ   Your   Sketch   [C]   inputs   UPLOAD   ARDUINO   ATmega328  COMMUNICATE   outputs   Actuators   WRITE  
  4. 4. Examples  
  5. 5. Examples  
  6. 6. Examples  
  7. 7. Arduino  Board  
  8. 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 6DC  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. 9. Arduino  IDE  •  IDE:   –  Integrated   –  Development   –  Environment  
  10. 10. Code  Structure:  setup  func`on   setup  func`on  is  executed   only  once  at  the  start  10  
  11. 11. Code  Structure:  loop  func`on   loop  func`on  is   repeated  indefinitely  11  
  12. 12. Code   pinMode(13, Output)! prepare  pin  13  for   outputs  of  voltage   Digital  I/O  Func`ons:   pinMode   digitalWrite   digitalRead  12  
  13. 13. Code   digitalWrite(13, HIGH)! Sets  pin  13  to  a  voltage  that   means  “on”   Digital  I/O  Func`ons:    pinMode    digitalWrite    digitalRead  13  
  14. 14. Code   delay(1000);! Tells  microcontroller  to  do   nothing  for  1000  ms  =  1  s   Digital  I/O  Func`ons:    pinMode    digitalWrite    digitalRead  14  
  15. 15. Code   digitalWrite(13, LOW)! Sets  pin  13  to  voltage   that  means  “off”   Digital  I/O  Func`ons:    pinMode    digitalWrite    digitalRead  15  
  16. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 27. What  is  Processing  ?       Create  images,  anima`ons  and  interac`ons   through    “sketching” with  code  
  28. 28. What  is  Processing  ?    SoftwarePrototyping_
  29. 29. What  is  Processing  ?  
  30. 30. Family  Tree  
  31. 31. Assignment  for  today...   DESIGN  YOUR   ENTERTAINMENT  SYSTEM  
  32. 32. Example  running  zelda  
  33. 33. Example  Kirby’s  flying  adventure  
  34. 34. Example  threadmill  supermario  
  35. 35. Example  Smoking  IR-­‐Gun  
  36. 36. Principle  for  game  interface   Sensors   READ   Your   Sketch   [C]   inputs   UPLOAD   ARDUINO   ATmega328   COMMUNICATE   outputs   Actuators   WRITE  
  37. 37. Principle  for  game  interface   Sensors   READ   Bujon_   communi-­‐ ca`on.pde   inputs   UPLOAD   ARDUINO   ATmega328   COMMUNICATE   outputs   Nintendo  NES   Processing   (emulator)  
  38. 38. 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); }
  39. 39. Processing  Code  •  basic_example_vNESp5_arduino.pde  
  40. 40. Resources    •  hjp://www.arduino.cc/  •  hjp://processing.org/  •  hjp://mcanet.info/vNESp5/  •  hjp://io.workspace.howest.be/Workshop/MM.zip    

×