Skip to content

Instantly share code, notes, and snippets.

@bitsandbooks
Last active September 17, 2022 20:06
Show Gist options
  • Save bitsandbooks/40ac36bd1e450367b5588d26ac85de18 to your computer and use it in GitHub Desktop.
Save bitsandbooks/40ac36bd1e450367b5588d26ac85de18 to your computer and use it in GitHub Desktop.
CircuitPython with the CPX
#!/usr/bin/python
# CPX Light Effects program. Copyright (c) 2022 by Elk Grove Village Public Library.
# Licensed under the [GNU General Public License, version 3](https://www.gnu.org/licenses/gpl-3.0.en.html).
from adafruit_circuitplayground.express import cpx
import time
wait_time = 0.05
pixel_count = 10
def fill(color_value):
for pixel in range( 0, pixel_count, 1 ):
cpx.pixels[pixel] = color_value
time.sleep(wait_time)
time.sleep(wait_time)
for pixel in range( pixel_count-1, -1, -1 ):
cpx.pixels[pixel] = (0,0,0)
time.sleep(wait_time)
def scanner(color_value):
red_value = color_value[0]
green_value = color_value[1]
blue_value = color_value[2]
temp = round(pixel_count/2)
low_r = round(red_value/255)
low_g = round(green_value/255)
low_b = round(blue_value/255)
med_r = round(red_value/4)
med_g = round(green_value/4)
med_b = round(blue_value/4)
for n in range (0,10,1):
cpx.pixels[0] = (red_value, green_value, blue_value)
cpx.pixels[5] = (red_value, green_value, blue_value)
for i in range(1, temp, 1):
time.sleep(wait_time)
cpx.pixels[i] = (red_value, green_value, blue_value)
cpx.pixels[i-1] = (low_r, low_g, low_b)
cpx.pixels[i+temp] = (red_value, green_value, blue_value)
cpx.pixels[i+temp-1] = (low_r, low_g, low_b)
time.sleep(wait_time)
cpx.pixels[temp-1] = (low_r, low_g, low_b)
cpx.pixels[pixel_count-1] = (low_r, low_g, low_b)
while True:
if cpx.button_a:
cpx.start_tone(440)
fill( (0,0,255) ) # blue
cpx.stop_tone()
elif cpx.button_b:
cpx.start_tone(880)
fill( (0,255,0) ) # green
cpx.stop_tone()
elif cpx.touch_A1:
scanner( (255,255,0) )
elif (cpx.light <= 10):
for i in range(0,10,1):
cpx.play_tone(1000,0.01)
else:
fill( (255,0,0) ) # red

CircuitPython with the Circuit Playground Express

You can also test for other things on the CPX. Here, the CPX will beep ten times if you hold your hand over it using cpx.light.

We've also added a slightly more challenging piece of code: a new method, called scanner(), which creates a different effect when you're touching A1 (using cpx.touch_A1) at the start of the loop. Feel free to dig through the code and figure out how it works.

There are lots of fun things you can do with CircuitPython. Enjoy and explore!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment