Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Arduino Programming

Introduction to Arduino Programming

Part of my 2-hour workshop "Prototyping New Wearable Experiences with Soft Electronics and Arduinos" at Wearables DevCon 2014 (wearablesdevcon.com).

Pearl Chen

March 05, 2014
Tweet

More Decks by Pearl Chen

Other Decks in Technology

Transcript

  1. klab.ca Why use an Arduino? (Part 1) Analyze incoming data

    using computer logic Affect something physical (or virtual) Sense something happening in the physical world Scenario #1 The computer analyzes the weight of the pressure input to be larger than a sparrow’s weight. Squirrel?? A water hose is triggered and a spray of water is sent out in the direction of the intruder! A pressure sensor detects that something is trying to take seeds from a bird feeder. Examples The computer analyzes the levels to be lower than normal. The interface on a mobile phone app updates and warns the diabetic that they should eat an apple. A diabetic uses a glucose sensor to measure their blood-sugar level.
  2. klab.ca Why use an Arduino? (Part 2) Affect something physical

    Notification of something happening in the virtual world Scenario #2 The computer analyzes the sender to be your best friend. A LED sitting on your desk blinks urgently to get your attention. A Twitter @mention to your Twitter handle is detected. Examples Analyze incoming data using computer logic
  3. klab.ca Need more detail? Check out the Sparkfun buying guide:

    https://www.sparkfun.com/arduino_guide How to choose an Arduino Leonardo Mega LilyPad Chip ATmega32U4 ATmega2560 ATmega328 Speed 16MHz 16MHz 8MHz Memory 32Kb 256Kb 32Kb I/O Pins 20 54 9 or 14 Best used for general, all purpose larger projects wearables
  4. klab.ca Example code • There are lots of built-in code

    samples to get you started: File > Examples ➡Open File > Examples > 01.Basic > BareMinimum • In most cases, the only thing you’ll need to do to get the examples working is to find & replace the pin numbers in the code to the actual pins used in your circuit. ➡ github.com/pchen/ArduinoLilyPadProtoSnap Example code geared specifically for LilyPad ProtoSnap pins:
  5. klab.ca Functions A block of code, grouped with a name.

    • To define a function:
 
 void functionName() {
 
 } • To call/execute a function:
 
 functionName();
  6. klab.ca setup() and loop() 2 functions are required for every

    Arduino program: • setup()
 Runs on power up or when reset button is pressed. • loop()
 Executes forever, as long as there is power and no code errors
  7. klab.ca Verify and Upload • The 2 most used buttons

    on the Arduino IDE • Verify - make sure that there are no errors • Upload - compiles (checks for errors too) and writes program to microcontroller’s memory
  8. klab.ca Comments When you need to make a note to

    yourself or others. • Ignored by the Arduino.   • // single line comment • /*
 multi
 line
 comment
 */
  9. klab.ca Arduino code • Simplified version of Java. • .ino

    extension for Arduino version 1.0+ 
 .pde for older sketches • Called a “sketch” because it should feel like picking up a pencil and making a quick drawing.
 ➡Upload to board: 01.Basics > BlinkExternalLED
 (We’ll get to how this sketch is made soon...)
  10. klab.ca Arduino code walk-through ➡Open 01.Basics > TurnOnBuiltInLED • Goal:

    Light up one (then two) of the external LilyPad LEDs

  11. klab.ca Statements A single command. • Always end with a

    semicolon (;)
 (Much like all English sentences end with a period.)
 • Examples:
 delay(1000); // calling a function 
 int a = 1; //assigning a value to variable

  12. klab.ca Variables • Define a variable:
 datatype variableName; • Examples:


    boolean isSaturday = true;
 int myNumber = 1;
 float pi = 3.14;
 char firstInitial = ‘P’;
 char myName[] = “Pearl”;
 String myName = “Pearl Chen”; Common Data Types boolean true or false int whole number float decimal number char single character char[] or String group of characters A named container that holds a value for later use.
  13. klab.ca pinMode() and digitalWrite() for output (Part 1) • pinMode(

    pin#, OUTPUT );
 Set a pin to send outgoing commands • digitalWrite( pin#, HIGH );
 Output to a pin: HIGH / 5 volts / “on” / 1 • Goal: Light up one (then two) of the external LilyPad LEDs
 Solution: 01.Basics > TurnOnExternalLED
  14. klab.ca pinMode() and digitalWrite() for output (Part 2) • pinMode(

    pin#, OUTPUT );
 Set a pin to send outgoing commands • digitalWrite( pin#, LOW );
 Output to a pin: LOW / 0 volts / “off” / 0 • delay( milliseconds );
 Pause for a set number of milliseconds before executing next line of code. (Note: 1 sec = 1000 ms) • Goal: Try to make the built in LED blink. 
 Solution: 01.Basics > BlinkBuiltInLED
  15. klab.ca pinMode() and digitalRead() for input • pinMode( pin#, INPUT_PULLUP

    );
 Set a pin be used as a digital input pin • digitalRead( pin# );
 Listen for input values on a pin. Since it’s a digital signal, it will be either HIGH (1) or LOW (0) • (The input will "float" and randomly return either HIGH or LOW without a resistor wired to ground in a circuit. Arduino has a built-in “pull-up resistor” which can be activated using pinMode() as above. More info here.) ➡Open 02.Digital > ButtonBuiltInLED ➡Open 02.Digital > SwitchBuiltInLED
  16. klab.ca Conditionals (Examples) • From ButtonBuiltInLED:
 
 buttonState = digitalRead(buttonPin);


    
 if (buttonState == LOW) {
 // turn LED on
 }
 else {
 // turn LED off
 }
  17. klab.ca Conditionals • “if” test:
 if (condition test) {
 //statements

    if true
 } • “if/else” test:
 if (condition test) {
 //statements if true
 }else{
 //statements if false
 } Comparison Operators == equal to != not equal to < less than <= less than or 
 equal to > greater than >= greater than or 
 Logic branching -- allows for different outcomes depending on the condition being tested.
  18. klab.ca analogWrite() for analog outputs • Analog output pins: 3,

    5, 6, 9, 10, 11 • pinMode( pin#, OUTPUT );
 Setting pinMode() not necessary for analog -- but won’t hurt. • analogWrite( pin#, dutyCycle );
 Output to a pin a duty cycle between 0 (always off) and 255 (always on). • Goal: Light up external LilyPad LEDs on pin 5 and 6 at brightness steps between 0 and 255
 Solution: 03.Analog > DimExternalLEDs
  19. klab.ca analogRead() for analog input • pinMode( pin#, INPUT );


    Setting pinMode() not necessary for analog -- but won’t hurt. • analogRead( pin# );
 Value will be between 0 and the components limit. • Solution: 03.Analog > TempSensor • Also try: 03.Analog > ToneTheremin
  20. klab.ca Serial for debugging • in setup() use Serial.begin( baudRate

    ) • in loop() use Serial.print() or Serial.println() • To see the serial monitor console, go to Tools > Serial Monitor while your LilyPad is hooked up to your computer via USB. Make sure that the baud rate in the lower righthand corder is also the same as that in Serial.begin().
  21. klab.ca Coding Challenge Write an Arduino sketch that keeps “score”.

    • Light up an LED each time you click on a button. • When you reach 5 points: buzzzzzzzz! (You win.) • Bonus: Try to remove points when an opponent blocks your light sensor.
  22. klab.ca Thank you! ! Oh, and check out some upcoming

    Arduino workshops!  Sign up for mailing list on karma-laboratory.com.