1 of 61

Infrared Sensor

SensorBot

Confidential and proprietary property of TinkRworks, Inc.

Confidential and proprietary property of TinkRworks, Inc.

2 of 61

  • What is Infrared?

1

Confidential and proprietary property of TinkRworks, Inc.

3 of 61

Your IR Sensor

  • You installed your IR Sensor back in the Lights module.

​

  • Now, let’s explore what that is.

2

Confidential and proprietary property of TinkRworks, Inc.

4 of 61

What is IR?

  • Infrared is a part of the Electromagnetic Spectrum.

​

  • The Visible Spectrum contains the only wavelengths we can see.

​

  • Infrared Starts where the color “Red” ends.

3

Confidential and proprietary property of TinkRworks, Inc.

5 of 61

What is IR used for?

  • We can use an IR filter to see IR light.
  • All objects give off IR light in proportion to heat.

​

4

Visible light

Infrared light

Confidential and proprietary property of TinkRworks, Inc.

6 of 61

What is IR used for?

Astronomers can use infrared filters to see through all the dust in space too!

5

Confidential and proprietary property of TinkRworks, Inc.

7 of 61

  • Remote Control and Sensor

6

Confidential and proprietary property of TinkRworks, Inc.

8 of 61

Remote and Sensor

Remote Control

IR Sensor

7

The remote control connects to the IR sensor.

Infrared light

Confidential and proprietary property of TinkRworks, Inc.

9 of 61

Protocol

What does the remote-control protocol look like?

8

Start indicates the beginning of the transmission

The stop indicates the end of the transmission

Command

Device address (unique per TV manufacturer)

Confidential and proprietary property of TinkRworks, Inc.

10 of 61

IR Sensor

The IR receiver needs power and a way to communicate with the Arduino.

9

GND

5V

S

Confidential and proprietary property of TinkRworks, Inc.

11 of 61

  • Program IR Sensor

10

Confidential and proprietary property of TinkRworks, Inc.

12 of 61

It’s Time to Code!

  • Open Google Chrome (web browser)

​

  • Navigate to www.tinkrcode.com

​

​

  • Click “Have a Code?”

11

Confidential and proprietary property of TinkRworks, Inc.

13 of 61

Project Code

  • You can enter the Shapes or Numbers by selecting the circle next to the words.
    • If you make a mistake, click “Clear”.

​

  • Click Submit

​

12

Confidential and proprietary property of TinkRworks, Inc.

14 of 61

Class Code

  • Enter your class code of either Fruit or Numbers.
    • If you make a mistake, click “Clear”.

​

​

  • Click Submit

13

Confidential and proprietary property of TinkRworks, Inc.

15 of 61

Open Your Program

  • Open your program

​

14

Confidential and proprietary property of TinkRworks, Inc.

16 of 61

Connect to Bluetooth

15

  • 1. Click Connect

​

​

  • 2. Choose the number that is on your Project Sticker and click Pair.

​

​

  • 3. Green means connected!

​

​

Confidential and proprietary property of TinkRworks, Inc.

17 of 61

Programming with the IR Sensor

  • This block is provided in the IR drawer.

​

  • This block reads the IR sensor, and if a button on the remote is being pressed, the value of that remote button will be put into a variable named ir.

​

  • Your IR sensor is connected to pin D2.

16

Confidential and proprietary property of TinkRworks, Inc.

18 of 61

Serial Printing

17

Confidential and proprietary property of TinkRworks, Inc.

19 of 61

Reading the IR Sensor

Run this code and press several different buttons on the remote.

​

​

18

Confidential and proprietary property of TinkRworks, Inc.

20 of 61

Reading the IR Sensor

19

  • What happens?

​

  • What do you think the number 17 means?
    • 17 prints out whenever there isn’t a button being pressed

​

  • What do the other numbers mean?
    • Different buttons print out different numbers.

​

Confidential and proprietary property of TinkRworks, Inc.

21 of 61

