AP CSP CodeX - Mission 11: Spirit Level

Mission 11 Lesson Plan

Mission 11: Spirit Level

Students read the CodeX accelerometer and draw a moving bubble on the display to build a digital spirit level.

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

Overview

During this mission, students build a digital spirit level. They read the CodeX accelerometer, convert the reading to degrees with the math library, and draw lines and a circle on the display to show the position of the CodeX. After Objective 6, students create a function with a return value, a good reminder of how useful abstraction can be.

🎯 Mission Goal: Students will use a graphical display to show the position of the CodeX, based on reading the accelerometer.

🎯

Learning Targets

  • I can use code to read the CodeX accelerometer.
  • I can convert an accelerometer reading to degrees.
  • I can use functions from the math library.
  • I can use built-in functions to draw lines and circles on the display screen.
💡

Key Concepts

  • Meet the accelerometer. There's one in your cell phone, and in many other devices we use.
  • Make sure students read the accelerometer toolbox entry!
  • Convert units generated by the accelerometer into degrees.
  • Use a bit o' math for scaling the degrees to a range suitable for the moving "bubble".
  • The CodeX screen is a graphical user interface and can display lines, circles and text.
✅

Assessment Opportunities

☑️

Success Criteria

  • Read data from the accelerometer
  • Convert data to degrees
  • Draw lines on the display screen
  • Use data from the accelerometer to draw a circle
  • Create and call a function with a return
📁

Digital Resources

🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
  • ▸Physical spirit level (for the warm-up)
🌍

Real-World Applications

📱An accelerometer is a handy sensor that measures changes in speed, tilt, and physical movement. You run into them every day without even noticing. For example:
  • Cell phone screen rotation
  • Fitness app step counter
  • Airbag deployment in vehicles
  • Electronic stability control in vehicles
  • Smart watches that have fall detection
  • Earthquake and structural tracking
🚀

Extensions & Cross-Curricular

ExtensionUse the y value from the accelerometer to move the bubble up and down as well as side to side.
ExtensionChange the color of the bubble when the CodeX is level.
ExtensionDisplay the tilt in degrees as text on the screen.
MathThe program converts a reading to degrees with the math library. Look up what the inverse sine function does, and check one of the program's results by hand.
ScienceResearch how the accelerometer in a phone knows which way the screen is turned, and how it detects a fall.
Lang ArtsWrite a short product description for a digital level, explaining to a builder how to use it.
🔤

Vocabulary

▾
Accelerometer - A sensor chip that detects motion, impacts, and orientation; a device that measures proper acceleration.
Tuple - A read-only version of a list, indicated with parenthesis, and has items you can access with an index.
🐍

New Python Code

▾
import mathImport the math module, used for math operations, like math.pi, math.asin, etc.
val = accel.read()Get values from the accelerometer.
val = accel.read() tilt_x = val[0]Get a single value from the accelerometer.
display.fill(WHITE)Fill the CodeX display with a color.
display.draw_line(x1, y1, x2, y2, color) display.draw_line(CENTER,0,CENTER,105,BLACK)Draw a line.
display.draw_circle(x, y, radius, color) display.draw_circle(x,CENTER,15,ORANGE)Draw a circle.
display.fill_circle(CENTER,CENTER,15,RED)Draw a filled-in circle.
display.draw_text(str(score), x=20, y=20, scale=3, color=BLACK)Display text at a specific location, using keyword parameters.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-DA-11 3A-DA-12 3A-AP-13 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21

CSTA Standards - Grades 11-12

3B-CS-02 3B-DA-06 3B-AP-10 3B-AP-14 3B-AP-16 3B-AP-22 3B-AP-23

AP CSP Framework

DAT-2.A - Describe what information can be extracted from data.
AAP-3.D - Select appropriate libraries or existing code segments to use in creating new programs.
Computational Thinking Practice 3.A - Generalize data sources through variables.
Computational Thinking Practice 4.C - Identify and correct errors in algorithms and programs, including error discovery through testing.
📝
Preparing for the Lesson
  • The assignment is best completed digitally. Prepare the assignment to give through your LMS.
  • Bring a physical spirit level to class for the warm-up.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍🏫
Teacher Notes
  • Encourage the students to do as much code on their own as they can, and use CodeTrek to check their work, or as a hint when needed.
  • The assignment adds an extra step to complete after Objective 6, before turning in the program.
  • If you have time at the end of the lesson, use the Mission 11 Kahoot! Review.
  • 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

Bring a physical level to school and show it to the students. Discuss how a mechanical spirit level works. The "spirit" is a liquid with space for a bubble, which will be in the center of the tube when it is horizontal. Today's program will create a digital level using the CodeX and its accelerometer.

💻Activity - Mission 11

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 assignment open on one computer and CodeSpace on the other computer.

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

Teaching tip - Objective 1: The objective introduces the accelerometer. It returns three values for 3 dimensions: x, y and z. Students will work with x (and y in the challenges). There is a bit of reading for this objective so students understand what the accelerometer is and what it does, and the values it will return. Use the assignment for talking points and review.
Teaching tip - Objective 2: This objective shows the code for reading the accelerometer. Use the assignment for talking points and review.
Teaching tip - Objective 3: This objective uses functions from the math library. If your students are not yet in trig, don't worry about explaining the math. They can just copy the code.
Teaching tip - Objective 4: This objective introduces draw_line() and draw_circle() for the first time. Students can fill the screen with any color and then draw lines and circles on the screen. There is a bit of reading on this objective so students can understand how the simple graphics work and the orientation of the screen.

NOTE: If displaying on a screen, using a BLACK fill and WHITE lines is easier to see.

Here are all the commands students could use. These will be used in a future lesson. See the CodeX canvas documentation for details.

  • draw_line(x1, y1, x2, y2, color=(255, 255, 255))
  • fill_rect(x1, y1, width, height, color=(255, 255, 255))
  • fill_circle(x, y, radius, color=(255, 255, 255))
  • fill(color)
  • draw_text(text, scale=1, color=(255, 255, 255), background=None, x=0, y=0)
  • draw_rect(x1, y1, width, height, color=(255, 255, 255))
Teaching tip - Objective 6: Students complete the Spirit Level program using CodeTrek.
🧩Activity - After Objective 6
Teaching tip - After Objective 6: The students create a function with a return for converting an accelerometer reading into degrees. Instructions are on the assignment document. This is a good time to remind students about abstraction and how useful it can be in programming. Two options are given. Both are fairly simple, and students should be able to code both. When creating a function for the background, students should NOT include the constant CENTER. The code solution for functions is available.
Teaching tip - Challenge: If students have time, have them try the challenge. This gives them an opportunity to apply their knowledge in a new situation. A code solution for the challenge is available.

✅ The assignment is complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

You can discuss with students other applications for the CodeX accelerometer. Let them brainstorm ideas.

Optional. Use a formative assessment for the wrap-up.

Formative assessment:

  • Daily reflection journal
  • Mission 11 Kahoot! Review (in class or individual)
  • Exit ticket or group review on the accelerometer, tuples, or other topics used in the lesson