Brain DeCoded with CodeX - Mission 1, Objective 4: Code Action
Code Action
Students light up CodeX's four RGB pixel LEDs and use the buttons to control them.
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
ifstatement 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
buttons.is_pressed() is different from buttons.was_pressed(). Have students try it and observe the difference.
🔤
Vocabulary
▾
🐍
New Python Code
▾
📐
Standards
▾
Computer Science - Grades 9-12
- 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.
-
The code inside an
ifstatement must be indented! All lines of code need to have the same indentation. Otherwise an error will occur. -
The
ifstatement 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
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?"
Introduce the two new pieces of Python before students start typing, then show the color list.
-
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. -
Explain
while True:as the loop that keeps the program listening, so it does not run once and quit. -
Write one
ifstatement on the board. Point at the colon and at the indented line underneath, and say why each one matters. - 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.
Students work through Objective 4 in CodeSpace and build the button_fun program.
- Students follow the CodeTrek steps to type the code into button_fun.
- Students program at least four of the buttons, each one lighting a pixel in a color they choose.
- Students run the program and press the buttons to check that each one lights the pixel they expected.
- Students debug as needed, then submit the completed program.
was_pressed() for is_pressed() and describe what changes.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?