scratch for arduino lesson 3 – analog input · slide 1 scratch for arduino lesson 3 – analog...

39
Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due to safety concerns.

Upload: others

Post on 16-Jul-2020

19 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slide 1

Scratch for ArduinoLesson 3 – Analog Input

Potentiometers, Variables, and Etch-a-Sketch*

Etch-a-Sketch may be omitted due to safety concerns.

Page 2: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 2

Analog Input

● Today we will extend the ongoing LED project by introducing an analog input device

Page 3: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 3

Graphical Dimmer Switch

● Last week some of you may have completed the Graphical Slider control

Page 4: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 4

● Today we will be using a Potentiometer as a variable resistor, or Rheostat, to recreate the effect with a physical switch...

Physical Dimmer Switch

Page 5: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 5

● What is electrical resistance?

Resistors & Variable Resistors

a measure of the difficulty to pass an electric current through a circuit element

Page 6: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 6

● Fixed Resistors have fixed resistance

Resistors & Variable Resistors

● Variable Resistors have variable resistance

Page 7: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 7

● Inside there’s a fixed resistor and a wiper that points between the minimum and maximum terminals of the resistor

Variable Resistors

Page 8: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 8

● Let’s use a multimeter to measure the minimum and maximum resistance of the wiper terminal

Variable Resistors

? ohms

Page 9: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 9

● Resistance causes voltage to drop, so the potentiometer also behaves as a Voltage Divider

Variable Resistors

5 V0 V

0 V

Page 10: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 10

● Resistance causes voltage to drop, so the potentiometer also behaves as a Voltage Divider

Variable Resistors

5 V0 V

5 V

Page 11: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 11

● Resistance causes voltage to drop, so the potentiometer also behaves as a Voltage Divider

Variable Resistors

5 V0 V

2.5 V

Page 12: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 12

● Resistance causes voltage to drop, so the potentiometer also behaves as a Voltage Divider

Variable Resistors

5 V0 V

1 V

Page 13: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 13

● Unlike the digital pins which read either 0 or 1, analog inputs can read a range of values.

Analog vs. Digital Input

5 V0 V

?

Let’s find out what that input range is...

Page 14: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slide 14

Let’s Get Hands-On

(the exciting part…?)

Page 15: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 15

Connect to Arduino

This end to computer

This end to Arduino

Some of the LED on the Arduino should light up after connection

Page 16: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 16

Start up mBlock

● Don’t forget mLink● Go to https://ide.mblock.cc to start mBlock● File → New

Get rid of this guy

Add this one instead

Page 17: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 17

Connect with Arduino

● Switch to “Live” mode and “Connect”

● Select a COM port[x] “Show all connectable devices”

● If appears, click on

it followed by and

“Updates”

Page 18: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 18

Review Breadboard

Use long strips to create lots of pinholes for GND & 5v

Useful when connecting multiple devices

Page 19: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 19

Potentiometer Circuit

+-

+-

5 V

-

GND5 VA0 port will read the Potentiometer wiper.

GND & 5v connected through long strips.

Leave room for the LED circuit!

Page 20: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slide 20

Exercise 3a

Read Analog Input

Page 21: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 21

Read Analog Input

Need to do something with the value….

Page 22: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 22

Read Analog Input

Create a new variable.

Keep reading the Analog Input pin A0 in a loop.Set the variable to the value.See Monitor

on the Stage

Page 23: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 23

Challenges● Note in your Student Handout the min/max values for

the A0 readings for the Potentiometer’s Wiper

● Write a program that:

1) checks if Potentiometer wiper is closer to GND or closer to 5V

2) if closer to GND Panda says “GND”

else

Panda says “5V”

Page 24: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 24

Condition on Analog Input

Add the Panda portion & Test it out!

Can you add conditions for 4 different outcomes?

The “if – then” and “if – then - else”conditional constructsare very importantin programming.

They allow you to change the behavior of your program based on input or state.

Page 25: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 25

Light Control

● Back to our dimmer effect...

● Let’s combine our LED and Potentiometer circuits to re-create this effect.

Page 26: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 26

Challenges

● Make a Physical Dimmer Switch:

Page 27: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 27

Dimmer Switch Circuit

Page 28: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 28

Challenges

● In your Handout, note the math required to convertbetween 0-1023 (input) and 0-255 (output)

● Then program it in mblockHint: look at the map block under Data

0-1023 ? 0-255

Page 29: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slide 29

Exercise 3b

Mapping Input to Output

Page 30: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 30

We should now have two variables.

One to read Potentiometer from A0.

Second to store the mapped output PWM (0-255).

Variables

Page 31: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 31

Dimmer Program

1) Read and store A0

2) Map and store PWM output3)Output PWM to

the LED pin

Page 32: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 32

Challenges

● Try to upload the program to the Arduino for a smoother effect. Just change the mode, and the start event to when Arduino starts up.

● Change the program so that the Potentiometer controls how fast the LED blinks, instead of its brightness level.

Page 33: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slide 33

Exercise 3c

Move, Panda, Move!!!

Page 34: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 34

Move, Panda, Move!● Make Potentiometer control Panda’s X-axis position.

1-D Joystick

Page 35: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 35

Move, Panda, Move!● Find Pand X-axis min and max (move it around)

● Map A0 to Panda’s X-limits and store in a variable (for all sprites).

● Tell Panda to set its position to that saved value.

1-D Joystick

Page 36: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 36

Move, Panda, Move!

1-D Joystick

Page 37: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 37

Challenges

● Make Panda point towards the new X ● Make Panda look like it’s walking by changing

costumes– And make Panda only walk when it’s moving…

● Add a second Potentiometer to control Y.Like an Etch-a-Sketch!

Page 38: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 38

Extra Challenges

● Use multiple LEDs to create a Metering Effect. The bigger the reading, the more LEDs are lit.

You can start with 3 LEDs, e.g.:

if (A0 < 330) turn off everythingelse if (330 < A0 < 660) turn on 1else if (660 < A0 < 990) turn on 2else turn on 3

Page 39: Scratch for Arduino Lesson 3 – Analog Input · Slide 1 Scratch for Arduino Lesson 3 – Analog Input Potentiometers, Variables, and Etch-a-Sketch* Etch-a-Sketch may be omitted due

Slides available at: http://a9i.sg/huayi

Slide 39

Copyright

● Created by A Posteriori LLP● Visit http://aposteriori.com.sg/ for more

tips and tutorials● This work is licensed under a Creative

Commons Attribution-ShareAlike 4.0 International License.