Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Circuit Playground Express Led Button Test
# this program detects a button press and lights up a new Led, 2nd button changes colors
from adafruit_circuitplayground.express import cpx, time
cpx.pixels.brightness = 0.3
cpx.pixels.fill((0, 0, 0))
# pick how many colors to use
color_mode = 3
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
def color_switcher(new_color):
switcher = {
1: RED,
2: GREEN,
3: BLUE,
}
# get the color from switcher dictionary with black as default
return switcher.get(new_color, (0,0,0))
counter = 0
current_color = 0
while True:
# check the buttons
if cpx.button_a:
# add 1 to counter
counter += 1
# use mod to set counter range 0 to 9
counter %= 9
print("Button A pressed!")
time.sleep(1)
elif cpx.button_b:
current_color %= 3
current_color += 1
print("Button B pressed!")
time.sleep(1)
# output to LED's
else:
print("Current counter = {}".format(counter))
print("Current Color = {}".format(current_color))
time.sleep(.5)
cpx.pixels.fill = (color_switcher(current_color))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment