Circuit Playground Express has 10 Neopixels in a ring. They're numbered 0-9 starting at the top left pixel, next to the micro USB port, and going counter-clockwise around the board. Each pixel can be individually programmed to display any color, or they can be programmed together.
Each NeoPixel LED contains 3 colors: red, green and blue. The colors are collectively referred to as RGB. When working with colored lights, every color is created from these three being combined at different levels. If only the red is turned on, you'll see red. If red and blue are turned on equally, you'll see purple. If all are turned on equally, you get white. We're going to use this process to add a color of the rainbow to every note on our tone piano.
Let's start by turning on the first Neopixel. We'll turn it blue. Download the following file. Rename it to code.py
. Copy it to your CPX.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp while True: cp.pixels[0] = (0, 0, 3)
You will be rewarded with a blue pixel on your CPX.
Now let's take a look at the code.
The first line has two parts. The first part is the module. This is different for different types of hardware. As we're using the Circuit Playground Express, we're all good there! The second part of the first line imports the information from the module needed to make the code do what it does. This line will be part of all of the code we use for this project. It's necessary for the board to understand the rest of the code we write.
The second line is telling the CPX to turn on pixel 0, and to make it blue. The colors are written using RGB, which ranges from 0-255. It is the combination of the RGB numbers that change what color is displayed, based on the ratio of the three numbers. The higher the number, the brighter that individual color is. The three numbers together, separated by commas, are called a tuple. The reason for the two sets of parentheses is that the pixels()
function is expecting a single piece of information, which is the (R, G, B)
tuple as a whole..
If you change [0]
to any number 0 through 9, you can turn on any one of the NeoPixels. If you change the numbers in ((0, 0, 3))
to any number 0-255, you'll change the color and brightness each NeoPixel displays.
You can include more than one NeoPixel by adding another line of code with the number of the desired pixel. The following code lights up the opposite pixel as well. Try editing your code.py
file to add the additional line of code.
from adafruit_circuitplayground.express import cpx cpx.pixels[0] = ((0, 0, 3)) cpx.pixels[5] = ((3, 0, 0))
You can add more pixels or change the colors of the two we've already lit. Have a little fun with it!
For this project, we're going to turn all the NeoPixels the same color. We use a different color for each tone. For now, we'll start by turning them all blue.
If you made any changes to your code that you'd like to keep, rename your current code.py
first. Then, download the following file. Rename it to code.py
and copy it to your CPX.
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground import cp while True: cp.pixels.fill((0, 0, 3))
The difference between this code and the first set of code is, instead of specifying an individual NeoPixel by number, we are programming them all at once to display the same color, by using cp.pixels.fill
.
You can experiment with different colors again by changing the ((0, 0, 3))
to numbers between 0-255.
Next, we'll learn about the slide switch.
Text editor powered by tinymce.