VR - Mission 6: Robot Metronome

Mission 6 Lesson Plan

Robot Metronome

Students build an interactive metronome that flashes LEDs and plays a tone in time, then add a toggle for sound and a list of selectable tempos.

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

Overview

Mission 6 combines loops, branching, Booleans, and lists into one project: a working metronome. Students start by flashing the user LEDs and playing a tone once per second, then use an equation to calculate timing, add an if statement to control the sound, and use the 'not' operator to toggle sound on and off with a button. From there, students use a bit-shift operator to light up individual LEDs and build a list of five tempos that wraps around when a button cycles past the end of the list.

🎯 Mission Goal: Students create an interactive metronome with LED indicators, sound, and tempo selection.

🎯

Learning Targets

  • I can create a simple metronome by flashing all user LEDs and playing a tone, once per second.
  • I can update a variable using a mathematical equation.
  • I can use an if statement to control program flow.
  • I can use the logical operator not to toggle a Boolean variable.
  • I can use a bit-shift operator to control LEDs.
  • I can define and use a list of tempos.
  • I can wrap around an index variable to start over when the length of the list is reached.
πŸ’‘

Key Concepts

  • Infinite while loops are used to execute an algorithm continuously.
  • Iterating with a while loop or a for loop reduces the need for repeated code.
  • Both loops are versatile and can iterate forward or backward.
  • Branching with an if: statement controls the flow of the program.
  • A Boolean variable can be used as a toggle, or on/off switch.
  • A bit shift operator is used to change which LED lights up.
  • A list can hold multiple values.
  • Each item in a list is in order and is accessed using an index.
  • The state of a button press is True or False and can be used to control the CodeBot.
βœ…

Assessment Opportunities

  • Quiz after Objective 4
  • Quiz after Objective 8
  • Submit program code, or give students printed code and have them explain each line: Objective 4, Objective 7
  • Give extra practice with logical operators
  • Give extra practice with lists
  • Code Tracing Chart as a debugging tool
  • Submit final metronome.py program
  • Level-1 Mission 6 Review Kahoot!
β˜‘οΈ

Success Criteria

  • Use a while loop to flash LEDs
  • Use two for loops to flash LEDs
  • Use an infinite loop to flash LEDs continuously
  • Toggle a Boolean variable to control sound and the power LED
  • Use a list for five tempos
  • Wrap around the list for continuous selection
🌍

Real-World Applications

The concepts in this lesson show up in real-world technology all the time: continuous looping, LEDs as indicators, lists, and toggles for on/off. Challenge students to come up with their own examples of each.

πŸ”Continuous looping: a turn signal, a crosswalk countdown, and a heart rate monitor all repeat a pattern until something stops them.
πŸ’‘LEDs as indicators: status lights on a router, a charging cable, or a game controller tell you what state a device is in, just like the metronome's power LED.
πŸ“‹Lists and toggles: playlists, contact lists, and channel guides that wrap around from the last item back to the first all work like the tempo list.
πŸš€

Extensions & Cross-Curricular

ExtensionWrite the algorithm for the program before coding it.
ExtensionAdd a function to the program.
ExtensionAdd more tempos to the list. Only five can show on the line sensor LEDs, so have students come up with a solution.
Lang ArtsHave students compare and contrast the user LEDs with the line sensor LEDs, or compare and contrast a while loop with a for loop.
ScienceRun an experiment with the metronome. Is it easier to follow sound only, lights only, or both? Have students design their own experiment.
MathThe mission shows how to assign a variable a value from an equation. Write a program with different equations, get solutions, then graph the data.
πŸ”€

Vocabulary

β–Ύ
Intervals - Time-related functions available in Python to track time. A common function for a delay is sleep(). Common functions for checking the current elapsed time are time.time() and time.ticks().
Infinite loop - A loop that doesn't end because the loop condition is always true.
Literal - An actual value, like 1 or "hello" or True.
Variable - A name to which you assign some data, any type of information your program uses. It must be defined before it is used.
State - The status of a system with transitions. Your program can only be in one of a known set of states at any given time.
Transition - Moving between states. A program can transition from one state to another when certain conditions are met.
Boolean data type - A data type that has two values: True or False.
Not (logical operator) - A logical operator that needs only one Boolean operand and inverts it. It can be used to toggle a Boolean variable.
Branching (control flow) - Decision points where code takes a different path depending on a condition.
Data type - The kind of value stored in a variable. Common built-in data types are str (string), int (integer), float (decimal number), and bool (Boolean).
List data type - A sequence of items that you can access with an index.
Index - The position of an item in a list. The first index of a list is 0.
Logical operators - Operators that compare multiple conditions: 'or' and 'and'.
Magic number - A literal value used directly in code.
Single equals (=) - Assignment - used to assign a value to a variable.
Double equals (==) - A comparison operator used to determine if two objects are the same.
🐍

