Teacher Resources Python with Robots MS Mission 3-4

Mission 3-4: Navigation Challenge Lesson 4 | Python with Robots MS
Mission 3-4 Lesson Plan

Navigation Challenge Lesson 4: Button Controls & Algorithms

Students will use button presses to control robot movement and follow an algorithm to move the 'bot in a square pattern.

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

Overview

Mission 3-4 introduces students to button-controlled code and algorithm planning. Students learn to check for button presses using buttons.was_pressed(), implement a safety feature to prevent CodeBot from moving before they are ready, and use if/elif/else statements to branch code based on conditions. The lesson also introduces pseudocode algorithms as a planning tool and covers code readability through comments and whitespace.

🎯 Mission Goal: Students will use button presses to control robot movement and follow an algorithm to move CodeBot in a square pattern.

🎯

Learning Targets

  • I can check for a button press.
  • I can use an if statement for branching.
  • I can indent code inside a control flow.
  • I can implement a safety feature in code when the CodeBot moves.
  • I can write and follow an algorithm.
  • I can increase code readability by using comments and blank lines.
  • I can make the 'bot move in a square pattern.
  • I can understand the control flow of an if statement.
💡

Key Concepts

  • The push buttons can be used to control program flow. One way to do this is by enacting a safety feature, so code is only run after a button is pressed - keeping the 'bot from moving right when the program is run.
  • Branching with if/elif/else statements controls the flow of the program.
  • The colon (:) at the end of an if statement introduces a new block of code. Everything inside the block should be indented at the same level.
  • An algorithm is a useful tool for planning a program. In this lesson, pseudocode is used to plan the square navigation program.

Assessment Opportunities

☑️

Success Criteria

  • Use buttons.was_pressed() in an if statement
  • Implement a safety feature in code
  • Plan a program using an algorithm
  • Write code to drive CodeBot in a specific pattern
  • Use buttons.was_pressed() in an if/elif/else statement
🧰

Classroom Materials

  • CodeBot
  • USB cable
  • 4 AA batteries
  • Measuring stick
🌍

Real-World Applications

💡You've used some fundamental computer science and robotics principles - controlling motors with specific timing and sequencing, incorporating safety features, and programming different options depending on conditions. This code is used in cars, robots, electric toothbrushes, and more!
🤖Algorithms are part of our lives. We use algorithms all the time, for many of the tasks we complete each day. Have students think of a daily task, like getting ready for school or making a jump shot in basketball, and break it down into an algorithm.
🚀

Extensions & Cross-Curricular

ExtensionSuggested unplugged algorithm activities you can choose from: Algorithms lesson with drawings, Algorithms lesson with LEGO, PB&J Algorithm, or Hide and Seek.
MathHave a discussion about the algorithms used in math, and have students write algorithms for common math problems.
Lang ArtsSupports language arts through reading instructions and reflection writing.
🔤

Vocabulary

Algorithm - A list of instructions, in order, that the computer can follow to complete a task. A precise sequence of steps that the computer can follow exactly, one step at a time, to complete a task or solve a problem.
Comments - Notes in the code about what you are doing; ignored by the computer.
Whitespace - Adding blank lines and space around symbols to make the code more readable.
Control flow (branching) - Decision points in code; code takes a different branch depending on a condition.
Condition - A Boolean value (True or False), often the result of a comparison operator like <, >, or ==. Use an if statement, optionally followed by elif or else, for branching.
Indenting - A way to structure blocks of code by offsetting a block four spaces; blocks of code are indented following a defining statement with a colon (:).
🐍

New Python Code

buttons.was_pressed(0)Checks to see if BTN-0 was pressed; returns True or False
leds.user(0b00011000) sleep(10) leds.user(0) Countdown visual using LEDs - used inside the safety feature to give the student time to move into position
if buttons.was_pressed(0): {indented code} Safety feature in code that only executes indented code if Button-0 was pressed
if buttons.was_pressed(0): # turn right elif buttons.was_pressed(1): # turn left else: # stop Example of control flow, or branching, using if/elif/else
📐

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
  • All CodeBots will need batteries for this lesson. The 'bots will be moving around the room. Rechargeable batteries work fine.
  • CodeBots will need space to move. Dedicate some floor space in your room for students to test their code.
  • Decide what materials you want to use for presenting the lesson. The slides can be projected on a large screen. Choose either the slides or the instructions in CodeSpace - trying to use both could be confusing.
  • Be familiar with the mission log assignment and the questions they will answer. Prepare the assignment to give through your LMS.
  • Look over the unplugged activities to see if you want to use any of them. If so, decide when - will you use an activity to introduce algorithms, or at the end of the lesson after students have had some practice?

