a1tD0000003mBLFIA2

# Course: Scratch with Autonomous Car5: Light Detection

• Beginner

### Lesson Description:

In this lesson, students will learn basic concepts in Computer Science:
1    Light sensors
2    If loops, Forever loops
To illustrate these concepts we will design automatic headlights for the RobotLAB Autonomous Car!

Objective:
Apply programming concepts to turn the headlights on and off depending on the light in the room!

## Standards Covered

### 3-5-ETS1-1

Define a simple design problem reflecting a need or a want that includes specified criteria for success and constraints on materials, time, or cost.

### 3-5-ETS1-2

Generate and compare multiple possible solutions to a problem based on how well each is likely to meet the criteria and constraints of the problem.

### 3-5-ETS1-3

Plan and carry out fair tests in which variables are controlled and failure points are considered to identify aspects of a model or prototype that can be improved.

### CCSS.MATH.PRACTICE.MP1

Make sense of problems and persevere in solving them.

### CCSS.MATH.PRACTICE.MP2

Reason abstractly and quantitatively.

### CCSS.MATH.PRACTICE.MP3

Construct viable arguments and critique the reasoning of others.

### CCSS.MATH.PRACTICE.MP4

Model with mathematics.

### CCSS.MATH.PRACTICE.MP5

Use appropriate tools strategically.

### CCSS.MATH.PRACTICE.MP6

Attend to precision.

### CCSS.MATH.PRACTICE.MP7

Look for and make use of structure.

### CCSS.MATH.PRACTICE.MP8

Look for and express regularity in repeated reasoning.

## Teaching Tips:

What else do people do in cars?

In order for an autonomous car to be effective, it has to juggle a lot of responsibilities.  If time permits, allow students to collaborate and make a list of all the functions an autonomous car needs to perform.

## What else can you make?

Vote for the functions you think are the most important!
• play my favorite music
• use headlights in the dark
• use windshield wipers in the rain
• turn on the AC when it is hot
• park in a parking lot

Can you name some objects in everyday life that use light sensors? Type your answer using only one word.

## Teaching Tips:

Question 1: Why would you need to turn the headlights on in your car?
To see in the dark.

Question 2: What do you think this code does?

Turns on the headlights of the car on and off.

At this point in the class, the teacher should read some of the answers aloud, and ask students for their thoughts.

Challenge:

What does this code do?

Show the class this program, but don't run it until several students guess what it does.

//www.robotlab.com/hubfs/Education.Robotlab.com/Autonomous%20Car/Scratch/lesson5exercise.sb2

Your parents do a lot of things when they drive, and our self-driving cars will need to do these things too.  For this lesson we will be turning the headlights on and off!

Why would you need to turn the headlights on in your car?

What do you think the code below does?

(An LED is a kind of light.  You have seen the RobotLAB autonomous car light up with many LEDs.)

## Teaching Tips:

Here is the code: Lesson 5 Code

Here is a Step-by-Step guide to complete the code: Lesson 5 Code Instructions

Notice that it is very similar to the first lesson’s loops. While it should reinforce understanding of the basic programming concepts, it may also be a good way to find out which students are struggling.

Code solution:

Challenge solution:

1. Make your list save the current value from the CDS sensor every half second.
2. Write a function that deletes all the list data when the space key is pressed.

Note: Choosing 30 as the "dusk" value may not work for brightly lit classrooms.  Adjust this number if needed.

Time to code!

 1. Bluetooth 2. Orchestra 3. Scratch Connect your autonomous car to your dedicated computer via Bluetooth. Open Orchestra and connect your robot to the program. Open Scratch from Orchestra

Code:

Write some code that makes your car turn on its lights if the brightness in the room goes below 30.

Step 1

Look at this code from the first lesson.

Step 2

Notice how the code you just saw is very similar.  The blocks on the left turn the front lights on, and the blocks on the right turn the front lights off.

How can you tell if it is dark outside?  You can see it with your eyes!  How can your RobotLAB Autonomous Car tell if it’s dark?

Using a sensor called a CDS sensor.

Step 3

Write some code that makes your car turn on its lights if the brightness in the room goes below 30 (make it a variable).  Here are some blocks you can choose from:

Hint:

Step 4

It's much easier to troubleshoot your code when you can see the results from the sensor.  But how can you log the value or make any sense of it if it's constantly changing?

How can you log the data from the CDS sensor?
• make several variables
• write the values down
• make a list in Scratch
• I don't know

Try this:

1. Click "Make a List"

2. Name it something like "light" or "light data"

3. All these new blocks will appear!

4. And you'll see an empty list in the top left corner of the screen.

Challenge

1. Make your list save the current value from the CDS sensor every half second.
2. Write a function that deletes all the list data when the space key is pressed.

## Teaching Tips:

Question 1: Check the concepts you understand.
This is the students' self-assessment of their understanding of the material.  You will see a bar graph once all the poll answers are submitted

Question 2: What does the CDS sensor do?
It measures the level of light in the area.

Question 3: Why was the Forever loop necessary for the program?
It is necessary because we want the program to run over and over, not just one time.

Question 4: What was the coolest thing you learned today?
Any answer will do here.  If time permits have a short discussion with students about their answers.

At this point in the class, the teacher should read some of the answers aloud, and ask students for their thoughts.

Don’t worry, this isn’t for a grade, it’s just so your teacher can check the classes’ understanding.

Check the concepts you understand.
• CDS sensor
• Greater-than
• Less-than
• If loop
• Forever loop
• Variable

What does the CDS sensor do?

Why was the Forever loop necessary for the program?

What was the coolest thing you learned today?