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 Slide

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

    View Slide

  3. TSA
    Pro-Tip™

    View Slide

  4. View Slide

  5. View Slide

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

    View Slide

  7. Continuous
    Integration

    View Slide

  8. Feedback

    View Slide

  9. View Slide

  10. Feedback

    View Slide

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

    no build box?

    View Slide

  12. View Slide

  13. Raspberry Pi

    View Slide

  14. View Slide

  15. Raspberry
    Pi

    View Slide

  16. visual indicator
    aka
    build light

    View Slide

  17. View Slide

  18. View Slide

  19. Build

    Light

    View Slide

  20. Pretending
    to work

    View Slide

  21. raspberry pi
    &
    build light

    View Slide

  22. done.

    View Slide

  23. done.
    ?

    View Slide

  24. build light
    build my own
    lightsaber

    View Slide

  25. Learning
    by
    Doing

    View Slide

  26. Learning Doing

    View Slide

  27. Learning Doing
    Learning
    By Doing

    View Slide

  28. Choose a
    “right-sized“
    problem

    View Slide

  29. build light
    build my own
    lightsaber

    View Slide

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

    View Slide

  31. all
    the colors

    View Slide

  32. The
    Drefus
    Model

    View Slide

  33. Beginner Expert

    View Slide

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

    View Slide

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

    View Slide

  36. Know where you
    are on the
    Drefus scale

    View Slide

  37. View Slide

  38. all
    the colors

    View Slide

  39. multi-color LEDs Search

    View Slide

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

    View Slide

  41. RGB

    color mixing

    View Slide

  42. vary LED brightness Search

    View Slide

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

    View Slide

  44. Incandescent Bulb
    Voltage
    Brightness

    View Slide

  45. LED
    Voltage
    Brightness

    View Slide

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

    View Slide

  47. voltage
    100%
    0%
    time

    View Slide

  48. 50%

    on
    50%

    off
    duty cycle
    voltage
    100%
    0%
    time

    View Slide

  49. 50%

    on
    50%

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

    View Slide

  50. voltage
    100%
    0%
    time

    View Slide

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

    View Slide

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

    View Slide

  53. Don't
    trust your
    intuition
    '

    View Slide

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

    View Slide

  55. View Slide

  56. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM

    View Slide

  57. GPIO
    Pins

    View Slide

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

    View Slide

  59. Arduino

    View Slide

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

    View Slide

  61. Arduino

    View Slide

  62. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    PWM

    View Slide

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

    View Slide

  64. Feature-creep
    as a
    learning tool

    View Slide

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

    View Slide

  66. View Slide

  67. baby steps,
    towards an
    eventual goal.

    View Slide

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

    View Slide

  69. View Slide

  70. IO
    pin
    ground
    pin

    View Slide

  71. View Slide

  72. pin “high”
    (+ve voltage)

    View Slide

  73. pin “low”
    (0 voltage)

    View Slide

  74. 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 Slide

  75. sketch 2
    fading an LED (PWM)

    View Slide

  76. View Slide

  77. 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 Slide

  78. 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 Slide

  79. sketch 3
    mixing colors

    View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. 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 Slide

  84. 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 Slide

  85. 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 Slide

  86. !
    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 Slide

  87. !
    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 Slide

  88. !
    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 Slide

  89. !
    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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  93. sketch 4
    an echo server (serial IO)

    View Slide

  94. 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 Slide

  95. 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 Slide

  96. 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 Slide

  97. 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 Slide

  98. 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 Slide

  99. 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 Slide

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

    View Slide

  101. GPIO
    Pins

    View Slide

  102. Raspberry
    PI
    red
    LED
    green
    LED
    blue
    LED
    Arduino

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  106. 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 Slide

  107. Resources

    View Slide

  108. Resources

    View Slide

  109. moredip/aphex
    (work in progress)

    View Slide

  110. Have FUN!

    View Slide

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

    View Slide