IR Sensor Values

  • When does a number other than 17 print out?
    • When you press a button.

​

  • Try holding down a button.
    • What happens then?

​

  • When does a number other than 17 print out?
    • Only when you first press the button.

20

Confidential and proprietary property of TinkRworks, Inc.

22 of 61

Very Important!

The ir variable value is a number other than 17 ONLY when the button is first pressed.

21

Confidential and proprietary property of TinkRworks, Inc.

23 of 61

Ir Values

  • The numbers that print out don’t actually match up to the numbers on the remote control’s buttons.

​

  • To make it easier, TinkRcode has some blocks that represent the remote control’s buttons.
    • These blocks are equal to the numbers you printed out (like 6, or 10, etc.)

​

  • The values are Up, Down, Left, Right, Ok, Hash, Star, and the numbers Zero through Nine.

​

  • There is NOT a block for when no button is pressed. These blocks ignore when the ir value = 17.

​

22

Confidential and proprietary property of TinkRworks, Inc.

24 of 61

Revise Important Statement

The ir variable value is equal to the button block only when the button is first pressed.

23

Confidential and proprietary property of TinkRworks, Inc.

25 of 61

  • Using the ir Variable

24

Confidential and proprietary property of TinkRworks, Inc.

26 of 61

Conditionals

We want the bot to do something when we press a button.

​

​

25

Button is pressed

​

​

Do something

​

​

But we really want it to do something different when different buttons are pressed.

Confidential and proprietary property of TinkRworks, Inc.

27 of 61

Lots of Conditionals

What we really need to say is:

  • If button = up
    • Move forward
  • If button = down
    • Move backward
  • If button = left
    • Turn left
  • If button = right
    • Turn right
  • If…

​

26

Confidential and proprietary property of TinkRworks, Inc.

28 of 61

Another type of Conditional: the Case Block

  • The Switch/Case block 'picks' a code statement to run based on the value of a variable.

​

  • This is a useful command when a variable will have one of a fixed set of values and you want to do something different based on each value.

​

27

Confidential and proprietary property of TinkRworks, Inc.

29 of 61

The Case Block

The Switch block needs a variable. This variable is used to pick one of the case blocks to run.

​

The value in the Case block indicates that the block will run when the variable equals that value.

​

There can be lots of cases. Drag in as many Case blocks as you need.

​

The Default block is used when none of the values match.

28

Confidential and proprietary property of TinkRworks, Inc.

30 of 61

Programming With the IR Sensor

  • This Switch/Case statement can be found in the Bot drawer.

​

  • The variable is what is read in from the IR sensor. The IR sensor is connected to pin D2.

​

  • Several Case blocks are set up for some of the different remote-control buttons.

29

Confidential and proprietary property of TinkRworks, Inc.

31 of 61

Programming With the IR Sensor

  • Here’s an example of the IR Switch block that is set up so that the bot goes forward when the user presses the Up button on the remote control.

​

  • You can put subroutine calls into the Case statements.

30

Confidential and proprietary property of TinkRworks, Inc.

32 of 61

Moving Forward

Remember:

    • The ir variable value is equal to the button only when the button is first pressed.

​

31

  • Let’s look at the forward subroutine.

​

  • The subroutine starts the motors, so it is ok if it is only run once at the moment you press the button.

​

Confidential and proprietary property of TinkRworks, Inc.

33 of 61

Challenge #1: Remote Control Car

Challenge Name

Remote Control Car

​

Challenge Objective

Turn your robot into a remote-control car.

​

Challenge Notes

When you press the Up button, it should go forward. The down button should make the robot go backward and left and right should make it spin. Use the OK button to stop everything.

​

32

Confidential and proprietary property of TinkRworks, Inc.

34 of 61

Solution #1

Can put slow or fast here.

33

Confidential and proprietary property of TinkRworks, Inc.

35 of 61

Control Flow

  • After the Setup is run, the Control Flow starts at the top of the loop.

​

  • It sets the current value of the button to the variable ir.

34

  • The forward fast subroutine is in the UP case.

​

  • The control flow then goes to the subroutine and the motors are started.
  • Next is the Switch/Case block.

​

  • Let’s say that the UP button has been pressed.

​

  • The UP Case will now be entered.

​

  • When the subroutine is done, the Control Flow goes to BELOW the Switch/Case.
    • The other cases are skipped.
  • Finally, the Control Flow goes to the top of the Loop again.

​

Confidential and proprietary property of TinkRworks, Inc.

36 of 61

Challenge #2: Lights

Challenge Name

Lights

​

Challenge Objective

Add a series of lights to Case STAR of your Switch/Case block using delays.

​

Challenge Notes

You can create a lights subroutine, or just put them in your Case STAR.

​

When you have added them, try them out by pressing the * button on the remote controller.

​

35

Confidential and proprietary property of TinkRworks, Inc.

37 of 61

Solution #2

  • What happens when you run this program and press the *?

​

  • The lights should turn Green and then switch to Red. But they stay on Red.
    • Why is that?

​

​

36

Confidential and proprietary property of TinkRworks, Inc.

38 of 61

Solution #2

  • When the * button is pressed, all the code in the STAR Case is run.

​

  • When the code in the STAR case is finished, the program then moves downward.
    • (or back to the top of the loop if no code is below it).

​

  • At the top of the loop, it checks again for the current ir variable.

​

  • By this time, the ir variable doesn’t equal STAR anymore because ir has gone back to being equal to that 17/”no button” value.

​

37

Confidential and proprietary property of TinkRworks, Inc.

39 of 61

Auto Backup

  • Let’s say that we wanted to add these 2 subroutines to Case ONE.

​

  • What would we want this code to do?
    • Drive forward and then automatically back up when it sees or hits an object.

​

  • What will actually happen?
    • The bot will move forward slowly and will not automatically backup.

38

Confidential and proprietary property of TinkRworks, Inc.

40 of 61

At the Same Time

  • Let’s say we want to drive around using the arrow keys AND have the bot automatically backup when it sees an object (or runs into it).

​

  • We just saw that we putting the Auto Backup in the Switch/Case won’t work.

​

  • To do this we will use modes.

​

​

​

​

39

Confidential and proprietary property of TinkRworks, Inc.

41 of 61

  • Modes

40

Confidential and proprietary property of TinkRworks, Inc.

42 of 61

Mode

  • We are going to define mode as:
    • A setting that allows the bot to repeatedly run a subroutine, but you can also drive if you want to.

​

  • For instance, the bot will automatically backup and turn when it sees an object (Auto Backup), PLUS you will be able to drive it around normally.

​

  • Modes:
    • Mode 0 will mean “Just Drive”
    • Mode 1 will mean “Auto Backup, but you can drive too”

​

  • We will use the number buttons on the remote controller to select a mode.

41

Confidential and proprietary property of TinkRworks, Inc.

43 of 61

Mode Variable

  • The variable ‘mode’ will hold the value of the mode we want to use.

​

  • Create a Global Variable named ‘mode.’

42

Confidential and proprietary property of TinkRworks, Inc.

44 of 61

Mode One

  • To use the number button to select code, we will need to use a Case.

​

  • For now, remove the Forward Slow and Auto Backup subroutines from Case One.

​

​

43

Confidential and proprietary property of TinkRworks, Inc.

45 of 61

Mode One

  • In Case One, Set mode = 1.

​

  • Pressing the #1 button will make the mode =1, but now we need to DO something with the variable mode.
    • Setting mode =1 by itself doesn’t do anything.

​

44

Confidential and proprietary property of TinkRworks, Inc.

46 of 61

Mode One

  • We want to enter the Auto Backup mode when the 1 button is pressed.

​

  • How do code something to happen only if something is true (button #1 is pressed)?

​

  • We need a conditional!
    • The conditional needs to go outside the Switch/Case.

​

  • What should go here?

45

Confidential and proprietary property of TinkRworks, Inc.

47 of 61

Try it

  • What should your bot do if you press button #1 and then drive using the arrow keys?

​

  • Try it out.

​

  • Did it do what you expected?

​

46

Confidential and proprietary property of TinkRworks, Inc.

48 of 61

Not Auto Backup

  • Let’s say that you want to stop the Auto Backup subroutine and just drive.
    • How would you make this conditional not true anymore?
    • Change the mode to not equal 1.

​

  • Let’s make it so that if you hit the 0 button, your bot would just drive and nothing else.

​

​

​

47

Confidential and proprietary property of TinkRworks, Inc.

49 of 61

Not Auto Backup

  • Let’s say that you want to stop the Auto Backup subroutine and just drive.
    • How would you make this conditional not true anymore?
    • Change the mode to not equal 1.

​

  • Let’s make it so that if you hit the 0 button, your bot would just drive and nothing else.

​

​

​

48

Confidential and proprietary property of TinkRworks, Inc.

50 of 61

Not Auto Backup

49

  • What should you put here?

​

  • You set mode=0 so that this conditional here is no longer true.

​

​

​

Confidential and proprietary property of TinkRworks, Inc.

51 of 61

Case Zero

​

​

We don’t need a conditional for mode = 0 because we are stopping the Auto Backup and not starting anything else.

​

50

Confidential and proprietary property of TinkRworks, Inc.

52 of 61

  • Challenges

51

Confidential and proprietary property of TinkRworks, Inc.

53 of 61

Challenge #2: More Modes

Challenge Name

More Modes

​

Challenge Objective

Add the Wall Following subroutine to a mode.

Add the Not Touching You subroutine to another mode.

Add any other Subroutines that you have or want to make.

​

Challenge Notes

Think of bits of code that need to be looped over and over to work.

52

Confidential and proprietary property of TinkRworks, Inc.

54 of 61

Challenge #3: Lights for Modes

Challenge Name

Lights for Modes

​

Challenge Objective

Show which mode you are in by changing the bot’s lights.

​

Challenge Notes

What color will be Mode 0?

What color will be Mode 1?

What color will be Mode 2?

53

Confidential and proprietary property of TinkRworks, Inc.

55 of 61

Challenge #4: Drive With Lights

Challenge Name

Drive with Lights

​

Challenge Objective

Turn on some lights while you drive using a new mode.

​

Challenge Notes

Create a new mode to turn on the lights.

When you press the 0, stop the lights.

​

54

Confidential and proprietary property of TinkRworks, Inc.

56 of 61

  • Solutions

55

Confidential and proprietary property of TinkRworks, Inc.

57 of 61

#2 Solution

  • Mode 2 = Not Touching you

​

  • Mode 3 = Wall Follow

56

Confidential and proprietary property of TinkRworks, Inc.

58 of 61

Solution#3

  • Here the lights are set to Red for Mode 1

​

  • And turned off during Mode 0

​

  • Complete for the other modes as well.

57

Confidential and proprietary property of TinkRworks, Inc.

59 of 61

Solution #4

  • Mode 2 turns the lights green.

​

​

​

  • Button 0 turns the lights off.

58

Confidential and proprietary property of TinkRworks, Inc.

60 of 61

Extension

  • As an extension, you can also create a mode with changing lights.

​

  • This mode will flash the lights green and then red.

​

  • The lights will continue to change as long as the mode = 2.

59

Confidential and proprietary property of TinkRworks, Inc.

61 of 61

TinkRworks, Inc.

501 W Lake Street, Suite 106

Elmhurst, IL 60126

www.TinkRworks.com

© TinkRworks, Inc. All rights reserved

Sparking CREATIVITY

Fostering INNOVATION

Sharpening PROBLEM-SOLVING skills

Confidential and proprietary property of TinkRworks, Inc.