VR - Mission 5: Dance Bot
Dance Bot
Students use while loops, for loops, and custom functions to flash and sweep CodeBot's LEDs, then drive the 'bot around the floor to pop balloons in a dance.
Overview
Mission 5 walks students from a basic while loop all the way to a fully choreographed Dance Bot. Along the way they learn the CodeSpace debugger, print messages to the console, flash LEDs with for loops and loop variables, sweep LEDs backwards with optional range() arguments, and define their own functions to make that sweeping code reusable. The mission wraps up by combining a button press, the sweeping LEDs, and CodeBot's wheels so students can drive their 'bot around the floor and pop balloons while it dances.
π― Mission Goal: Students will use functions and loops to create a dancing CodeBot.
Learning Targets
- I can create a new file for code.
- I can use a while loop to flash the user LEDs.
- I can use the debugger to step through a program and track variables.
- I can print a message to the console.
- I can use a for loop to flash the user LEDs.
- I can use a loop variable to control a user LED.
- I can use a for loop with optional arguments to sweep LEDs backwards.
- I can define and use functions for sweeping LEDs.
- I can use a button to control the 'bot.
- I can move the CodeBot using the sweeping LEDs as the time element.
Key Concepts
- Computers execute code in sequential steps.
- The CodeSpace debugger lets you step through the code one line at a time to understand what the computer is doing.
- Loops allow a block of code to be repeated without typing it again. Python has two types of loops: the while loop and the for loop.
- 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.
- Increments (and decrements) are used for updating variables like counters.
- A function is a named chunk of code you can run anytime by calling its name.
- Button presses (inputs), LEDs (outputs), and speaker sounds (outputs) are part of the user interface. They allow the user to interact with the CodeBot.
Assessment Opportunities
- Quiz after Objective 2
- Quiz after Objective 6
- Submit program code, or give students printed code and have them explain each line: Objective 1, Objective 5
- Code Tracing Chart as a debugging tool
- Return to an earlier mission and have students define functions for repeated code
- Exit ticket: explain the difference between the two ways to check the state of a button
- Submit final "dancebot.py" program
- Level-1 Mission 5 Review Kahoot!
Success Criteria
- Use a while loop to flash user LEDs
- Use a for loop and loop variable to flash user LEDs
- Use two for loops to sweep across the LEDs
- Define functions for sweeping left and right
- Call the functions in the main program
- Check the state of a button press
- Use a while loop to sweep the LEDs until button 0 is pressed
- Move the 'bot around the surface and touch at least 5 balloons while sweeping the LEDs
Digital Resources
- βΈLevel-1 Mission 5 Assignment
- βΈLevel-1 Mission 5 Assignment Answers
- βΈLevel-1 Mission 5 Review Questions
- βΈLevel-1 Code Tracing Chart
- βΈLevel-1 Mission 5 Review Kahoot!
- βΈMission 5 Obj 2 Code Solution
- βΈMission 5 Obj 5 Code Solution
- βΈMission 5 Obj 6 Code Solution
- βΈMission 5 Final Code Solution
Classroom Materials
- βΈComputer or Chromebook with internet access
Real-World Applications
Abstraction is a key concept of computer science. Functions are a form of abstraction because they hide the details of how a task is accomplished, which lets you focus on the parts of a problem that need attention.
Extensions & Cross-Curricular
π€
Vocabulary
βΎ
end=',' or end=' ' to add a comma or space after each printed item.
π
New Python Code
βΎ
=), then increment it by adding 1.
range() function. This example also uses the loop variable (count) as part of an instruction.
range() function and optional arguments to count backwards.
not returns the opposite - if was_pressed(0) is True, the statement returns False.
π
Standards
βΎ
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
Certiport IT Specialist: Python Standards
PCEP: Certified Entry-Level Python Programmer
- Review incrementing a variable and how a counter can be used to control a while loop, since Objective 1 (Ah One-Two-Three!) introduces both for the first time.
- Review using a loop variable to control an LED before Objectives 3-4, where students convert their while loop to a for loop and sweep the LEDs.
- Have the Code Tracing Chart ready to share - it's most useful once the debugger and for loops are introduced.
- Skim the Toolbox entry on functions before Objective 6 (Funky Functions), in case students need a refresher on defining and calling one.
- There is a lot of information in this mission - take your time on each objective and give extra practice or review as needed.
- Functions are a form of abstraction. This key concept isn't explicit in the instructions, but this mission is a good place to start the conversation about abstraction and how it's used in a program - see Real-World Applications for ideas.
- Use the quizzes after Objective 2 and Objective 6 to check for understanding along the way.
- Have students fill out a Code Tracing Chart during one of the objectives and discuss it as a class.
- The final objective, Beautiful Moves!, can be frustrating because the balloons and the 'bot don't perform the same way every time. This is a good time for students to work in pairs and problem-solve together - remind them to try the same code more than once, since it may reach the goal on a later run.
Lesson Outline
Have students write or discuss their answers before revealing them.
- Question: What does it mean for a computer to execute code "sequentially"?
- Question: Why would you want to repeat a block of code instead of typing it over and over?
Students create a new file called "dancebot.py," then use a while loop and a counter variable to blink User LED-0 exactly 8 times. Have students submit their code, or hand them printed code to explain each line.
Students print the running count to the console, then use the CodeSpace debugger (step next, step into, step out) to step through their while loop line by line and track variables under Locals and Globals.
Give the quiz after Objective 2.
Students replace their while loop with a for loop and range(8) to blink the same LED - the for loop handles initializing and updating the counter automatically. Then they use the loop variable itself to control which LED is lit, sweeping a single LED from right (LED-0) to left (LED-7).
Students add a second for loop using the full range(start, stop, step) with optional arguments to sweep the LED back from left to right, completing the wave. Have students submit their code, or hand them printed code to explain each line.
Students package their two sweep loops into sweep_left() and sweep_right() functions, then call them in order so the wave keeps going without repeating code.
Give the quiz after Objective 6.
Students use while not buttons.was_pressed(0): to keep sweeping the LEDs and calling sweep_left() / sweep_right() until BTN-0 is pressed.
Students enable the motors and write go() and sweep() functions, then choreograph a dance - using the LED sweep as the timing element - that touches at least 5 balloons within 30 seconds. Students can work in pairs to problem-solve if the 'bot doesn't hit the goal on the first few tries.
Have students submit their final "dancebot.py" program.
Use an extension or cross-curricular activity as a post-mission activity.
Wrap up with the Level-1 Mission 5 Review Kahoot!, which covers all Mission 5 objectives.