Fly with Python - Mission 4: Flight Safety

Mission 4 Lesson Plan

Flight Safety

Students program a set of safety procedures for CodeAIR, using buttons, if statements, debouncing, and a user-defined function to arm the drone before take-off.

⏱ 60–90 min 🎯 Grades 6–12+ 💻 CodeSpace 🚁 CodeAIR 🐍 Python
View Lesson Outline
📋

Overview

Mission 4 kicks off Unit 2 with a focus on flight safety. Students build a reusable safety procedure that guards against accidental motor start-up, using button input, branching if / elif statements, the break statement, and button debouncing. They wrap up by writing their first user-defined function, button_arm(), that can be reused throughout future flight programs.

🎯 Mission Goal: Students will program a set of safety procedures that can be used in future drone flights.

🎯

Learning Targets

  • I can write code to arm the drone for take-off.
  • I can write code for a warning indicator.
  • I can understand the quadcopter power system.
💡

Key Concepts

  • Safety procedures can ensure the user is safe before a drone take-off.
  • CodeAIR has two input buttons.
  • if and elif statements allow a branch of indented code to run, based on a condition.
  • The break statement stops a loop.
  • Buttons can accidentally record two presses. Code to "debounce" a button is necessary.
  • Create a function for code you want to reuse.

Assessment Opportunities

  • Quiz after Objective 2
  • Quiz after Objective 5
  • Complete the program safety.py
  • Mission 4 Assignment
  • Mission 4 Review Questions
☑️

Success Criteria

  • Use an if statement and break statement to stop a loop when a button is pressed
  • Use code to "debounce" button presses
  • Create a user-defined function
  • safety.py works correctly and runs without errors
  • Complete Mission 4 Assignment
🧰

Classroom Materials

  • Laptop/computer with Chrome browser
  • CodeAIR drone, battery and USB cable for each student or programming pair
  • Adaptor for USB cable, if needed
  • Clear, obstacle-free workspace for motor testing
🌍

Real-World Applications

🛡️Safety interlocks are used in industrial robotics, medical devices, and consumer electronics to prevent equipment from running before the user is ready, the same idea behind button_arm().
Real drones, aircraft, and vehicles all use arm/disarm sequences before motors can spin, protecting both people and equipment from accidental power-up.
🔁Debouncing is a foundational technique in embedded systems, every keyboard, game controller, elevator button, and IoT device uses some form of it.
🧩User-defined functions are a building block of every real-world software system. They let engineers reuse safety-critical code without duplicating it.
🚀

Extensions & Cross-Curricular

ExtensionHave students use ChatGPT to annotate a section of their code. Then have students do their own annotation of a different section of code.
ExtensionHave students do a code review - go through the button_arm() function and describe what each section does.
ScienceThe mission discusses torque and Newton's 3rd Law. Study these topics in more depth.
Lang ArtsAfter using ChatGPT to annotate a section of code, write a paragraph that describes how artificial intelligence drives many software and physical systems.
Lang ArtsMake a list of troubleshooting strategies used to identify and fix errors.
🔤

Vocabulary

Quadcopter Safety Guidelines - Steps to take to ensure personal safety when working with a drone. They include wearing protective gear, avoiding contact with moving parts, and operating in a clear area.
Safety Interlock - A safety measure that prevents an electronic device from starting until an event is triggered, like a button press.
UX - User experience; it encompasses the navigation of a product and how easy to use it is.
Branching 'if' Statement - A programming control structure that lets code do something different if a certain condition happens, like a button press. This is different from sequential or iteration.
Bounce - When the metal contacts of an electronic input peripheral like a button bounce a few times before coming to a rest. This problem could mean a peripheral is read more than once.
Function - Reusable code with a name. Making reusable components is a major goal of software engineering. Once a function is defined, it must be called before the code is executed.
Variable - A named value used in code, like a box with a label. Use the variable name instead of the value. A value can be any data type, including a number, a string (text) or a Boolean.
Torque - Rotational force produced by motors. When torque is produced, there is a naturally occurring force in the opposite direction.
🐍

New Python Code

buttons.was_pressed(BTN_0)Checks to see if B0 was pressed since the last check.
breakBreaks out of the nearest enclosing loop.
if buttons.was_pressed(BTN_0):
    break
If statement (branching) that checks for a button press. buttons.was_pressed(BTN_0) is either True or False.
while True:
    if buttons.was_pressed(BTN_0):
        break
If statement in an infinite loop. The code waits for a button press before moving to the next line of code.
pixels.fill(YELLOW)Sets all 8 pixels to YELLOW (built-in color).
pixels.off()Turn off all 8 pixels.
sleep(0.1) / buttons.was_pressed()Debounce the buttons. This line of code resets both buttons!
from flight import *Imports the flight module so you can use built-in functions, like motor_test().
motor_test(True) / motor_test(False)Start / stop a motor test that spins the motors but not fast enough to lift off.
def button_arm():Function definition. The indented block below is the code of the function. A function definition always has () for parameters, even if none are given.
return do_launchReturns (sends) data from the function back to the code that called it. A return ends the function.
if button_arm():Call the function button_arm(), which returns a True or False value.
set_param('motorPowerSet.m2', 30000)Set motor (m2) power (30000).
set_param('motorPowerSet.enable', 1)Enable power to the motors.
set_param('motorPowerSet.enable', 0)Disable power to the motors.
📐

Standards

CSTA Standards - Grades 6-8

2-CS-02 2-CS-03 2-AP-10 2-AP-11 2-AP-12 2-AP-13 2-AP-16 2-AP-17 2-AP-19

CSTA Standards - Grades 9–10

3A-DA-11 3A-AP-13 3A-AP-16 3A-AP-17 3A-AP-21

CSTA Standards - Grades 11–12

3B-NI-03 3B-AP-08 3B-AP-14 3B-AP-15 3B-AP-22 3B-AP-23
📝
Preparing for the Lesson
  • Ensure all CodeAIR drones are charged before class. CodeAIR must be connected and turned on, even though students aren't flying yet.
  • Have the CodeAIR Flying Guide handy for any students who run into connection trouble.
  • Verify USB cables and any necessary adaptors are available for each student or programming pair.
  • Make sure students have a clear, obstacle-free workspace. The motor test will spin the propellers, even though it won't lift off.

🧑‍🏫
Teacher Notes
  • The hints in Objective 1 give suggestions for troubleshooting programming errors and for using ChatGPT to understand code. Review the hints with students before they start.
  • The assignment document follows the mission and is a place for guided notes. You can print the document for students (PDF) or assign it digitally through your LMS (doc).
  • The buttons to press are very tiny and can be hard to see. They are located just next to the first and last blue LEDs. The blinking lights help identify which button to press in the correct order.
  • Review questions can be used as a class review, made into a Kahoot!, or used to create an exam in your learning management system.
  • Extensions and cross-curricular projects are included to enhance the concepts in the mission. A remix is planned after Mission 5.
🗺️

Lesson Outline

🗣️Warm-up / Hook

Students access prior knowledge by answering questions in the pre-mission section of the assignment doc.

Connect the mission concept to real-world safety interlocks through a brief discussion.

  • Ask: "Why do you think a microwave won't turn on when the door is open? Or why a car won't start when it's in drive?"
  • Ask: "What could go wrong if a drone's motors started spinning before the user was ready?"
Teaching tip: Tie this discussion back to the user experience (UX) idea - a good safety system protects the user without being annoying.
📖Introduce the Mission

Front-load the key new programming concepts before students begin the coding session.

  • Introduce the two input buttons (B0 and B1), showing students where they are physically located on the CodeAIR.
  • Demonstrate the if branching statement, showing how code can take different paths based on a condition.
  • Preview the break statement and the idea of debouncing - why a single button press might register twice.
  • Mention that students will write their first user-defined function, button_arm(), that will be reused in future missions.
Teaching tip: Walk through Objective 1 hints with students, which include strategies for troubleshooting and for using ChatGPT to understand code.
💻Coding Time

Students work through the mission objectives and build the safety.py program. As they work, they should take notes and answer questions in their assignment doc.

  1. Detect a button press - use buttons.was_pressed() with an if statement and break inside a while True: loop (quiz after Objective 2).
  2. Add a warning indicator - use pixels.fill(YELLOW) and pixels.off() to give the user a visual warning before motors arm.
  3. Debounce the buttons - add a short sleep() and reset to prevent accidental double-presses.
  4. Define button_arm() - wrap the arming sequence in a user-defined function that returns True or False (quiz after Objective 5).
  5. Run a motor test - use motor_test() and set_param() to safely verify the motor power system.
Teaching tip: The buttons are small and hard to see. Have students use the blinking blue LEDs as a guide for which button to press next.
Teaching tip: Watch for indentation errors inside the function definition - students new to functions often misalign the body block.
🧑‍🤝‍🧑Class Debrief

Bring the class together to consolidate the big programming ideas from this mission and reinforce safety thinking.

  • Ask: "Why did we wrap the arming sequence in a function instead of writing the code over and over?" - connect to reusability.
  • Ask: "What is debouncing, and why do we need it?"
  • Ask: "How does Newton's 3rd Law explain why a drone needs four motors instead of just one?" - preview the science extension.
Teaching tip: Reinforce that button_arm() is a tool students will reuse in upcoming flight missions, and it pays off the time spent here.
✏️Wrap-up & Review

Students answer the reflection question in the assignment doc and then submit.

Use the Mission 4 Review Questions through a preferred method, such as class discussion, Kahoot!, or LMS quiz.

Teaching tip: A unit remix is planned after Mission 6, so let students start thinking about how they might combine safety, lights, and sounds into their own original program.