84

Introduction to Arduino with ArduBlock & SparkFun LilyPad

Embed Size (px)

DESCRIPTION

Powerpoint Slides for using ArduBlock and the SparkFun Lilypad Protosnap Development board. This is a slide deck used on the SparkFun National Tour

Citation preview

Page 1: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 2: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 3: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Introduction to ArduinoWith ArduBlock & LilyPad Dev

Brian HuangEducation Engineer

[email protected]

Page 4: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Pre-Class Survey

http://bit.ly/14Xk3Ek

Page 5: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Resources

This PPT

ArduBlock Download & Installation http://www.ardublock.com/

Page 6: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 7: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 8: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Installation Process

ArduBlock is a run-time Java script that must be saved to a specific location on your computer.

The file ardublock-xxxx.jar must be inside the following file structure within your Arduino sketchbook:

\Arduino\tools\ArduBlockTool\tool

Note: Folder names are case sensitive

Page 9: Introduction to Arduino with ArduBlock & SparkFun LilyPad

General Rules

Raise your hand if you have a question.Be respectful.When I raise my hand…Share your findings with the people

near you.Poke around, experiment, and have

fun…Imagine, create, and play!!!

Page 10: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Objectives

Provide a thorough introduction to the Arduino programming environment.

Develop a use of simple functions to interact with the LEDs, light sensor, push button, and buzzer on the Protosnap Pro Mini.

Page 11: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 12: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Arduino LilyPad Protosnap Dev

What is it?

What can it do?

Who cares?

Page 13: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What is it?

Heart of it – ATMega328 (a $4 microcontroller)

Open Source

High tolerance for ESD and “bad” electronics practices…

Provides easy way to interact with a computer physically – a.k.a. physical computing

Page 14: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What can it do?

- Great for prototyping ideas

- Great to teach Intro to Programming Arduino (no assembly required!)

- Lots and lots of options!

- Large pads can be sew-able

Page 15: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Who cares?

Hackers / Makers

Engineers

Artists

Musicians

Kids!

Teachers!!

You!!!

Page 16: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Physical Computing

Page 17: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What is all this stuff???Components

Buzzer / Speaker

Light Sensor

Temp.Sensor

the Brain ATmega328(16 MHz clock)

RGB LED

Vibe Motor

Switch / Button

Light Emitting

Diodes (LED)

Page 18: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What is all this stuff???Silk Screen & Pins

Actual Pins on Atmega328

Page 19: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Startup Arduino IDE

Double-click on either the Arduino Icon or wherever you installed (saved) the Arduino program.

Page 20: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Plug it in

Page 21: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Lesson #0a – Setting up Arduino

1. Setup Board (LilyPad Arduino w/ ATmega328)

2. Setup COM PortPC – Highest COM #Mac – /dev/tty.usbserial-A####xXx

Page 22: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Tools BoardSetup Board

Page 23: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Tools Serial PortSetup COM Port

PC – Highest COM #

Mac – /dev/tty.usbserial-A####xxx

Page 24: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Other Board Types

Page 25: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Tools ArduBlockLoad ArduBlock

Page 26: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Programming Area

Zoomed out ViewProgramming

Blocks Palate

Page 27: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What is ArduBlock?

• Graphical Introduction to Programming• Scratch-like interface• Clear transition to text-based coding• Generates real Arduino code in the

background.

Page 28: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Block Organization

Manipulating or modifying the program flow or behavior.

Serial and I2C communication: transferring data between devices & PC

Special utilities: Tone, servo, and ultrasonic sensors

Creating, assigning, or using constants, variables, or arrays.

Simple and complex math operations.

Boolean logic operations and comparisons.

Reading or setting values (voltages) to pins on the microcontroller.

Page 29: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Block OrganizationControl

Manipulating or modifying the program flow or behavior.

Page 30: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Pin # – any number from 0 to 19

Values –

Pin # – 3, 5, 6, 9, 10, or 11 .

Values –

Allows the control of the pins on the Atmega328 chip – turning them on or off (like the light switch)

Block OrganizationPins

or to

Notice the different shapes?

Digital Values Analog (Number) Values

Page 31: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Lesson #0b – Test Upload

Notes:• Click and drag blocks from the left pane to the

Programming Area.• Blocks should snap/click together.• Right-click to copy-paste (clone) a block• Drag blocks back to the left pane to discard.

Click Upload to Arduino and watch…

Create the following program -- What do you expect this to do?

Page 32: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Click back to the Arduino window to check for status messages

Page 33: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What do you see?

LED on pin #13

Page 34: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 35: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What about turning the LED off?

Click to change the HIGH to a LOW

Page 36: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Let’s get to hacking…

Lesson #1 – What if I want to Blink?“Hello World” of Physical Computing

Psuedo-code – how should this work?

Turn LED

ON

Wait

Turn

LED OFF

Wait

Rinse &

Repeat

Page 37: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Let’s get to hacking…

Lesson #1 – What if I want to Blink?“Hello World” of Physical Computing

Runs once at the beginning of code.

Repeats – continuously over and over and over…

Page 38: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Example

Page 39: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What other LEDs can we play with?

16 18 17

Page 40: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Pins with an A are actually Analog INPUT pins – but they’re really

identified as…

Digital Pins are numbered 0 to 13.

Analog Input Pins are A0 to A5. but…

Analog Pin Digital I/O ID

A0 14

A1 15

A2 16

A3 17

A4 18

A5 19Analo

g P

in M

appin

g

Page 41: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Alright? Let’s blink!

Challenge 1a – Blink to mimic a heartbeat

Challenge 1b – Blink multiple LEDs

First -- plan out a pattern on paper – then start coding

Challenge 1c – Multiple LEDS – Knight Rider (Cylon)-esque

Page 42: Introduction to Arduino with ArduBlock & SparkFun LilyPad
Page 43: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Save your Project

• Save early, Save often!

• Enumerate your iterations – (e.g. blink_10.adp, blink_11.adp, blink_12.adp…)

Page 44: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Speeding up and Slowing Down…

What if we wanted to create a pattern that starts off with a slow blink and gets faster and faster? What part do we need to be able to change?

constant

Page 45: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Programming Concepts: Using Variables

Variable Types:

8 bits 16 bits 32 bits

byte char

intunsigned int

longunsigned long

float

Page 46: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Using Variables in ArduBlockNumbers/Constants

Variable is just a placeholder for a value.

This placeholder can be manipulated within the code.

Page 47: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Start by setting a variable called delayTime in the setup

Page 48: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Now… replace the 1000 in each delay with the variable

name

Page 49: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Decrement delayTime each loop – using Math Operators

Page 50: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Example: Did you come up with something like this?

Page 51: Introduction to Arduino with ArduBlock & SparkFun LilyPad

What happens when you reach zero?

Let’s find out!

Page 52: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Debugging – how can we see what’s going on?

Serial Monitor

Serial print command

Any message

“glue”

Data

Page 53: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Add this block… so that we can watch the variable value

Page 54: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Upload & Click back to Arduino

Open the Serial Monitor

Page 55: Introduction to Arduino with ArduBlock & SparkFun LilyPad

How do we fix / prevent this from happening?

If this is TRUE…

Do this.

Otherwise, do this.

Conditional Logic: If(), If() – else…

Page 56: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Logical Operators Comparisons / Conditionals

Greater than

Less than

Equal toGreater than or equal

toLess than or equal to

Not equal to

Page 57: Introduction to Arduino with ArduBlock & SparkFun LilyPad

If delayTime <= 0… reset

Page 58: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Advanced…Combinations of inequalities

Combine multiple conditions together using or and and

Example:

Page 59: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Other than just turning an LED on and off…

What else might we want to do?

Page 60: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Concept: Analog vs. Digital

Micro-controllers are digital devices – ON or OFF.

An Analog signal is anything that can be a full range of values. xWhat are some examples? Think of like a ramp or a hill.

Page 61: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Concept: Analog vs. Digital

To create an analog signal, the microcontroller uses a technique called PWM.

Pulse Width Modulation (PWM)

Pins 3, 5, 6, 9, 10, 11 are capable of producing an Analog Output

Page 62: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Color Mixing with 3 LEDsRGB LED

Challenge 2a:

1) Experiment with different values – what do you observe happens? Be prepared to share with the class – what did you try, and what did you see?

2) Do you observe anything strange about the behavior?

3) How do you create Orange? Purple? Magenta?

LED PINS

RED = 9

GREEN = 11

BLUE = 10

Change either of these values… See what happens!

Page 63: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Color Mixing with 3 LEDsTri-color LED

The RGB LED is a Common-Anode (means the positive side of the LED is all tied to Vcc).

The signal line is connected to the bottom of each LED.

Don’t worry - it’s just a different method of completing a circuit!

Pin9

Pin11

Pin 10

5 V

Value = 0 (LOW) LED is ONValue = 255 (HIGH) LED is OFF

Page 64: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Color Mixing with 3 LEDs

Challenge 2b:

Experiment with different values of Red, Green, and Blue to create a couple different colors.

Write down the values you used. Check these values with www.colorpicker.com

Challenge 2c:

Create a unique lighting display that blinks, fades, or switches colors. Be prepared to have a name for your art piece and show this to the rest of the class.

Page 65: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Save your Project #2

Page 66: Introduction to Arduino with ArduBlock & SparkFun LilyPad

New Challenge Handling Inputs

Page 67: Introduction to Arduino with ArduBlock & SparkFun LilyPad

A Simple Example

Try this bit of code.

What do you think will happen? Test it and write down your observations.

DIG INPUT 2

Page 68: Introduction to Arduino with ArduBlock & SparkFun LilyPad

digital InputsButtons / Slide Switches

INPUT A5INPUT A5 (a.k.a. 19)

What about this??

Analog Pin Digital I/O ID

A0 14

A1 15

A2 16

A3 17

A4 18

A5 19Analo

g P

in M

appin

g

Page 69: Introduction to Arduino with ArduBlock & SparkFun LilyPad

digital InputsButtons / Slide Switches

Challenge 3a:

Re-arrange the blocks or add new ones to:• Blink LED quickly when the button is pressed.• Otherwise blink slow

Same as Pin A5

Page 70: Introduction to Arduino with ArduBlock & SparkFun LilyPad

digital InputsButtons / Slide Switches

Challenge 3b:

Now, using the button / switch – to start a disco/light changing pattern

Challenge 3c:

Now, using the button / switch – to turn on and off the VIBE motor.

Page 71: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Reading the Light Sensor?

What is the biggest and smallest value the analog pin will “measure”? [Hint: open your serial monitor to find out!]

Page 72: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Challenge

Use the light sensor input to affect the blink rate of the LEDs.

Use the light sensor input to affect the speed of a “knight-rider” or cylon display.

Use the light sensor to ___________.

Page 73: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Save your Project #3Sensors

Page 74: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Making MusicSetting up the Speaker

Three simple blocks to play with…

Page 75: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Making MusicLet’s Play a song…

Twinkle-Twinkle:CC GG AA G FF EE DD C GG

Happy Birthday:G G A G C B G G A G D C G G G E C C B A 

Hot Cross Buns:B A G <> B A G <> G G G G <> A A A A B A G

Mary Had A Little Lamb:B A G A BB B <> AA A <> BB B <>

B A G A BBBB AAA BAG.

Page 76: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Musical Notes / Frequencies

Note  Frequency (Hz)

C4 261C#

4/Db4 277

D4 293D#

4/Eb4 311

E4 329F4 349

F#4/Gb

4 369G4 392

G#4/Ab

4 415A4 440

A#4/Bb

4 466B4 493

Note  Frequency (Hz)

C5 523C#

5/Db5 554

D5 587D#

5/Eb5 622

E5 659F5 698

F#5/Gb

5 739G5 783

G#5/Ab

5 830A5 880

A#5/Bb

5 932B5 987

Page 77: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Save your Project #4Tone

Page 78: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Getting started with Arduino

Page 79: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Additional Resources

http://www.sparkfun.com

http://learn.sparkfun.com

http://www.arduino.cc

http://www.bildr.org

Page 80: Introduction to Arduino with ArduBlock & SparkFun LilyPad

SparkFun Tutorials and Projects

- GardenBot: www.sparkfun.com/news/598

- High Altitude Balloon:www.sparkfun.com/tutorials/180

- GPS Clock: www.sparkfun.com/tutorials/47

- Earthquake Data Logger: www.sparkfun.com/tutorials/235

Page 81: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Thousands of Amazing ideas…

The Cat Defender…http://www.plasma2002.com/blenderdefender/List of projects at Sparkfun:https://learn.sparkfun.com/tutorials

Page 82: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Questions?

Page 83: Introduction to Arduino with ArduBlock & SparkFun LilyPad

Before leaving, please fill out a quick survey so that I can get your e-mail and a little info about your familiarity with Arduino / Sparkfun.

Thanks!

http://www.sparkfun.com/WorkshopFeedback

Feedback?

Page 84: Introduction to Arduino with ArduBlock & SparkFun LilyPad

www.sparkfun.com6175 Longbow Drive, Suite 200

Boulder, Colorado 80301