Brain DeCoded with CodeX - Mission 1, Objective 4: Code Action

Mission 1 · Objective 4 Lesson Plan

Code Action

Students light up CodeX's four RGB pixel LEDs and use the buttons to control them.

⏱ 20-40 min 🎯 Grades 8-12+ 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

Objective 3 was output only. Here students add input. They write a program that watches the CodeX buttons and lights up one of the four RGB pixel LEDs in a chosen color each time a button is pressed. It is the first program in the pack that reacts to the person using it.

Two new pieces of Python show up: the while True: loop that keeps the program running, and the if statement that runs a branch of code only when a condition is met. Indentation is what tells Python which lines belong inside the loop and inside the if, so expect indentation and missing colons to be the two errors you troubleshoot most.

🎯 Project Goal: Students will write a Python program to control the four RGB pixel LEDs using buttons for input.

🎯

Learning Targets

  • I can control CodeX's four RGB pixel LEDs.
  • I can use CodeX's six buttons for input.
💡

Key Concepts

  • The four RGB pixel LEDs are numbered 0, 1, 2 and 3. They can be set to any color.
  • Students can use pre-defined colors for the RGB pixels without having to break down a color into Red/Green/Blue.
  • The CodeX has 6 buttons for input.
  • An if statement is used to create a branch of code that runs depending on a condition.

Assessment Opportunities

  • Complete the program button_fun.
  • Exit ticket: draw a picture of CodeX and label the four RGB pixel LEDs and the six buttons.

Success Criteria

  • Complete the CodeTrek steps
  • Program runs correctly without errors
📂

Digital Resources

  • RGB Colors tool in the CodeSpace Toolbox, the list of pre-defined color names students will type
🧰

Classroom Materials

  • Laptop/computer with Chrome browser
  • CodeX
  • USB cable
🚀

Extensions & Cross-Curricular

ExtensionThe goal can be met by programming 4 of the buttons. Have students program all 6 buttons.
ExtensionThe function buttons.is_pressed() is different from buttons.was_pressed(). Have students try it and observe the difference.
ExtensionHave a lesson about RGB colors and have students define their own colors to use in the code using RGB.
MathRGB colors use values from 0-255, which is based on binary numbers. Have a lesson on binary numbers.
ScienceHave a lesson on light and how red, green, and blue light can combine to create all the colors. Discuss how colors in light are different from colors in paint.
Lang ArtsHave students write about one of the topics covered in this objective.
🔤

Vocabulary

NeoPixels:LED lights that can display any color, using a combination of RED, GREEN and BLUE lights.
Boolean values:values that are either True or False (one of two states).
🐍

New Python Code

while True:Infinite loop, keeps the code running continuously.
if buttons.was_pressed(BTN_A):Used to create a branch of code that runs, depending on a condition. In this case, if Btn-A is pressed. If not pressed, the code is skipped.
pixels.set(0, RED)Turns a single NeoPixel a color. The first argument is the pixel number, and the second argument is the color.
📐

Standards

Computer Science - Grades 9-12

9-12.AP.15
📝
Preparing for the Lesson
  • Write and run the button_fun program yourself before class so you know which pixel lights up for each button.
  • Open the RGB Colors tool in the Toolbox so you can project the list of pre-defined color names.
  • Have CodeX units and USB cables counted out and ready. Pixel 0 through 3 and the six button names are worth pointing out on a projected device or a board sketch before students start typing.
  • Decide how you want to handle indentation. Writing one correctly indented if statement on the board gives students a model to compare against.

🧑‍🏫
Teacher Notes
  • The code inside an if statement must be indented! All lines of code need to have the same indentation. Otherwise an error will occur.
  • The if statement must end with a colon (:). Otherwise an error will occur.
  • A list of pre-defined colors can be found in the RGB Colors tool. Students should click on it and add it to their toolbox for easy reference. Pre-defined colors are typed in ALL CAPS.
  • Extensions and cross-curricular projects are included to enhance the concepts in the objective. You can use the extensions to extend students' programming experience.
🗺️

Lesson Outline

🗣️Warm-up / Hook

Hold up a CodeX and open with a quick discussion about input.

  • Ask: "Every program we have written so far just runs and ends. What would it take for a program to wait for you?"
  • Ask: "How many buttons do you count on the CodeX, and what could each one do?"
Teaching tip: Point out the four RGB pixels and number them 0, 1, 2, 3 out loud. Counting from zero is new for most students and it comes up again all through the pack.
📖Lesson Activities

Introduce the two new pieces of Python before students start typing, then show the color list.

  1. Name the six buttons and the four pixels on the device, and show that pixels.set(0, RED) takes a pixel number and a color.
  2. Explain while True: as the loop that keeps the program listening, so it does not run once and quit.
  3. Write one if statement on the board. Point at the colon and at the indented line underneath, and say why each one matters.
  4. Open the RGB Colors tool and project it. Have students click it so it lands in their own toolbox, and note that color names are typed in ALL CAPS.
Teaching tip: Almost every error in this objective is a missing colon or a line that is not indented to match the others. Ask students to check those two things before they raise a hand.
💻Student Work Time

Students work through Objective 4 in CodeSpace and build the button_fun program.

  1. Students follow the CodeTrek steps to type the code into button_fun.
  2. Students program at least four of the buttons, each one lighting a pixel in a color they choose.
  3. Students run the program and press the buttons to check that each one lights the pixel they expected.
  4. Students debug as needed, then submit the completed program.
Teaching tip: Students who finish early can program all six buttons, or swap was_pressed() for is_pressed() and describe what changes.
✏️Wrap-up & Review

Collect devices, then close with the exit ticket.

  • Exit ticket: draw a picture of CodeX and label the four RGB pixel LEDs and the six buttons.
  • Quick share: what happened the first time you ran your program, and what did you fix?
Teaching tip: This is a good spot for the RGB color extension. Students who want a color that is not on the pre-defined list need to mix it from Red, Green and Blue values, which leads straight into the math and science extensions.