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

Building Your Own Lightsaber

Building Your Own Lightsaber

Presented at CodeMash 2014

Pete Hodgson

January 03, 2014
Tweet

More Decks by Pete Hodgson

Other Decks in Programming

Transcript

  1. Building
    your own
    Lightsaber

    View full-size slide

  2. me me me
    Pete Hodgson
    Consultant at ThoughtWorks
    @ph1
    blog.thepete.net

    View full-size slide

  3. TSA
    Pro-Tip™

    View full-size slide

  4. Green Field
    http://www.flickr.com/photos/jillclardy/3213748255

    View full-size slide

  5. Continuous
    Integration

    View full-size slide

  6. http://www.flickr.com/photos/johnmueller/52621490/
    what if you have

    no build box?

    View full-size slide

  7. Raspberry Pi

    View full-size slide

  8. visual indicator
    aka
    build light

    View full-size slide

  9. Build

    Light

    View full-size slide

  10. Pretending
    to work

    View full-size slide

  11. raspberry pi
    &
    build light

    View full-size slide

  12. build light
    build my own
    lightsaber

    View full-size slide

  13. Learning
    by
    Doing

    View full-size slide

  14. Learning Doing

    View full-size slide

  15. Learning Doing
    Learning
    By Doing

    View full-size slide

  16. Choose a
    “right-sized“
    problem

    View full-size slide

  17. build light
    build my own
    lightsaber

    View full-size slide

  18. LEDs
    low voltage (can be powered via USB)
    bright
    flexible (blinky! colors!)

    View full-size slide

  19. all
    the colors

    View full-size slide

  20. The
    Drefus
    Model

    View full-size slide

  21. Beginner Expert

    View full-size slide

  22. Beginner
    detailed
    step-by-step
    instructions
    no wider
    context

    View full-size slide

  23. Beginner
    detailed
    step-by-step
    instructions
    no wider
    context
    Expert
    wider
    context
    (goal)
    no details
    (yet)

    View full-size slide

  24. Know where you
    are on the
    Drefus scale

    View full-size slide

  25. all
    the colors

    View full-size slide

  26. multi-color LEDs Search

    View full-size slide

  27. multi-color LEDs Search
    an LED can only be one color…
    but LEDs can come in many colors…
    solution: combine different colored LEDs

    View full-size slide

  28. RGB

    color mixing

    View full-size slide

  29. vary LED brightness Search

    View full-size slide

  30. vary LED brightness
    Pulse
    Width
    Modulation
    P
    W
    M
    Search

    View full-size slide

  31. Incandescent Bulb
    Voltage
    Brightness

    View full-size slide

  32. LED
    Voltage
    Brightness

    View full-size slide

  33. voltage
    100%
    0%
    time
    brightness: 100%

    View full-size slide

  34. voltage
    100%
    0%
    time

    View full-size slide

  35. 50%

    on
    50%

    off
    duty cycle
    voltage
    100%
    0%
    time

    View full-size slide

  36. 50%

    on
    50%

    off
    duty cycle
    brightness: 50%
    voltage
    100%
    0%
    time

    View full-size slide

  37. voltage
    100%
    0%
    time

    View full-size slide

  38. 25%
    on
    75%
    off
    voltage
    100%
    0%
    time

    View full-size slide

  39. brightness: 25%
    25%
    on
    75%
    off
    voltage
    100%
    0%
    time

    View full-size slide

  40. Don't
    trust your
    intuition
    '

    View full-size slide

  41. Red LED
    + Green LED
    + Blue LED
    + PWM
    = all the colors!

    View full-size slide

  42. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM

    View full-size slide

  43. Raspberry Pi has two
    GPIO pins which capable
    of PWM output.
    Our light needs three.

    View full-size slide

  44. Arduino is an Open-Source electronics
    prototyping platform based on flexible,
    easy-to-use hardware and software.
    - arduino.cc

    View full-size slide

  45. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM

    View full-size slide

  46. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM
    Arduino
    ???

    View full-size slide

  47. Feature-creep
    as a
    learning tool

    View full-size slide

  48. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM
    Arduino
    ???

    View full-size slide

  49. baby steps,
    towards an
    eventual goal.

    View full-size slide

  50. sketch 1
    blinking an LED
    (Hello World of hardware)

    View full-size slide

  51. IO
    pin
    ground
    pin

    View full-size slide

  52. pin “high”
    (+ve voltage)

    View full-size slide

  53. pin “low”
    (0 voltage)

    View full-size slide

  54. int LED_PIN = 6;!
    !
    void setup() { !
    pinMode(LED_PIN, OUTPUT); !
    }!
    !
    void loop() {!
    digitalWrite(LED_PIN, HIGH);!
    delay(1000); !
    digitalWrite(LED_PIN, LOW);!
    delay(1000); !
    }!

    View full-size slide

  55. sketch 2
    fading an LED (PWM)

    View full-size slide

  56. int LED_PIN = 6;!
    int MAX_BRIGHTNESS = 255;!
    int brightness = 0;!
    !
    void setup() { !
    pinMode(LED_PIN, OUTPUT); !
    }!
    !
    void loop() {!
    analogWrite(LED_PIN, brightness);!
    !
    brightness = brightness + 5;!
    if( brightness > MAX_BRIGHTNESS )!
    brightness = 0;!
    !
    delay(30);!
    }!

    View full-size slide

  57. int LED_PIN = 6;!
    int MAX_BRIGHTNESS = 255;!
    int brightness = 0;!
    !
    void setup() { !
    pinMode(LED_PIN, OUTPUT); !
    }!
    !
    void loop() {!
    analogWrite(LED_PIN, brightness);!
    !
    brightness = brightness + 5;!
    if( brightness > MAX_BRIGHTNESS )!
    brightness = 0;!
    !
    delay(30);!
    }!
    PWM

    View full-size slide

  58. sketch 3
    mixing colors

    View full-size slide

  59. int RED_PIN = 3;!
    int GREEN_PIN = 5;!
    int BLUE_PIN = 6;!
    int MAX_BRIGHTNESS = 255;!
    !
    void setup() { !
    pinMode(RED_PIN, OUTPUT );!
    pinMode(GREEN_PIN, OUTPUT );!
    pinMode(BLUE_PIN, OUTPUT );!
    }!
    !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!

    View full-size slide

  60. int RED_PIN = 3;!
    int GREEN_PIN = 5;!
    int BLUE_PIN = 6;!
    int MAX_BRIGHTNESS = 255;!
    !
    void setup() { !
    pinMode(RED_PIN, OUTPUT );!
    pinMode(GREEN_PIN, OUTPUT );!
    pinMode(BLUE_PIN, OUTPUT );!
    }!
    !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!

    View full-size slide

  61. int RED_PIN = 3;!
    int GREEN_PIN = 5;!
    int BLUE_PIN = 6;!
    int MAX_BRIGHTNESS = 255;!
    !
    void setup() { !
    pinMode(RED_PIN, OUTPUT );!
    pinMode(GREEN_PIN, OUTPUT );!
    pinMode(BLUE_PIN, OUTPUT );!
    }!
    !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!

    View full-size slide

  62. !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!
    colorComponents[1] = random(MAX_BRIGHTNESS);!
    colorComponents[2] = random(MAX_BRIGHTNESS);!
    }!
    !
    void displayColor(byte colorComponents[]){!
    analogWrite( RED_PIN, colorComponents[0] );!
    analogWrite( GREEN_PIN, colorComponents[1] );!
    analogWrite( BLUE_PIN, colorComponents[2] ); !
    }!

    View full-size slide

  63. !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!
    colorComponents[1] = random(MAX_BRIGHTNESS);!
    colorComponents[2] = random(MAX_BRIGHTNESS);!
    }!
    !
    void displayColor(byte colorComponents[]){!
    analogWrite( RED_PIN, colorComponents[0] );!
    analogWrite( GREEN_PIN, colorComponents[1] );!
    analogWrite( BLUE_PIN, colorComponents[2] ); !
    }!

    View full-size slide

  64. !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!
    colorComponents[1] = random(MAX_BRIGHTNESS);!
    colorComponents[2] = random(MAX_BRIGHTNESS);!
    }!
    !
    void displayColor(byte colorComponents[]){!
    analogWrite( RED_PIN, colorComponents[0] );!
    analogWrite( GREEN_PIN, colorComponents[1] );!
    analogWrite( BLUE_PIN, colorComponents[2] ); !
    }!

    View full-size slide

  65. !
    void loop() {!
    byte color[3];!
    assignRandomColorTo(color);!
    displayColor(color);!
    delay(1000); !
    }!
    !
    void assignRandomColorTo(byte colorComponents[]){!
    colorComponents[0] = random(MAX_BRIGHTNESS);!
    colorComponents[1] = random(MAX_BRIGHTNESS);!
    colorComponents[2] = random(MAX_BRIGHTNESS);!
    }!
    !
    void displayColor(byte colorComponents[]){!
    analogWrite( RED_PIN, colorComponents[0] );!
    analogWrite( GREEN_PIN, colorComponents[1] );!
    analogWrite( BLUE_PIN, colorComponents[2] ); !
    }!

    View full-size slide

  66. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM
    Arduino
    ???

    View full-size slide

  67. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM
    Arduino
    ???

    View full-size slide

  68. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM
    Arduino
    serial

    View full-size slide

  69. sketch 4
    an echo server (serial IO)

    View full-size slide

  70. void setup()!
    {!
    Serial.begin(57600); // start serial port at 57600 bps!
    }!
    !
    void loop() {!
    waitForInput();!
    !
    static char input[1025];!
    int bytesRead = Serial.readBytesUntil( '\n', input, 1024 );!
    !
    if( bytesRead ){!
    String str = String(input);!
    str.toUpperCase();!
    Serial.println(str);!
    }!
    }!
    !
    void waitForInput() {!
    while (Serial.available() <= 0) {!
    // busy loop!
    }!
    }!

    View full-size slide

  71. void setup()!
    {!
    Serial.begin(57600); // start serial port at 57600 bps!
    }!
    !
    void loop() {!
    waitForInput();!
    !
    static char input[1025];!
    int bytesRead = Serial.readBytesUntil( '\n', input, 1024 );!
    !
    if( bytesRead ){!
    String str = String(input);!
    str.toUpperCase();!
    Serial.println(str);!
    }!
    }!
    !
    void waitForInput() {!
    while (Serial.available() <= 0) {!
    // busy loop!
    }!
    }!

    View full-size slide

  72. void setup()!
    {!
    Serial.begin(57600); // start serial port at 57600 bps!
    }!
    !
    void loop() {!
    waitForInput();!
    !
    static char input[1025];!
    int bytesRead = Serial.readBytesUntil( '\n', input, 1024 );!
    !
    if( bytesRead ){!
    String str = String(input);!
    str.toUpperCase();!
    Serial.println(str);!
    }!
    }!
    !
    void waitForInput() {!
    while (Serial.available() <= 0) {!
    // busy loop!
    }!
    }!

    View full-size slide

  73. void setup()!
    {!
    Serial.begin(57600); // start serial port at 57600 bps!
    }!
    !
    void loop() {!
    waitForInput();!
    !
    static char input[1025];!
    int bytesRead = Serial.readBytesUntil( '\n', input, 1024 );!
    !
    if( bytesRead ){!
    String str = String(input);!
    str.toUpperCase();!
    Serial.println(str);!
    }!
    }!
    !
    void waitForInput() {!
    while (Serial.available() <= 0) {!
    // busy loop!
    }!
    }!

    View full-size slide

  74. void setup()!
    {!
    Serial.begin(57600); // start serial port at 57600 bps!
    }!
    !
    void loop() {!
    waitForInput();!
    !
    static char input[1025];!
    int bytesRead = Serial.readBytesUntil( '\n', input, 1024 );!
    !
    if( bytesRead ){!
    String str = String(input);!
    str.toUpperCase();!
    Serial.println(str);!
    }!
    }!
    !
    void waitForInput() {!
    while (Serial.available() <= 0) {!
    // busy loop!
    }!
    }!

    View full-size slide

  75. void setup()!
    {!
    Serial.begin(57600); // start serial port at 57600 bps!
    }!
    !
    void loop() {!
    waitForInput();!
    !
    static char input[1025];!
    int bytesRead = Serial.readBytesUntil( '\n', input, 1024 );!
    !
    if( bytesRead ){!
    String str = String(input);!
    str.toUpperCase();!
    Serial.println(str);!
    }!
    }!
    !
    void waitForInput() {!
    while (Serial.available() <= 0) {!
    // busy loop!
    }!
    }!

    View full-size slide

  76. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM
    Arduino
    serial

    View full-size slide

  77. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    Arduino

    View full-size slide

  78. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    Arduino
    “ffaa11\n”

    View full-size slide

  79. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    Arduino
    “ffaa11\n”

    View full-size slide

  80. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    Arduino
    “ffaa11\n”

    View full-size slide

  81. Much Learning
    Arduino-
    compatibles
    bareduinos
    LPC810
    soldering!
    transistors
    protoboard
    fritzing
    voltage
    regulators
    node.js
    cctray
    line-level
    converters
    command-line
    builds
    C++ on Arduino
    usb to serial
    external
    programmers
    OSS hardware

    View full-size slide

  82. moredip/aphex
    (work in progress)

    View full-size slide

  83. Pete Hodgson
    @ph1
    [email protected]
    these
    slides
    http://bit.ly/buildlightsaber

    View full-size slide