38
klab.ca/lilypadintro Google+: klab.ca/+ Twitter: @pearlchen (for absolute beginners) klab.ca/lilypadintro Introduction to the Arduino LilyPad Pearl Chen [email protected] Monday, 20 August, 12

Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen [email protected]

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Google+: klab.ca/+Twitter: @pearlchen

(for absolute beginners)klab.ca/lilypadintro

Introduction to theArduino LilyPad

Pearl [email protected]

Monday, 20 August, 12

Page 2: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Today’s Agenda

talkcode sew

Monday, 20 August, 12

Page 3: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Arduino

Monday, 20 August, 12

Page 4: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Read about all the Arduino boards: http://arduino.cc/en/Main/Boards

What is Arduino?It’s hardware...

Monday, 20 August, 12

Page 5: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

The Arduino IDE. Download the latest here: http://arduino.cc/en/Main/Software

What is Arduino?And it’s software!

Monday, 20 August, 12

Page 6: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Why use an Arduino? (Part 1)

Analyzeincoming data

using computer logic

Affectsomething physical

(or virtual)

Sensesomething happeningin the physical world

Scenario #1

The computer analyzesthe weight of the pressure

input to be larger than a sparrow’s weight.

Squirrel??

A water hose is triggeredand a spray of water

is sent out in the directionof the intruder!

A pressure sensordetects that somethingis trying to take seeds

from a bird feeder.

Examples

The computer analyzesthe levels to be

lower than normal.

The interface on amobile phone app

updates and warnsthe diabetic that

they should eat an apple.

A diabetic uses aglucose sensorto measure their

blood-sugar level.

Monday, 20 August, 12

Page 7: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Why use an Arduino? (Part 2)

Affectsomething physical

Notificationof something happening

in the virtual world

Scenario #2

The computer analyzesthe sender to be your

best friend.

A LED sitting on your desk blinks urgently

to get your attention.

A Twitter @mentionto your Twitter handle

is detected.

Examples

Analyzeincoming data

using computer logic

Monday, 20 August, 12

Page 8: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Need more detail? Check out the Sparkfun buying guide: https://www.sparkfun.com/arduino_guide

How to choose an Arduino

Leonardo Mega LilyPad

Chip

Speed

Memory

I/O Pins

Best used for

ATmega32U4 ATmega2560 ATmega328

16MHz 16MHz 8MHz

32Kb 256Kb 32Kb

20 54 9 or 14

general, all purpose larger projects wearables

Monday, 20 August, 12

Page 10: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Let’s get coding!

Monday, 20 August, 12

Page 12: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

LilyPad ProtoSnap I/O pins

Pin Component

3 Vibration Motor

9-11 RGB LED

A5 Button

2 Slide Switch

5, 6, A2, A3, A4 White LEDs

7 / 12 Buzzer +/-

A6 Light Sensor

A1 Temp Sensor

Monday, 20 August, 12

Page 13: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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:

Monday, 20 August, 12

Page 14: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Functions

A block of code, grouped with a name.

• To define a function:

void functionName() {

}

• To call/execute a function:

functionName();

Monday, 20 August, 12

Page 15: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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

Monday, 20 August, 12

Page 16: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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

Monday, 20 August, 12

Page 17: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Comments

When you need to make a note to yourself or others.

• Ignored by the Arduino.

• // single line comment

• /*multilinecomment*/

Monday, 20 August, 12

Page 18: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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 it made soon...)

Monday, 20 August, 12

Page 19: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Arduino code walk-through

➡Open 01.Basics > TurnOnBuiltInLED

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

Monday, 20 August, 12

Page 20: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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

Monday, 20 August, 12

Page 21: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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.

Monday, 20 August, 12

Page 22: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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 LEDsSolution: 01.Basics > TurnOnExternalLED

Monday, 20 August, 12

Page 23: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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

Monday, 20 August, 12

Page 24: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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

Monday, 20 August, 12

Page 25: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Conditionals (Examples)

• From ButtonBuiltInLED:

buttonState = digitalRead(buttonPin);

if (buttonState == LOW) { // turn LED on}else { // turn LED off}

Monday, 20 August, 12

Page 26: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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 equal to

Logic branching -- allows for different outcomes depending on the condition being tested.

Monday, 20 August, 12

Page 27: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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 255Solution: 03.Analog > DimExternalLEDs

Monday, 20 August, 12

Page 28: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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

Monday, 20 August, 12

Page 29: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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().

Monday, 20 August, 12

Page 30: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

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.

Monday, 20 August, 12

Page 31: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Let’s get sewing!

Monday, 20 August, 12

Page 32: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

French Knot how-to

Because conductive thread tends to fray, making a regular knot on the thread end doesn’t always stick.

Instead, use something called a “french knot” (or similar: a “colonial knot”) to the underside of your fabric.

➡ http://www.sublimestitching.com/pages/how-to-french-knot

Monday, 20 August, 12

Page 33: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Additional Resources

Monday, 20 August, 12

Page 34: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Groups & Organizations in Toronto

• Toronto wearables meetup (http://blog.ocad.ca/wordpress/socialbodylab/toronto-wearables-meetup/)

• OCAD’s Social Body Lab (http://blog.ocad.ca/wordpress/socialbodylab/)

• Internet of Things Toronto (#IOToronto)(http://www.meetup.com/IOToronto/)

• InterAccess(http://www.interaccess.org/)

• Site3 coLaboratory(http://site3.ca/)

• HackLab.TO(http://hacklab.to/)

Monday, 20 August, 12

Page 35: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Books

• Fashionable Technology by Sabine Seymour

• Design of the Elastic Mind exhibition catalogue microsite (or book) from MOMA show in 2008

• Programming Interactivity (2nd Edition) by Joshua Noble

• Making Things Talk (2nd Edition) by Tom Igoe

Monday, 20 August, 12

Page 36: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Electronics shopping

• Electronics store listings for Toronto (compiled by me)

Monday, 20 August, 12

Page 37: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Other websites

• High-Low Tech

• Leah Buechley’s LilyPad page

Monday, 20 August, 12

Page 38: Introduction to the Arduino LilyPad - Karma Laboratorykarma-laboratory.com/workshops/IntroToArduinoLilyPad.pdf · 2012-08-20 · Introduction to the Arduino LilyPad Pearl Chen pearl@karma-laboratory.com

klab.ca/lilypadintro

Thank you!Oh, and check out some upcoming Arduino workshops!

Sign up for mailing list on karma-laboratory.com.

Monday, 20 August, 12