thursday week 2: digital input and output theory and...

37
Thursday Week 2: Digital Input and Output 1 Theory and Practice of Tangible User Interfaces Digital Input and Output RGB LEDs fade with PWM week 02

Upload: phamcong

Post on 22-Apr-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

1

Theory and Practice of Tangible User Interfaces

Digital Input and OutputRGB LEDs

fade with PWM

week 02

Page 2: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

2

Theory and Practice of Tangible User Interfaces

Microcontrollers

Output Transducersactuators (e.g., motors, buzzers)

Input Transducerssensors (e.g., switches, levers, sliders, etc.)

Arduino

Illustration adapted and modified from O’Sullivan and Igoe

Page 3: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

3

Theory and Practice of Tangible User Interfaces

Digital vs.

AnalogBinary vs. continuous signals

Binary / Digital = “whether or not”•

Continuous / Analog signal = “how much”

or “faster,”

“brighter,”

etc.

Page 4: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

4

Theory and Practice of Tangible User Interfaces

Digital vs. AnalogInternally, all microprocessors

compute

binary: 0 or 1 (0V or 5V)

In general, most microprocessors output only binary (0V or 5V)

Specifically, Arduino output pins can

only be LOW

(0V) or

HIGH (5V)

Page 5: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

5

Theory and Practice of Tangible User Interfaces

Digital vs. AnalogTwo states

(binary signal) vs.

multiple states

(continuous signal)

Digital Input and Output

Analog Input (next week!)

Page 6: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

6

Theory and Practice of Tangible User Interfaces

Digital OutputBlinking LED

Page 7: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

7

Theory and Practice of Tangible User Interfaces

Can We Do Analog Out?LED with 23% brightness?

100% brightness 23% brightness

Page 8: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

8

Theory and Practice of Tangible User Interfaces

Pulse Width Modulation

(PWM)

Most microprocessors can

only output

binary: LOW (0V)

or HIGH (5V)

So you fake it with PWM, Pulse Width Modulation

It gives you an illusion of analog values, in between LOW and HIGH

Page 9: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

9

Theory and Practice of Tangible User Interfaces

PWM

Page 10: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

10

Theory and Practice of Tangible User Interfaces

PWM

75% brightness

25% brightness

50% brightness

Page 11: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

11

Theory and Practice of Tangible User Interfaces

Pulse Width Modulation

(PWM)

Your Arduino board has built in PWM circuits, on pins 3, 5, 6, 9, 10, and 11

Page 12: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

12

Theory and Practice of Tangible User Interfaces

analogWrite(pin, value)The duty cycle: between 0 and 255

analogWrite(11, 191)

analogWrite(11, 127)

analogWrite(11, 64)

75% brightness

50% brightness

25% brightness

Page 13: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

13

Theory and Practice of Tangible User Interfaces

In Class ExerciseMake a color mixer with RGB LEDs

Exercise with digital input and output, and PWM

Page 14: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

14

Theory and Practice of Tangible User Interfaces

Color Mixer with RGB LEDsMake any colors with Red, Green, and Blue LEDs, except black

Page 15: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

15

Theory and Practice of Tangible User Interfaces

1. LED Blink 2. LED Fade 3. Circuit with 3 LEDs

4. RGB LED Fade 5. Serial RGB LED

In Class Exercise

Page 16: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

16

Theory and Practice of Tangible User Interfaces

Blinking LED (c.f. homework)

Resistorred, red, brown, gold

LED

Page 17: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

17

Theory and Practice of Tangible User Interfaces

1. LED Blink 2. LED Fade 3. Circuit with 3 LEDs

4. RGB LED Fade 5. Serial RGB LED

In Class Exercise

Page 18: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

18

Theory and Practice of Tangible User Interfaces

LED Fade

Resistorred, red, brown, gold

LED

Page 19: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

19

Theory and Practice of Tangible User Interfaces

1. LED Blink 2. LED Fade 3. Circuit with 3 LEDs

4. RGB LED Fade 5. Serial RGB LED

In Class Exercise

Page 20: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

20

Theory and Practice of Tangible User Interfaces

Circuit with 3 LEDsPlug three LEDs, red, green, and blue and make different colors

Page 21: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

21

Theory and Practice of Tangible User Interfaces

1. LED Blink 2. LED Fade 3. Circuit with 3 LEDs

4. RGB LED Fade 5. Serial RGB LED

In Class Exercise

Page 22: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

22

Theory and Practice of Tangible User Interfaces

RGB LED FadeSlow color fading and mixing

Page 23: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

23

Theory and Practice of Tangible User Interfaces

DiffuserTake a few packing peanuts to experiment

Page 24: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

24

Theory and Practice of Tangible User Interfaces

1. LED Blink 2. LED Fade 3. Circuit with 3 LEDs

4. RGB LED Fade 5. Serial RGB LED

In Class Exercise

Page 25: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

25

Theory and Practice of Tangible User Interfaces

Serial RGB

Tell it to mix 50 red, 100 green, and 20 blue... Etc.

g50

Page 26: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

26

Theory and Practice of Tangible User Interfaces

Arduino Board•

USB to serial

Page 27: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

27

Theory and Practice of Tangible User Interfaces

Serial Monitor

Page 28: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

28

Theory and Practice of Tangible User Interfaces

Serial CommunicationSerial.begin()

e.g., Serial.begin(9600)

Serial.print()

e.g., Serial.print(colorVal)

Serial.read()

Page 29: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

29

Theory and Practice of Tangible User Interfaces

Serial RGB

Tell it to mix 50 red, 100 green, and 20 blue... etc.

g50

Page 30: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

30

Theory and Practice of Tangible User Interfaces

Page 31: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

31

Theory and Practice of Tangible User Interfaces

Page 32: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

32

Theory and Practice of Tangible User Interfaces

Page 33: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

33

Theory and Practice of Tangible User Interfaces

Page 34: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

34

Theory and Practice of Tangible User Interfaces

1. LED Blink 2. LED Fade 3. Circuit with 3 LEDs

4. RGB LED Fade 5. Serial RGB LED

In Class Exercise

Page 35: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

35

Theory and Practice of Tangible User Interfaces

Homework (due next Thursday, Sep 13)

Part I: Design a good diffuser for your RGB LEDs

e.g., ping pong ball, styrofoam, mylar, cottons, icecream

cone?

Part II: Modify the Serial RGB code for new keyboard input:

Basic: Control the RGB values with multiple key presses (e.g., instead of typing “r127”

to set the Red LED to 50% brightness, count how many times the characters “r”

“g”

“b”

were pressed. E.g., press “r”

5 times to get it to 50%, 8 times for 80%, 10 times for 100%, 11 times to go back to 0%, etc.)

Advanced: Find new ways of controlling the colors of LEDs

using the keyboard

Post both parts on the course website (photo, descriptions, code)

Page 36: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

36

Theory and Practice of Tangible User Interfaces

Supplement ReadingsMore on PWM and LED fading: Chapter 6 (p. 112-114) of O’Sullivan and Igoe

More on microcontroller in general: Chapter 4 (p.49-63)

Page 37: Thursday Week 2: Digital Input and Output Theory and …courses.ischool.berkeley.edu/i290-13/f07/system/files/tangible_wk2... · Thursday Week 2: Digital Input and Output 1 Theory

Thursday Week 2: Digital Input and Output

37

Theory and Practice of Tangible User Interfaces

Next Thursday: Analog InputTwo states

(binary signal) vs.

multiple states

(continuous signal)

Digital Input and Output

Analog Input (next week!)