🧑‍🏫
Teacher Notes
  • This lesson covers objectives 9-11 in Mission 3. You do not need to use the instructions in CodeSpace. Some concepts are introduced in a different order than CodeTrek, but all material is covered and all goals for each objective will be met. Choose either the CodeSpace instructions or the slides - not both, because even the code used in the slides differs slightly from CodeTrek.
  • Algorithms are used throughout this lesson. There are many unplugged activities for practicing algorithms, found in the learning portal under "CS Unplugged."
🗺️

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.

  • Question: How do you move CodeBot forward?
  • Question: How do you rotate CodeBot clockwise?

The questions can help students focus on the programming process and review the previous lesson.

💻Mission 3 Lesson 4 Activities

Each student will complete a Mission Log. Students could work in pairs through the lesson, or individually. Students should have access to their Mission 3 Lesson 3 Log with the robot labs - they can use their data to help navigate CodeBot in a square.

💡 Teaching tip: Push Button Controls - slides 3-5
This information is added to Objective 9. It isn't in CodeSpace until Objective 10, but this is a good time to introduce it. Students learn about checking if a button is pressed and using this code to delay CodeBot movement until they are ready. The "safety feature" discussed here is new information but will be used in future lessons - it can really save students time and aggravation when running code at a testing station on the floor.
💡 Teaching tip: Push Button Controls - slides 6-7
Help students determine a good countdown time. If they are far from a testing station, they need more time; if close, they need less. The suggestion on the slides is 30 seconds, which should be plenty. You can reduce the sleep() amount for your students' environment.
💡 Teaching tip: Code with Push Buttons - slides 8-9
Students use the program from the last lesson (MoveOut) to try the safety feature code. They will need to indent their current code inside the if statement. A quick way to do this is to highlight the code and press <TAB> - that will indent all highlighted code at once. Model this for your students.
💡 Teaching tip: Objective 9 - slides 10-13
This is the beginning of the actual Objective 9. The objective starts with discussing an algorithm. You may want to do an additional unplugged algorithm activity - several suggestions are listed above.
💡 Teaching tip: Objective 9 Activity - slide 14
Students write an algorithm in their mission log. An algorithm is given in CodeSpace, and you can show it to the students, or have them come up with their own.
💡 Teaching tip: Objective 9 - slides 15-17
These slides cover the goal for this objective - have CodeBot move in a square. Direction and side length don't matter. Concepts from earlier are reintroduced here: comments and whitespace using blank lines. Students are also given the "divide and conquer" suggestion - do just one step at a time.
💡 Teaching tip: Objective 9 Activity - slides 18-22
Students should work on one side and rotation of a square. Once they get that, they can copy and paste the code three more times to have a complete square. The slides break down this process, with sample code on slides 20 and 21. Students will use the safety feature in this program.
💡 Teaching tip: Objective 10 - slides 23-24
This objective introduces button presses using both buttons. The terms "control flow" and "branching" are introduced.
💡 Teaching tip: Objective 10 Activity - slides 25-29
This activity uses a simple program that students will step through using the debugger. The code is given on slide 25. Students will "Step In" three separate times - first pressing BTN-0, second pressing BTN-1, third pressing no button - to see the control flow of the if statement. Once a condition is true, the indented code executes and the rest is skipped. Students record their observations in the mission log. The last slide summarizes the control flow of the if/elif/else statement.
💡 Teaching tip: Objective 11 - slides 30-31
This objective applies what students have learned about control flow and button presses. They can continue with the WhatIf program and add code to meet the goals. Slide 31 shows a possible algorithm for the completed program - the example uses one of their earlier Mission 3 programs (like SequenceLEDs or BinaryLEDs) so the code is already done and students can focus on the control structure. Students write their algorithm in the mission log.
💡 Teaching tip: Objective 11 Activity - slides 32-35
The first instruction for modifying the code is to add four lines to the safety feature. Next, students open previous programs and copy/paste code into the WhatIf program. Code for the square should already be indented. Code for the second branch (from a previous program) will not be indented - all lines inside the elif branch need to be indented. Remind students: highlight the code and press <TAB> as a shortcut.

🔑 Optional: Mission 3 Obj. 9-11 Kahoot! Review. A review Kahoot! is available for this lesson. You can do it together as a class or assign it independently. It includes two review questions that are frequently missed from an earlier Kahoot!

🧑‍🤝‍🧑Post-Mission Reflection

The post-mission reflection asks students to summarize the control flow of an if/elif/else statement. This was covered on slide 29 if students need a refresher.

  • You can use an extension or cross-curricular activity as a post-mission activity.
  • You can use the Mission 3 Obj. 9-11 Kahoot as a lesson review. (link)

End by collecting the Mission 3 Lesson 4 Log.