New Python Code

β–Ύ
while True: leds.user(0b11111111) spkr.pitch(784) sleep(0.1) spkr.off() leds.user(0) sleep(0.9) Infinite loop. It does not stop because the loop condition is always true - the program must be stopped manually by clicking the STOP button.
pause = (60/tempo) - beat_duration Assign a value to a variable using a mathematical equation.
sound_on = True A Boolean variable.
if sound_on: spkr.pitch(784) An if statement (branching), also known as control flow.
if buttons.was_pressed(0): sound_on = not sound_on Toggle a Boolean variable using the not operator.
leds.pwr(not sound_on) Use the opposite of a Boolean to turn something on or off.
leds.ls(1 << tempo_select) A bit-shift operator - shifts a bit to the left by the index amount to control which LED lights up.
tempo_list = [50, 70, 100, 140, 180] Define a list.
tempo = tempo_list[tempo_select] Access an item from a list using a variable as the index.
len(tempo_list) The len() function returns the number of items in a list.
if tempo_select == len(tempo_list): tempo_select = 0 Wrap around an index variable to start over when the length of the list is reached.
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 9-10

3A-CS-03 3A-DA-11 3A-AP-13 3A-AP-14 3A-AP-15 3A-AP-16 3A-AP-18 3A-AP-19 3A-AP-21 3A-AP-23

CSTA Standards - Grades 11-12

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

Certiport IT Specialist: Python Standards

1.1 1.2 1.3 1.4 2.1 2.2 4.1 5.1

PCEP: Certified Entry-Level Python Programmer

Section 1.1 Section 1.2 Section 1.3 Section 1.4 Section 2.1 Section 2.2 Section 3.1
πŸ“
Preparing for the Lesson
  • Review Mission 3's binary LED pattern before Objective 1 - the first objective uses a similar pattern to turn on all user LEDs.
  • Review Mission 4's speaker and tone code before Objective 2, as needed.
  • Review the difference between = and == before students start writing conditionals.

πŸ§‘β€πŸ«
Teacher Notes
  • When an infinite loop is used, the program will not stop automatically - it must be stopped manually by pressing the STOP button.
  • There is a lot going on in this mission. Take your time and don't rush through the objectives.
  • Create worksheets or extra activities as needed to review throughout the objectives - extra practice with logical operators and with lists is suggested in Assessment Opportunities.
πŸ—ΊοΈ

Lesson Outline

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

Have students write or discuss their answers before revealing them.

  • Question: What is a real metronome used for, and how does it keep a steady beat?
  • Question: Why would a loop that never stops on its own be useful in a program?
Teaching tip: Use a mix of discussion strategies to keep engagement high - write-then-share, think-pair-share, or randomly calling on students.
πŸ’‘Objectives 1-3: Building the Beat

Students flash all user LEDs in a binary pattern and play a tone, reviewing techniques from Mission 3 and Mission 4, to build one beat of the metronome. They then wrap that beat in an infinite while loop so it runs continuously, once per second, and use an equation to calculate the pause between beats.

Teaching tip: Review the binary LED pattern from Mission 3 and the speaker/tone code from Mission 4 before students start.
πŸ”€Objective 4: If Statements

Students add an if statement so the speaker only plays a tone when a Boolean variable is True, introducing branching and control flow. Have students submit their code, or hand them printed code to explain each line.

Teaching tip: Review the difference between = and == here if students mix them up.

Give the quiz after Objective 4.

πŸ”˜Objectives 5-6: Toggling Sound

Students use a button and the not operator to toggle the sound_on Boolean, then use the opposite of that Boolean to control the power LED as a visual on/off indicator.

Teaching tip: Give extra practice with logical operators here if students need it - this is a common sticking point.
πŸ“‹Objectives 7-8: Bit-Shift and Lists

Students use a bit-shift operator to light up a single line sensor LED based on an index variable, previewing how the tempo selection will be displayed. Have students submit their code, or hand them printed code to explain each line. They then define a list of five tempos, use the index variable to select a tempo from the list, and add a wrap-around check so the index resets to 0 after the last item in the list.

Teaching tip: Give extra practice with lists here, and use the Code Tracing Chart as a debugging tool if students get stuck tracing the index variable.

Give the quiz after Objective 8.

πŸ₯Bringing It Together: metronome.py

Students combine the flashing LEDs, tone, sound toggle, and tempo list into one complete metronome program. Have students submit their final "metronome.py" program.

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

Use an extension or cross-curricular activity as a post-mission activity.

Wrap up with the Level-1 Mission 6 Review Kahoot!, which covers all Mission 6 objectives.