Course: Scratch with Autonomous Car
2: Distance Detection

  • 1-5 grade
  • Beginner

Lesson Description:

In this lesson, students will learn basic concepts in programming: 
    1    Algorithms
    2    Sensors and data
To illustrate these concepts we will program the RobotLAB Autonomous car to use data from the distance sensor to stop before hitting an obstacle!

Apply basic programming concepts to make the car stop when it approaches an obstacle.


Standards Covered


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


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.


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.


Make sense of problems and persevere in solving them.


Reason abstractly and quantitatively.


Construct viable arguments and critique the reasoning of others.


Model with mathematics.


Use appropriate tools strategically.


Attend to precision.


Look for and make use of structure.


Look for and express regularity in repeated reasoning.

image description

Lesson Modules

Teaching Tips:

At the start of class, the teacher should run the demo code provided a program that drives towards a wall, but stops before it hits!  


(Be sure to run the demo code with the car on the ground so that it does not accidentally fall off of a high surface.)



The car approaches a wall, but stops before it hits! Look at the blocks and answer the following question.  

These blocks are necessary:
2, 3, 5, 6, 7, 8, 9, 10


What does your autonomous car need?

What does your autonomous car need to do to keep from running into things while it is driving?  When you are at the wheel, you can avoid the obstacles you see, but the RobotLAB Autonomous Car does not see like you do.  How can we help it “see” its environment?


The car approaches a wall, but stops before it hits!

What blocks do you think we need to create this algorithm?  (think back to what we talked about in the last lesson).
  • 1. key A pressed
  • 2. when A key pressed
  • 3. forever
  • 4. Steering
  • 5. if ____ then
  • 6. Go L: 500 R: 500
  • 7. Go L: 0 R: 0
  • 8. ___ > ___
  • 9. ___ < ___
  • 10. Sensor IR-2

Teaching Tips:


The correct answer is provided here for teachers:


 Variable exercises

  1. Make a variable that starts at 10, then in increments of 3- once per second until it reaches 25.
  2. Make a variable that starts at 100, then decreases by 5 every 1/2 second until it reaches 0.

Bonus: Make a variable that starts at 50, then decreases by half its value until the value is less than 1.


Teaching Tips:

Here is the code: Lesson 2 Code

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

At this point, take two student volunteers (or two groups)- one that used a number in their code, and one that used a variable.  Ask them to explain how their code works and their reasoning behind what their choices.

If time permits, discuss the benefits of using variables in your code.

  • Looks kind of silly now, like it might be a waste of time, right?  But imagine if you had 100 repeat-until statements.
  • Changing each of them individually would be a real pain.  A variable is much more convenient.

Why should I use a variable?


Teaching Tips:

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

Question 2: What is a variable? What does it do?
A variable is a way to save a number (or even a letter or word) using a name.  you can use it over and over again without having to type it in.

Question 3: What was the most challenging part of this lesson?
Any answer will do here.  If time permits have a short discussion with students about their answers.

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.

Store Student Files
Put the student files on a thumb drive or store them in some way so that the students can continue working on their project in the next lesson. 

Pack Up Hardware
Turn off the robots and computers and pack the robots in the box accordingly. 

You learned a lot today!

Today was the first step in making a real autonomous car.  You learned what an IR sensor is, and how to use the data from the sensor to tell your car when to drive and when to stop.  You also learned how to save a number as a variable, and make your code loop over and over.

Now you have a car that stops to avoid hitting obstacles and goes when the path is clear.   



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.
  • IR sensors
  • Variables
  • Forever loop

What is a variable? What does it do?

What was the most challenging part of this lesson?

What was the coolest thing you learned today?  Write your answer in the box, and see it appear with your classmates answers!