Teacher Resources: Python with Robots MS Mission 4-2

Mission 4-2: Animatronics Lesson 2 | Python with Robots MS
Mission 4-2 Lesson Plan

Animatronics Lesson 2: Counting Guests

Students count the number of guests using a button press and display the count using line sensor LEDs.

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

Overview

Mission 4-2 builds on the infinite loop from Mission 4-1. Students write a CountGuests program that uses a button press to increment a counter variable and display the count by turning on line sensor LEDs. They also add a speaker beep for each press and learn to debounce the button to prevent false counts.

🎯 Mission Goal: Students will write a CountGuests program that counts button presses, displays the running count using line sensor LEDs, beeps on each press, and breaks the loop when the count reaches 5.

🎯

Learning Targets

  • I can increment a counter variable.
  • I can use buttons.was_pressed() in an if statement to control program flow.
  • I can display a number using line sensor LEDs.
  • I can break a loop.
  • I can add a beep to a program.
  • I can debounce a button press.
💡

Key Concepts

  • Increments (and decrements) are used for updating variables like counters.
  • Button presses (inputs), LEDs (outputs) and speaker sounds (outputs) are part of the user interface. They allow the user to interact with the CodeBot.
  • A button press can "bounce," or detect an additional press that didn't happen. Using an extra buttons.was_pressed() after a short delay can debounce a button press.
  • A number can be displayed by using LEDs.

Assessment Opportunities

☑️

Success Criteria

  • Increment a variable to count a button press
  • Use a variable to turn on a line sensor LED
  • Add a beep when a button is pressed
  • Debounce a button press
🌍

Real-World Applications

🏭In manufacturing, sensors count parts on an assembly line to track production and flag errors.
🏟In retail, counters track customers entering stores or monitor how many items remain on shelves.
🌿In agriculture, sensors help with crop monitoring and livestock counting for efficient farm management.
🏥In healthcare, automated counters analyze blood cells faster and more accurately than manual methods.
🌏In smart cities, sensors control traffic flow and monitor pedestrian movement to improve safety.
🚀

Extensions & Cross-Curricular

ExtensionUse the user LEDs to display the number of guests. Go up to 8 guests before breaking the loop.
ExtensionUse the user LEDs to display the number of guests. Get a random number between 5 and 8 to break the loop, so the number of guests can be different.
ExtensionChange the tone of the beep with every button press by adding another variable.
MathWhen programming, you can increment a variable with something other than 1. Incrementing by 2 gives you all the even numbers - incrementing by other values is like skip counting. Practice skip counting, and then think about what the code would look like. What would the initial value be?
Lang ArtsWrite about a real-world application that counts things using some kind of sensor or input. Supports language arts through reading instructions, guided notes, and reflection writing.
ScienceLearn about the mechanics of a button - how pressing it completes a circuit and sends a signal to the microcontroller.
🔤

Vocabulary

Break - Exit the nearest enclosing loop.
Increment - Increase by one.
Debounce - Reset the internal status of a button so the press isn't counted twice.
🐍

New Python Code

breakBreak out of a loop
leds.ls_num(n_guests, True)Turn on a line sensor LED, using a variable to indicate which LED
n_guests = n_guests + 1Increment a variable
spkr.pitch(440)Play a tone on the speaker; the argument is the pitch frequency in Hertz
spkr.off()Turn off the speaker (usually after a short delay)
buttons.was_pressed(0)Debounce a button press by resetting the internal status (after a delay)
📐

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.
  • Look over the quiz in advance, and prepare for potential questions students may have.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍🏫
Teacher Notes
  • RECOMMENDATION: Use the slides instead of instructions in CodeSpace and CodeTrek. This mission is divided into 4 lessons that cover all the information, but chunked more. All goals will still be met, but students are asked to create a new program and only focus on the new material. The programs will be combined in Lesson 4. If you choose to use the instructions in CodeSpace and the CodeTrek, then don't use the slides.
  • On Objective 5, students test the program by pressing the button and lighting up line sensor LEDs. They may find that a single press lights up more than one LED. I recommend bringing this up. Then see if the same thing happens for Obj. 6. This action is called "bouncing." Students learn about and fix this in Obj. 7.
  • The quiz after Obj. 7 includes a question that may be difficult. The third question has a while loop with the condition i < 3. In this problem, the i variable is incremented outside the loop. It never changes, so the loop becomes infinite.
🗺️

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. These warm-up questions review code from Mission 4 Lesson 1. Students can share their answers, or compare with each other.

  • Question: What line of code increments a counter?
  • Question: How can you avoid a ValueError?
💻Mission 4 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 up into four lessons. The second lesson focuses only on the second goal. These slides review the task and goal. Students answer two questions in their mission log.
Teaching tip: Objective #4 - slides 6-9. Students start a new file. This isn't part of the instructions in CodeSpace or CodeTrek, but this way students can focus on the new concepts and not worry about a program getting too long too early. The code is given on slide 9. The program starts with blinking a single LED (like Objective 1) and breaking the loop.
Teaching tip: Objective #5 - slides 10-16. Follow the slides instead of the instructions in CodeSpace or CodeTrek. Students turn on a line sensor LED and increment the counter variable before breaking the loop. Students will delete the blinking LED code and then add to the if statement. All code is given on the slides. When students run their code, they may notice that a single button press lights up more than one line sensor LED - this is "bouncing." You can discuss or acknowledge it; students will learn the fix in Objective 7.
Teaching tip: Objective #6 - slides 17-19. Students add a beep to the if statement for every button press. Two functions are introduced that play a tone and stop the speaker. Students need to code a delay between the two functions.
Teaching tip: Objective #7 - slides 20-25. Slides 20-22 discuss button bouncing. Slides 23-24 give the algorithm for the solution. Students just need to add one line of code. At the end, students should have a working CountGuests program - it counts button presses, displays the number on line sensor LEDs, beeps for each press, and breaks the loop when the count reaches 5.
Teaching tip: Quiz - slide 26. Students take a short quiz over objectives 1-7. Watch for the third question, which has a while loop with i < 3 as the condition but i is incremented outside the loop - making it infinite.
Teaching tip: Extensions - slide 27. If students do an extension, suggest they do a File - Save As first to preserve their original code, which they will use later. A code solution for all three extensions is in the teacher resources above.

🗝️ Optional: Mission 4 Obj 4-7 Kahoot! Review. A review Kahoot! is available for these four objectives. You can do it together as a class or assign it independently. (link above)

🧑‍🤝‍🧑Post-Mission Reflection

The post-mission reflection asks students to review counting guests and debouncing button presses. You can use an extension or cross-curricular activity as a post-mission activity.

End by collecting the Mission 4 Lesson 2 Log.