Teacher Resources: Python with Robots MS Mission 5-2

Mission 5-2: Fence Patrol Lesson 2 | Python with Robots MS
Mission 5-2 Lesson Plan

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.

⏰ 35-40 min 🎯 Grades 6-8 💻 CodeSpace 🥇 CodeBot 🐍 Python
View Lesson Outline
📋

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

☑️

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
🧰

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.

📝You can be given a list of chores to complete without step-by-step directions for each chore.
🚗You don't need to know how an engine works to drive a car.
📍You can follow simple directions to get to a destination without needing all the details.
🎶The chorus of a song is like a function that is called multiple times.
🚀

Extensions & Cross-Curricular

ExtensionChange the while loop to start at the left LED and sweep right, instead of right to left. Initialize the control variable to 5 and count down.
ExtensionPlay a tone when an LED turns on, or when an LED turns off.
MathThis program uses a function. Discuss what a function is in math. Compare and contrast functions in math vs. programming.
Lang ArtsHave students write about an abstraction used in their daily lives. Supports reading instructions, guided notes, and reflection writing.
🔤

Vocabulary

DRY - Don't Repeat Yourself - never write the same code twice.
Function - (Review) A named chunk of code you can run anytime just by calling its name; lets you reuse code without retyping it or copy/paste.
Parameter - (Review) A variable that gets its value when the function is called; part of the function definition.
Argument - (Review) A value that is passed to a function during a function call.
🐍

New Python Code

is_detected = ls.read(0) > thresholdAssign a Boolean value to a variable using an if statement.
while True:
  is_detected = ls.read(0) > threshold
  leds.ls_num(n, is_detected)
Use a Boolean variable to turn on/off an LED
Surface detectionDark line on light surface – use val > threshold
Light line on dark surface – use val < threshold
def detect_line(n):
  is_detected = ls.read(n) > threshold
  leds.ls_num(n, is_detected)
Define a function with a parameter for detecting a line
detect_line(0)Call a function that has a parameter for detecting a line
n = 0
while n < 5:
  detect_line(n)
  n = n + 1
While loop that repeats 5 times - uses n as the control variable to read each line sensor and turn on/off each LED
def scan_lines():
  n = 0
  while n < 5:
    detect_line(n)
    n = n + 1
A function that calls another function - scans all 5 line sensors
📐

Standards

CSTA Standards - Grades 6-8

2-AP-10 2-AP-11 2-AP-12 2-AP-13 2-AP-14 2-AP-16 2-AP-17 2-AP-19
📝
Preparing for the Lesson
  • 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.

🧑‍🏫
Teacher Notes
  • 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

💡Lesson Tips and Tricks
Teaching tip: You can use a variety of discussion strategies to get the most engagement from your students. For example, you can have students write their answers before asking anyone for an answer. You can use one of many think-pair-share methods. You can have students write their answer and share with someone, and then have other students share answers they heard from their peers. You can randomly select students to answer.
🗣️Warm-up / Hook

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?
💻Mission 5 Lesson 2 Activities

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.

Teaching tip: Mission Introduction - slides 3-5. This mission is divided into four lessons. The second lesson focuses only on the first and second goals.
Teaching tip: Objective #3 - slides 6-9. This objective uses a Boolean variable. Do you need to review Boolean? It shows a new way to assign a Boolean value to a variable. You could show the two examples on the board and talk about how the new way works.
Teaching tip: Objective #3 Activity - slides 10-11. The slides give the instructions and show the code. Students will need their Test Surfaces paper. You can also have a small black paper to use as the line to detect, as shown in the image on the slides.
Teaching tip: Objective #3 Activity (optional) - slide 12. This is an optional extension if you have time. It reverses the if statement for white on black. If students do this, they should change their code back to the original before proceeding.
Teaching tip: Objective #4 - slides 13-16. This objective discusses the use of functions to avoid code repetition. Review functions as necessary - how to define and call them. The concept was first introduced in Mission 4 Lesson 3.
Teaching tip: Objective #4 Activity - slides 17-19. Students do not start a new program - they use the same LineSense program from Lesson 1. When defining the function, students can cut their code from the while loop and paste it above in the function. Make sure they change the literal value 0 to the variable n. The program should work exactly the same as Objective 3.
Teaching tip: Objective #5 - slides 20-21. This objective discusses using another function with a loop to call the first function. You might need to explain that functions can call each other.
Teaching tip: Objective #5 Activity - slides 22-25. Students define another function in their program. Functions usually are grouped near the top. It can go above or below the current function (slide shows it below). Then students modify their main program while loop. The program can be tested the same way as Objective 4, but the black paper can go under any of the five line sensors.
Teaching tip: Quiz - slide 26. Students take a short quiz over objectives 1-5. You might want to review the data from Lesson 1 since there will be questions on the reflectivity values of different surfaces. At the end of this objective, students should have a working program to turn in.
Teaching tip: Extension. Two suggestions are given. These are completely optional, but good for students who finish early. If an extension is completed, students should use File - Save As and give the extension program a new name. The LineSense program will be continued in the next lesson.

🗽 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.

🧑‍🤝‍🧑Post-Mission Reflection

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.