Teacher Resources Python with Robots MS Mission 3-4
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.
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
- Mission 3 Lesson 4 Log
- Submit completed program NavSquare
- Submit completed program WhatIf
- Mission 3 Obj. 9-11 Review Kahoot!
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
Digital Resources
Classroom Materials
- ▸CodeBot
- ▸USB cable
- ▸4 AA batteries
- ▸Measuring stick
Real-World Applications
Extensions & Cross-Curricular
🔤
Vocabulary
▾
<, >, or ==. Use an if statement, optionally followed by elif or else, for branching.
:).
🐍
New Python Code
▾
📐
Standards
▾
CSTA Standards - Grades 6-8
- 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?
- 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
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.
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.
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.
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.
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.
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.
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.
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.
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.
This objective introduces button presses using both buttons. The terms "control flow" and "branching" are introduced.
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.
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.
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!
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.