threadtech2: intro to programming leds with a lilypad arduino

38
threadtech 2 where fashion meets technology The Edge | Colleen Morgan | Digital Visual Arts Catalyst

Upload: colleen-morgan

Post on 03-Dec-2014

3.450 views

Category:

Self Improvement


8 download

DESCRIPTION

These are the slides for a ThreadTech workshop that was held at The Edge, Brisbane. The workshop introduced participants to programming LEDs with a Lilypad Arduino. Participants learn how to blink LEDs, create an LEDs sequence and to fade LEDs in and out.

TRANSCRIPT

Page 1: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

threadtech 2where fashion meets technology

The Edge | Colleen Morgan | Digital Visual Arts Catalyst

Page 2: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

TimelineTake a look at some examples

Introduction to workshop materials

Lilypad & Arduino - Setup

Learn how to Blink

Create a Light Wave

Learn how to Fade

Design circuit

What next, other fun, useful links

Page 5: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Workshop Materials

conductive threadlilypad light emitting diodes

(LEDs)

Lilypad Pro Kit

Jumper Leads

Page 6: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Lilypad

An Arduino Micro Processor designed to be sewn.

Designed by Leah Buechley from MIT and SparkFun Electronics.

Can be programmed with the Arduino Software (Opensorce)

Is Washable!! (by hand)

Page 7: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Lilypad

positive power supply(anode)

negative power supply

(cathode)

ResetButton

Digital I/O Pins1 - 13

Analog Input Pins

6 Pins For The FTDI Basic

Breakout Board

Page 8: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Arduino Software

Arduino is an opensource software

Free to download

Based on the C programming language and Processing Software

Runs on Windows, Mac OS and Linux

http://arduino.cc

Page 9: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Arduino Setup

1. Select Board: Lilypad Arduino w/ ATMega 328

2. Select Serial Port:

Mac will look something like this: /dev/tty.usbserial....

PC will look something like this: com1

http://arduino.cc

Tools Menu

Page 10: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Arduino Software

Text EditorFor Writing

Code

Toolbar

Debugging Text

Page 11: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Arduino ToolbarVerify/CompileChecks Code for Errors

Stop Open Sketch Upload

Serial MonitorSave SketchNew Sketch

Page 12: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Sketch Setup

sample code from arduino library

Page 13: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Blink LED

sample code from arduino library

/*Multi-Line Comment*/

// single line comment

void setup:run once when sketch starts

Statement

void loop:run over and over

again

Page 14: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Statements:

A statement is basically a computer sentence. Instead of finishing with a . all computer sentences end with a ; (semicolon).

Lets have a look at the first statement we have in our blink code:

‘pinMode’ tells the computer to get ready to activate a pin on the arduino board.

‘(13, OUTPUT)’ tells the computer that the pin we are activating is pin 13 and that we want that pin to act as an output. We are using pin 13 because all Arduino boards have a built in LED attached to pin 13.

‘;’the semicolon tells the computer that that is the end of the statement.

Blink LED

Page 15: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Procedures:

A procedure is a collection of statements. It is used to group statements together so that we can refer to them all with one name.

Lets look at the first procedure we have in our code:

All procedures begin with a return value. We do not require a return value at the moment so we begin with ‘void’.

The next part of the procedure is the name of the procedure. This one is called ‘setup’.

We then have a set of ‘()’brackets. These brackets are for the input values of the procedure. Again we do not have any so we leave them empty.

All statements within a procedure are enclosed between a set of {} Curly Brackets.

Blink LED

Page 16: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Procedures:

Every Arduino sketch has two special procedures that have to be there even if they end up remaining empty.

The first procedure is the setup procedure. The setup procedure wakes up the Arduino board and only runs once.

The second procedure is the loop procedure. The loop procedure runs the statements within it over and over again forever (or until it runs out of power or is switched off).

Blink LED

Page 17: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Loop Procedure:

Now lets have a look at the loop procedure in our code.

The first statement we see is ‘digitalWrite(13, HIGH);’Lets break it down. ‘digitalWrite’ tells the computer to get ready to write a digital output. Then the stuff in the brackets tells the computer what to write and to what pin.So we can see that we want the computer to write something to LED attached to the digital pin ’13’. And we want it to write it as ‘HIGH’.

On/Off states of LEDs are determined by HIGH/LOW commands in Arduino.

The next statement is ‘delay(1000);’The delay statement tells the computer that we want it to stop what it is doing for a certain amount of time. Time in arduino land is written as milliseconds. So here we want the computer to stop what it is doing ‘delay’for ‘(1000)’, 1000 milliseconds or one second.

Blink LED

Page 18: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Loop Procedure:

Using your detective skills I am sure you can figure out what the next two statements are telling the computer to do.

Now lets try mixing it up a little:

Can you make the LED light up for 5 seconds?

One LED is a little boring, can we have two or three ... or even five??

What do you think is the first step to add another LED? Hint: we will have to write some code in the setup procedure!

Now we will play around with our blinking LEDs. Can you get all of them to blink at once, or one after the other?

Blink LED

Page 19: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Hardware Set Up:

Blink LEDPositive

Negative LED attached to:

PIN 13

Page 20: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Hardware Set Up:

Blink LEDPositive

Negative

LED attached to:

PIN 13 PIN12

Page 21: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Hardware Set Up:

Blink LEDPositive

Negative

LED attached to:

PIN 13 PIN12PIN 7

Page 22: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

There is another way to get your LEDs to blink one after another with some simple code shortcuts. Lets have a look:

The first line of code is a statement. But it is a particular type of statement. A statement that tells the computer, ok this is a variable that we are going to use throughout our procedures. If we define our variables right at the start then we do not have to write them over and over again in our procedures later on, we can just refer to them by the names we give them.

LED Sequence

Page 23: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Ok so lets break down this variable statement. Firstly ‘int’ tells the computer that the type of variable we are defining is an integer or a whole number. The next part of code is the name of the variable. This one is called ‘timer. We are using these statements to set some a timer to use when our lights blink. We are setting the timer to ‘50’milliseconds.

Now lets check our the setup procedure.

LED Sequence

Page 24: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

This time we are getting a little tricker. We are going to introduce a ‘for’ statement. The general form of a ‘for’ statement is:

for(initialisation, condition, increment)statement;

We are going to use this for statement to tell the computer to set pins 5, 6, 7, 8 and 9 as outputs.

LED Sequence

Page 25: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Lets look at this for statement a little differently:

We initialise the statement by saying that we want the computer to default by beginning at pin 5 (int thisPin = 5).

We then set a condition for the statement. This condition says that as long as the pin number is less than 10 (thisPin <10) we want the computer to move along to the next pin in order (thisPin++).

Now we are ready to have a look at the statement that is assigned to this for statement:

This statement tells the computer that each of the pins should be set as outputs.

LED SequenceStatement Type (initialisation condition increment)

for (int thisPin = 5; thisPin < 10; thisPin++)

Page 26: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Now for the loop procedure:

You can see that we have the for statement again. But this time we have a couple of different statements assigned to it. The first statement tells the computer to turn on each of the LEDs it comes across as it goes between pin 5 and 9.

The next statement calls a delay. Remember the timer we set at the beginning of this sketch. All we need to do now is write the name of that statement and the computer will know what time we want it to delay for. Remember it was 50 milliseconds.

The third statement then tells the computer to turn each of the LEDs as it goes between pin 5 and 9.

Now lets see if we can loop from the highest pin to the lowest instead.We can also play around by setting new timers too.

LED Sequence

Page 27: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fade LED

OK Enough of this blinking business.

Now it is time to get a little subtler. Lets get our LEDs to FADE

in and out.

Page 28: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fading:

Lets have a little look at a piece of code for fading LEDs.

