Fly with Python - Mission 4: Flight Safety
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.
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.
-
ifandelifstatements allow a branch of indented code to run, based on a condition. -
The
breakstatement 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
Digital Resources
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
button_arm().
Extensions & Cross-Curricular
button_arm() function and describe what each section does.
🔤
Vocabulary
▾
🐍
New Python Code
▾
breakIf statement (branching) that checks for a button press.
buttons.was_pressed(BTN_0) is either True or False.
if buttons.was_pressed(BTN_0):
breakIf statement in an infinite loop. The code waits for a button press before moving to the next line of code.
motor_test().
button_arm(), which returns a True or False value.
📐
Standards
▾
CSTA Standards - Grades 6-8
CSTA Standards - Grades 9–10
CSTA Standards - Grades 11–12
- 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.
- 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
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?"
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
ifbranching statement, showing how code can take different paths based on a condition. - Preview the
breakstatement 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.
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.
-
Detect a button press - use
buttons.was_pressed()with anifstatement andbreakinside awhile True:loop (quiz after Objective 2). -
Add a warning indicator - use
pixels.fill(YELLOW)andpixels.off()to give the user a visual warning before motors arm. -
Debounce the buttons - add a short
sleep()and reset to prevent accidental double-presses. -
Define
button_arm()- wrap the arming sequence in a user-defined function that returns True or False (quiz after Objective 5). -
Run a motor test - use
motor_test()andset_param()to safely verify the motor power system.
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.
button_arm() is a tool students will reuse in upcoming flight missions, and it pays off the time spent here.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.