Python Level-1 with Virtual Robotics - Mission 7: Line Sensors

Mission 7 Lesson Plan

Line Sensors

Students use sensor inputs to program the 'bot to navigate around lines.

⏰ 2-4 hours 🎯 Grades 9-12+ πŸ’» CodeSpace πŸ€– Virtual CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

Overview

Students use the CodeBot's line sensors to detect brightness and turn those readings into decisions. They read and print sensor values, use a threshold comparison operator to check whether a reading falls within a target range, and use abs() to compare two readings no matter which one is higher. From there, students bring together a constant, a matrix of compass directions and brightness readings, and a function that returns a value, so the CodeBot can find and respond to a specific color.

🎯 Mission Goal: Students use sensor inputs to program the 'bot to navigate around lines.

🎯

Learning Targets

  • I can read a line sensor and print the value.
  • I can print multiple numbers separated with a comma.
  • I can use threshold comparison operators to make decisions with sensor data.
  • I can break out of a loop.
  • I can use the built-in abs() function.
  • I can define and use a constant.
  • I can create a 2-dimensional array.
  • I can define a function that returns a value.
  • I can program the CodeBot to respond to a specified color.
πŸ’‘

Key Concepts

  • The data from an analog sensor needs to be changed to digital numbers for computing.
  • The CodeBot has five line sensors, and each one gives its own brightness reading.
  • Python has many built-in functions for math. Absolute value is one of them.
  • A threshold comparison operator lets you check if a value is within a range.
  • A user can input data using an input() function and typing an answer on the console.
  • A matrix, or 2-dimensional array, can be used to store two pieces of data that are related, like a compass direction and brightness reading.
  • A function can return a value to a variable.
βœ…

Assessment Opportunities

  • Quiz after Objective 1
  • Quiz after Objective 3
  • Quiz after Objective 6
  • Obj. 2 Line Sensor Readings Chart (in assignment)
  • Submit program code, or give students printed code and have them explain each line: Objective 3, Objective 5
  • Give extra practice with functions that return
  • Give extra practice with 2-dimensional arrays
  • Code Tracing Chart as a debugging tool
  • Submit final line_sense.py program
  • Level-1 Mission 7 Review Kahoot!
β˜‘οΈ

Success Criteria

  • Create a color detection program using line sensors that detect brightness.
  • Display data on the console.
  • Use abs() in a comparison.
  • Break out of a loop if a condition is met.
  • Define and use a constant.
  • Define and use a matrix.
  • Get input from the user on the console.
  • Program the CodeBot to respond to a specific brightness level (color).
🧰

Classroom Materials

  • β–ΈComputer or Chromebook with internet access
🌍

Real-World Applications

From robot housekeepers to self-driving cars, the sensing and control techniques used in this mission apply to all kinds of intelligent systems. Analog sensors are non-contact sensors used in many industrial and commercial applications - code like this impacts your life every day!

πŸ“¦Automatic guided vehicles use this kind of code to zoom around warehouse distribution centers.
🌊Robots are used to clean up environmental waste, explore underground mines, and discover shipwrecks in the deepest oceans.
πŸš€

Extensions & Cross-Curricular

ExtensionAdd a button to stop the 'bot.
ExtensionAdd a beep when the correct color is found.
ExtensionLight up user LEDs to communicate what direction the CodeBot is facing. Flash the LEDs when the color is found.
Lang ArtsHave students write a summary of their project, using technical terms.
ScienceHave a lesson on how the line sensor works.
ScienceHave a lesson on brightness and reflectivity.
MathExperiment with different thresholds and line sensors. Make a chart of the results, and graph the data.
MathWhat if there wasn't a built-in abs() function? What are some alternative solutions to getting a positive answer every time?
πŸ”€

Vocabulary

β–Ύ
Line sensors - Photo reflective sensors that detect lines and boundaries beneath your 'bot.
Analog - Infinite variation, like from dark to light or cold to hot.
Int data type - A value that is an integer; designated by int in Python; can be positive or negative.
ADC - Analog to digital converter.
Constant - A name, like a variable, that represents data in your code that doesn't change.
Iterative process - Repeatedly taking small steps to build a whole solution.
Matrix - A list of lists; also known as a 2-dimensional array.
🐍

New Python Code

β–Ύ
reading = ls.read(num) left = ls.read(0) right = ls.read(4) Read the brightness level detected by a line sensor.
print(left, right, sep=',') Print numbers separated by a comma.
if 3547 < left < 3567: Threshold comparison operator to check if a value is within a range.
break Break out of a loop.
motors.enable(False) Stop the motors.
MIN_DIFF = 100 Define a constant (use all CAPS).
abs(left - right) Find the absolute value.
sensor_data = [ ['N', 3557], ['E', 286], ['S', 1391], ['W', 2481] ] Define a matrix (a list of lists).
target_dir = input("Enter direction: ") Prompt user for input using the console.
return d[0] Function return.
found = find_name(left) Call a function with a return.
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-DA-10 3A-DA-12 3A-AP-13 3A-AP-14 3A-AP-15 3A-AP-23

CSTA Standards - Grades 11-12

3B-CS-02 3B-DA-05 3B-DA-06 3B-AP-10 3B-AP-12 3B-AP-14 3B-AP-21 3B-AP-23

Certiport IT Specialist: Python Standards

1.1 1.2 1.3 1.4 2.1 2.2 3.2 4.1 4.2 5.1

PCEP: Certified Entry-Level Python Programmer

Section 1.1 Section 1.2 Section 1.3 Section 1.4 Section 1.5 Section 2.1 Section 2.2 Section 3.1 Section 4.1
πŸ“
Preparing for the Lesson
  • The Level-1 Mission 7 Assignment includes a chart for recording line sensor readings at each compass point, used in Objective 2 - have it ready to hand out.
  • Objective 6 requires importing the sleep() function from time - remind students before they start.
  • For Objective 6, students will zoom out in the simulator to find the water bottle, then open the console log and type the correct compass direction, using a capital letter, when the program runs. Walk through this once yourself first.

πŸ§‘β€πŸ«
Teacher Notes
  • During Objective 4, an if statement is added to the code. The existing if statement gets nested inside the new one, so make sure students are indenting properly.
  • A lot of code is added to Objective 6, and a lot is deleted. Students need to be very careful about their indenting, parentheses, typing, and spelling.
  • A lot of new concepts are introduced very quickly in this mission. Take time to review as needed, and give extra practice if students need it.
πŸ—ΊοΈ

Lesson Outline

πŸ—£οΈWarm-up / Hook

Have students write or discuss their answers before revealing them.

  • Question: What is a line sensor, and how could a robot use one to tell it has found a colored line or boundary?
  • Question: Why might a program check whether a number falls within a range, instead of checking for one exact value?
Teaching tip: Use a mix of discussion strategies to keep engagement high - write-then-share, think-pair-share, or randomly calling on students.
πŸ’‘Objective 1: Reading the Line Sensor

Students read a line sensor and print its brightness value, seeing how the CodeBot's analog to digital converter (ADC) turns light readings into numbers a program can use.

Give the quiz after Objective 1.

πŸ”’Objectives 2-3: Printing Data and Threshold Checks

Students print multiple sensor readings on the same line separated by commas, then use a threshold comparison operator to check whether a reading falls within a set range. Have students record their readings in the Objective 2 chart from the assignment.

Teaching tip: Give the quiz after Objective 3, and have students submit their code, or hand them printed code to explain each line.
πŸ”€Objectives 4-5: Breaking Out and Using abs()

Students add a break statement so the program exits the loop once a compass direction is found, then use the built-in abs() function to compare two sensor readings regardless of which one is higher, using MIN_DIFF as a constant threshold.

Teaching tip: The existing if statement gets nested inside a new if statement in Objective 4 - make sure students are indenting the code properly.

Have students submit their code, or hand them printed code to explain each line.

πŸ“Objective 6: Constants, Matrices, and Functions

A lot of new concepts come together here: defining a constant, building a matrix (a 2-dimensional array) of compass directions and readings, writing a function that returns a value, and using input() to get a direction from the console. Together these let the CodeBot respond to a specific color.

Teaching tip: A lot of code is added and a lot is deleted in this objective. Remind students to be careful with indenting, parentheses, typing, and spelling.
Teaching tip: To test, zoom out to find the water bottle in the simulator, then open the console log and type the correct direction using a capital letter.

Give the quiz after Objective 6.

🏁Bringing It Together: line_sense.py

Students combine reading the sensors, checking thresholds, breaking out of the loop, and the matrix and function logic into one complete program that finds and responds to a compass direction.

Have students submit their final "line_sense.py" program.

πŸ§‘β€πŸ€β€πŸ§‘Post-Mission Reflection

Use an extension or cross-curricular activity as a post-mission activity. New concepts were introduced quickly in this mission, so build in review time or extra practice with functions that return and with matrices as needed.

Wrap up with the Level-1 Mission 7 Review Kahoot!, which covers all Mission 7 objectives.