VR - Mission 5: Dance Bot

Mission 5 Lesson Plan

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.

⏰ 2-4 hours 🎯 Grades 9-12+ πŸ’» CodeSpace πŸ€– Virtual CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

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
🌍

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.

πŸ”Look up definitions of abstraction, then come up with your own. Look for examples in everyday life, like how a car works: you don't need to know how the engine or gas pedal work to drive one.
🎬While loops, for loops, and functions are used constantly in animation and automation. Discuss real-world animations or automations with repetitive tasks that could be simplified using loops.
πŸ“‹Algorithms are step-by-step instructions to complete a task, and they show up throughout daily life. Think about a routine you follow every day - what's the algorithm for it?
πŸš€

Extensions & Cross-Curricular

ExtensionReturn to an earlier mission and have students define functions for repeated code.
ExtensionHave a competition to see which 'bot can touch the most balloons.
ExtensionUse a loop in the main program to repeat calls to the go() and sweep() functions.
ExtensionUse the sandbox to synchronize dance moves to music, or move balloons around the same dance floor.
Lang ArtsHave students write the algorithm to their dance as numbered instructions.
ScienceWrite the algorithm to the scientific method.
MathWrite the algorithm to solve an equation.
Physical ScienceExperiment with wheel speed in the sandbox. Try different speeds, note the distance traveled in tile squares, graph the results, and determine the rate using distance = rate x time.
πŸ”€

Vocabulary

β–Ύ
Sequential - Running code in order (sequence), one line at a time.
Loop - Repeating a section of code, subject to a condition.
Indenting - Structuring blocks of code by offsetting them four spaces; blocks are indented following a statement with a colon (:).
While loop - Repeats a block of indented code while a condition is true.
Variable - A label, or name, for a container that stores information for a computer to use.
Initialize - The first, or initial, value assigned to a variable.
Increment - Updating a variable by adding 1 to it, like counting.
Bug - A problem with the code where it doesn't do what you expect.
Debugger - An advanced CodeSpace tool that gives more control over a program being debugged, with step next, step into, and step out buttons, plus Locals and Globals variable tracking.
String formatting - Changing how printed text is formatted using key arguments, like end=',' or end=' ' to add a comma or space after each printed item.
For loop - Repeats a section of code across a range of numbers or items in a list. It automatically initializes and updates the loop variable.
Readability - Code that is easy to understand, like using blank lines to separate sections of code.
Algorithm - A precise sequence of instructions the computer can follow exactly, one step at a time, to complete a task or solve a problem.
Function - A named chunk of code you can run anytime just by calling its name - a way to reuse code without retyping it. The code doesn't run until you call it.
Editor shortcuts - Keyboard hotkeys, or combinations of keys, that let you write code faster.
Iterating - Moving through a sequence, like in a for loop or list, one item at a time.
Parameter - A variable defined as part of a function definition that receives an argument (value) from the function call.
🐍

New Python Code

β–Ύ
count = 0 while count < 8: leds.user_num(0, True) sleep(0.1) leds.user_num(0, False) sleep(0.1) count = count + 1 While loop for repeating code while a condition is true.
count = 0 count = count + 1 Initialize a variable with the assignment operator (=), then increment it by adding 1.
print(count) print(count, end=' ') Display a message on the console. The panel must be open to see the message.
for count in range(8): print(count) leds.user_num(count, True) sleep(0.1) leds.user_num(count, False) sleep(0.1) For loop with the range() function. This example also uses the loop variable (count) as part of an instruction.
for count in range(6, -1, -1): print(count) leds.user_num(count, True) sleep(0.1) leds.user_num(count, False) sleep(0.1) For loop with the full range() function and optional arguments to count backwards.
def sweep_left(): for count in range(8): print(count) leds.user_num(count, True) sleep(0.1) leds.user_num(count, False) Define a function. The indented code following the colon (:) runs when the function is called.
sweep_left() sweep_right() Function calls. Notice there's no colon (:), but the call does include parenthesis (). The function call is not indented.
buttons.is_pressed(0) buttons.was_pressed(0) not buttons.was_pressed(0) Check the current or last state of a button press. not returns the opposite - if was_pressed(0) is True, the statement returns False.
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-AP-13 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-19 3A-AP-21 3A-AP-22 3A-AP-23

CSTA Standards - Grades 11-12

3B-CS-02 3B-AP-10 3B-AP-14 3B-AP-16 3B-AP-17 3B-AP-20 3B-AP-22 3B-AP-23

Certiport IT Specialist: Python Standards

2.2 4.1 4.2 5.1

PCEP: Certified Entry-Level Python Programmer

Section 1.1 Section 1.2 Section 4.2
πŸ“
Preparing for the Lesson
  • 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.

πŸ§‘β€πŸ«
Teacher Notes
  • 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

πŸ—£οΈWarm-up / Hook

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?
Teaching tip: Use a mix of discussion strategies to keep engagement high - write-then-share, think-pair-share, having students share what they heard from a peer, or randomly calling on students.
πŸ’»Objective 1: Ah One-Two-Three!

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.

Teaching tip: Review incrementing a variable before students start - a counter that increases inside the loop is what eventually makes the while condition false.
🐞Objective 2: Enter the Debugger

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.

Teaching tip: Have students fill out a Code Tracing Chart while using the debugger, then discuss it together as a class.

Give the quiz after Objective 2.

πŸ”Objectives 3-4: For Loops & the Wave Begins

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

⬅️Objective 5: Complete the Wave

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.

🧩Objective 6: Funky Functions

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.

Teaching tip: This is a great moment to have students return to an earlier mission and turn a repeated block of code into a function.

Give the quiz after Objective 6.

πŸ”˜Objective 7: Just Waiting for a Button

Students use while not buttons.was_pressed(0): to keep sweeping the LEDs and calling sweep_left() / sweep_right() until BTN-0 is pressed.

Teaching tip: Use an exit ticket to have students explain the difference between is_pressed() and was_pressed().
πŸ’ƒObjective 8: Beautiful Moves!

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.

πŸ§‘β€πŸ€β€πŸ§‘Post-Mission Reflection

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.