Skip to content

Instantly share code, notes, and snippets.

@Jivemofo
Created June 25, 2019 22:17
Show Gist options
  • Save Jivemofo/4d6525387b91a2e93755b353a9f1cf7a to your computer and use it in GitHub Desktop.
Save Jivemofo/4d6525387b91a2e93755b353a9f1cf7a to your computer and use it in GitHub Desktop.
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
# switch between colors
def one(): # Red
return (255, 0, 0)
def two(): # Green
return (0, 255, 0)
def three(): # Blue
return (0, 0, 255)
def color_switcher(new_color):
switcher = {
1: one,
2: two,
3: three,
}
# get the function from switcher dictionary
func = switcher.get(new_color)
return func()
counter = 0
current_color = 0
while True:
# check the buttons
if cpx.button_a:
if counter < 9:
counter += 1
else:
counter = 0
print("Button A pressed!")
time.sleep(1)
elif cpx.button_b:
if current_color < color_mode:
current_color += 1
else:
current_color = 1
print("Button B pressed!")
time.sleep(1)
# output to LED's
else:
print("Current counter = " + str(counter))
print("Current Color = " + str(current_color))
time.sleep(.5)
cpx.pixels.fill = (color_switcher(current_color))
@seantibor
Copy link

This is really fun. I'm going to fork it and rewrite a few sections so you can see some more Pythonic ways of doing these things. Some of it is easier to read, some of it is just shorter.

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