Teacher Resources: Python with Robots MS Mission 5-2
Fence Patrol Lesson 2: Functions and Line Sensing
Students will use functions to read data from CodeBot's line sensors and use the data to control the line sensor LEDs.
Overview
Mission 5-2 builds on Lesson 1's line sensor work by introducing functions as a way to organize and reuse code. Students learn to assign Boolean values using comparisons, use a Boolean variable to control an LED, and write functions that take parameters. They also write a function that calls another function using a while loop to scan all five line sensors.
🎯 Mission Goal: Students will define and use functions to read all five of CodeBot's line sensors and turn on or off the corresponding line sensor LEDs based on what the sensor detects.
Learning Targets
- I can assign a Boolean value to a variable using an if statement.
- I can use a Boolean variable to turn on/off an LED.
- I can define a function that reads a line sensor and uses the data to turn on/off an LED.
- I can define a function that uses a loop to call another function.
Key Concepts
- Use threshold comparisons to make decisions with sensor data.
- A Boolean variable can get its value directly from an if statement.
- A function can use a parameter to indicate which line sensor to read and LED to turn on/off.
- A function can call another function as part of its code block.
- A while loop that repeats a specific number of times can be used to call a function multiple times.
Assessment Opportunities
- Mission 5 Lesson 2 Log assignment
- Quiz after Objective 5
- Submit completed program LineSense
- Mission 5 Obj. 3-5 Review Kahoot!
Success Criteria
- Use an if statement to assign a Boolean value to a variable
- Use a Boolean variable to turn on/off an LED
- Define a function that reads a line sensor and then uses the data to turn on/off an LED
- Define a function with a while loop to call another function
Digital Resources
Classroom Materials
- ▸CodeBot Test Surfaces
- ▸CodeBot
- ▸USB cable
Real-World Applications
Functions are a form of abstraction, used everywhere in real-world software. Abstraction simplifies complex systems by focusing on essential features while hiding the details.
Extensions & Cross-Curricular
🔤
Vocabulary
▾
🐍
New Python Code
▾
is_detected = ls.read(0) > threshold
leds.ls_num(n, is_detected)Use a Boolean variable to turn on/off an LED
val > thresholdLight line on dark surface – use
val < threshold
is_detected = ls.read(n) > threshold
leds.ls_num(n, is_detected)Define a function with a parameter for detecting a line
while n < 5:
detect_line(n)
n = n + 1While loop that repeats 5 times - uses
n as the control variable to read each line sensor and turn on/off each LED
n = 0
while n < 5:
detect_line(n)
n = n + 1A function that calls another function - scans all 5 line sensors
📐
Standards
▾
CSTA Standards - Grades 6-8
- Decide what materials you want to use for presenting the lesson. The slides can be projected on a large screen.
- Be familiar with the mission log assignment and the questions they will answer. Prepare the assignment to give through your LMS.
- Have the Test Surfaces paper available for each student or programming pair.
- This lesson isn't very long - you should have time to do some review for the concepts used in earlier missions.
- If you have a word wall, or another form of vocabulary presentation, prepare the new terms.
- The slides should replace the instructions in CodeSpace. Code will be similar to CodeTrek, but a little different. All goals will be met.
- This lesson has students define and use functions. Consider whether you need to review this process with your class.
- You may want to review incrementing a variable and how a counter can be used to control a while loop for repeating a specific number of times.
- You may want to review using a variable for lighting LEDs.
Lesson Outline
Slide 2 - Students can write in their log first and then share, or discuss first and then write in their log. The warm-up questions review concepts from earlier lessons. Students can share their answers, or compare with each other.
- Question: What code reads a line sensor?
- Question: What do you remember about functions?
The Chrome browser works best, but other browsers also support CodeSpace. Each student will complete a Mission Log. Students could work in pairs through the lesson, or they can work individually.
🗽 Optional: Mission 5 Obj 3-5 Kahoot! Review. A review Kahoot! is available for the three objectives. You can do it together as a class, or assign it independently.
The post-mission reflection asks students to think about functions and their use in programming. It is an opinion question, so all answers should be valid. This is a good topic for class discussion.
You can use an extension or cross-curricular activity as a post-mission activity.
End by collecting the Mission 5 Lesson 2 Log.