AP CSP CodeX - Mission 6: Heartbeat (Obj. 1-7)

Mission 6 Obj 1-7 Lesson Plan

Mission 6: Heartbeat (Obj. 1-7)

Students use an infinite loop to animate a beating heart on the CodeX, then add a kill switch to stop the program.

⏰ 45 min 🎯 Grades 9-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

In Mission 6, students give the CodeX a beating heart. They use images and the sleep command to animate a heartbeat, then put that code in an infinite while True: loop so it repeats forever. Students learn how to stop an infinite loop with a "kill switch" that breaks out of the loop when a button is pressed, and they use the debugger to step through the code. After Objective 7, students save the program as Heart2_functions, put the heartbeat code in a function, and call that function in the loop.

🎯 Mission Goal: Students will use images in an infinite loop to create a beating heart animation.

🎯

Learning Targets

  • I can create an infinite loop to make my code more efficient.
  • I can code a "kill switch".
💡

Key Concepts

  • Infinite loops - when a condition is always True.
  • A function can be used anywhere, including the code in an infinite loop.
  • Readability is very important in coding. Whitespace can help, and comments are essential.
✅

Assessment Opportunities

☑️

Success Criteria

  • Create a program that shows a beating heart using an infinite loop
  • Include a "kill switch" to stop the program (BTN_U)
  • Create and call a function for the beating heart animation
📁

Digital Resources

🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
🌍

Real-World Applications

🚧Ask students if they have seen flashing road signs. Can they think of other continuously blinking or repeating indicators? How can computers make this more efficient?
🔘Embedded computers monitor push buttons to control things like speed UP and DOWN. That is real-world code used in many applications you see around you.
📺TV remotes, game controllers, push button light dimmers, and more.
🚀

Extensions & Cross-Curricular

ExtensionStudents who finish early can swap in other built-in images, like pics.HAPPY or pics.MUSIC, to make a different animation in the loop.
ExtensionStudents change the sleep delay, predict how the heartbeat will look, and then run the program to check their prediction.
MathStudents take their pulse, work out the seconds per beat (60 divided by beats per minute), and use that to choose a sleep delay.
Lang ArtsStudents write a short journal entry that explains, in their own words, the difference between a loop and an infinite loop.
🔤

Vocabulary

▾
Loop - Repeats a block of code, subject to a given condition.
While loop - Repeats a block of indented code as long as the condition is true.
Infinite loop - A loop that never ends because the loop is always true.
🐍

New Python Code

▾
while True: # Indent code to loop display.show(pics.HEART) sleep(delay)Infinite while loop.
if buttons.was_pressed(BTN_A): breakBreak out of a loop.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-AP-15 3A-AP-17 3A-AP-18

CSTA Standards - Grades 11-12

3B-CS-02 3B-AP-14 3B-AP-16
📝
Preparing for the Lesson
  • The assignment is best completed digitally. Prepare the assignment to give through your LMS.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.
  • Have the Mission 6 Kahoot Review ready in case you have time at the end of the lesson.

🧑‍🏫
Teacher Notes
  • Mission 6 may take a little over a class period, so it is divided into two lessons.
  • After Objective 7, students continue with the code to create and use a function. This goes beyond the instructions in CodeSpace. Students can stay in Obj. 7 or move to the sandbox. Before creating the function, we recommend saving the program with a new name to keep the original code. This step is not vital to the program.
  • If you have time at the end of the lesson, use the Mission 6 Kahoot Review. The first nine questions are covered in Objectives 1-7. You can hold off on the rest of the review until after Mission 6 is completed.
  • Refer to the lesson plans in the Python with CodeX Mission Pack Curriculum for additional challenges, cross-curricular suggestions, and additional information.
🗺️

Lesson Outline

This outline gives the narration for one way to present the lesson.

🗣️Warm-up (5 minutes)

Use a discussion strategy, like journaling, working at boards, selecting random students, or a form of think-pair-share.

Ask students if they have seen flashing road signs. Can they think of other continuously blinking or repeating indicators? How would you make something repeat forever with code?

💻Activity - Mission 6 Objectives 1-7 (40 minutes)

Randomly group students into pairs for pair programming, or they can work individually.

Students log in to one computer. Two computers can be used if they want to have the activity guide open on one computer and CodeSpace on the other.

Students go to make.firialabs.com and should be at the beginning of Mission 6.

Teaching tip - Objective 2: CodeTrek will remind students about adding the sleep command, but they still need to replace the #TODO with the actual code for displaying a small heart. It does not show the code for that.
Teaching tip - Objective 3: At this point, we suggest introducing students to features of the Editor they may not be aware of, such as cut, copy, paste, and undo. There is a Toolbox tool covering these "Editor Shortcuts", and they are also explained in a video. The video uses an older version of CodeSpace. A couple of other handy editor shortcuts are undo, indent, and comment/uncomment. These shortcuts are on the assignment. Comment/uncomment is extremely helpful for commenting out large sections of code, especially during testing.
Teaching tip - Objective 4: You can demonstrate this keyboard shortcut: to quickly indent code that is already typed, highlight the code and press TAB. To un-indent, press SHIFT-TAB. Remind students that they only need ONE heartbeat cycle in the loop, so they should delete all the extra code they added for Objective 3.
Teaching tip - Objective 7: This objective has students use the debugger. You may need to demonstrate this or remind them how to use it.
Teaching tip - After Objective 7: For extra practice, students should create a function for the code inside the while True: loop (not the button press). Then they call the function in the infinite loop. Students should be able to do this step on their own. We recommend doing a "Save As" and renaming the file Heart2_functions, but if they do not, that is okay. After creating the function, students also change the kill switch from BTN_A to BTN_U. In the next part of Mission 6, students will program BTN_A to do something else, and they will want to keep the kill switch. Students should test their code and debug any errors before submitting and going to the next lesson.

✅ The assignment is complete and ready to turn in. You can decide if you want students to turn in the program up to this point, or wait until the end of Mission 6.

🧑‍🤝‍🧑Wrap-Up (5 minutes)

Use a formative assessment for the wrap-up.

Formative assessment:

  • Daily reflection journal
  • Mission 6 Kahoot Review, questions 1-9 (in class or individual)
  • Exit ticket on loop, while loop, or infinite loop
  • Group review on loops