The first line of code is a comment as indicated by the //. This comment is just telling us what this sketch is all about.

Ok so lets break down the next line which is a variable statement. Firstly ‘int’ tells the computer that the type of variable we are defining is an integer or a whole number. The next part of code is the name of the variable. This one is called ‘value’. And as a default we want the value to be 0 that is what the ‘= 0’ bit is all about. And as usual the statement is finished with a ; semicolon.

Fade LED

Page 29: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fading:

Now lets check out the setup procedure:

With your new-found coding knowledge you should be able to decipher what is going on here.

That is right. We are telling the computer that we are going to be using 4 LEDs attached to pins 5, 6, 7 and 8. And that these LEDs are going to be receiving data so they are outputs not inputs.

Fade LED

Page 30: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fading:

Now lets check out the loop procedure:

It starts to get a little tricker now. Again are going to introduce a ‘for’ statement. The general form of a ‘for’ statement is:

for(initialisation, condition, increment)statement;

We are going to use our for statement to tell the computer to fade in some LEDs.

Fade LED

Page 31: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fading:

Lets have a look at that ‘for’ statement:

So we initialise the loop by saying that we want the value to start at 0 (value = 0;).

We then set the condition of the statement saying that as long as the value is equal to our less then 255 (<= 255;) we want the value to continue to increase by the increment of 5 (value+=5).

So what does all of this value business mean and what is the significance of 255.

LEDs can be faded by using something called “pulse width modulation’ or PWM. An LED has a PWM range from 0 to 255. When it is at 0 the LED is off. When it is at 255 the LED is at its brightest.

So by increasing the PMW value of the LED from 0 to 255 in increments of 5 we are fading the LED in!

Fade LEDStatement Type (initialisation condition increment)

for (value = 0; value <= 255; value+=5)

Page 32: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fading:

Now back to the loop procedure code, lets have a look at the statements for the ‘for statement.

This time we have some statements that tell the computer to write the outcome of the for statement to four pins, 5, 6, 9 and 10. We missed pins 7 and 8 because not all of the pins on the lilypad have the PMW functionality. Only pins 3, 5, 6, 9, 10 and 11 have PMW

And we can also see that there is a delay of 30 milliseconds so the computer knows to make the fade in go for that length of time. All of the statements inside the {} curly brackets belong to the for statement.

The last line of code is a delay which tells the computer to leave the four LEDs on for 200 milliseconds before it does anything else.

Fade LED

Page 33: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Fading:

Now lets try to to write some code to fade the LEDs out again. Remember the form of the ‘for’ statement we wrote earlier.

Hint: we will state the for statement like this:

{ for(value = 255; ...

Now for one last challenge of the day. Can you figure out how to make the LEDs fade in and out one at a time?

Fade LED

Page 34: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Hardware Set Up:

Battery SupplyPositive

Negative

LED attached to:

PIN 13 PIN12

Page 35: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

The Hardware Set Up:

Battery SupplyPositive

Negative For multiple LEDs

attached to one Pin.

**All LEDs attached to one pin will do the same thing - they

cannot be programmed separately.**

Page 36: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Useful LinksHardware

LittleBird Electronics - littlebirdelectronics.com.au

Jaycar - jaycar.com.au

Sparkfun - sparkfun.com (america)

Inspiration

Fashioning Technology - fashioningtech.com

Diana Eng - fashionnerd.com

Page 37: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

Other Fun...Materials

Lilypad Products - Lilypad Micro-processor, Tri-coloured LEDs, Sensors (movement, light, temperature)

Pressure Sensors

Electroluminescent (EL) Wire

Conductive Fabric

Fiber Optics

Alternative switches - press studs or zippers as switches.

Page 38: ThreadTech2: Intro to Programming LEDs with a Lilypad Arduino

What Next...Threadtech 3 - April 6 $90

Introduction to using sensor data to program lights.

Accelerometer (motion sensor), Light sensor, or Temperature Sensor

Lilypad Kit