Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jivemofo/a6403b1b5a8cbdc82eb403f54f6336f9 to your computer and use it in GitHub Desktop.
Save Jivemofo/a6403b1b5a8cbdc82eb403f54f6336f9 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
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
def color_switcher(new_color):
switcher = {
1: RED,
2: GREEN,
3: BLUE,
4: BLACK,
}
# get the color from switcher dictionary with black as default
return switcher.get(new_color, (255,0,0))
counter = 0
current_color = 1
while True:
# check the buttons
if cpx.button_a:
# add 1 to counter
counter += 1
# use mod to set counter range 0 to 10
counter %= 10
print("Button A pressed!")
time.sleep(.2)
elif cpx.button_b:
current_color %= 3
current_color += 1
print("Button B pressed!")
time.sleep(.2)
cpx.pixels.fill(BLACK)
# output to LED's
else:
print("Current counter = {}".format(counter))
print("Current Color = {}".format(current_color))
time.sleep(.1)
cpx.pixels[0:(counter)] = [(color_switcher(current_color))] * counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment