Forked from seantibor/gist:f6ca28987b419a701b6349055450939b
Last active
June 26, 2019 21:14
-
-
Save Jivemofo/ac370772bafba5f0ff6312017b4bd2d0 to your computer and use it in GitHub Desktop.
Circuit Playground Express Led Button Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this program detects a button press and lights up a new Led, 2nd button changes colors | |
from adafruit_circuitplayground.express import cpx, time | |
# pick how many colors to use | |
color_mode = 6 | |
RED = (255, 0, 0) | |
GREEN = (0, 255, 0) | |
BLUE = (0, 0, 255) | |
BLACK = (0, 0, 0) | |
YELLOW = (255, 255, 0) | |
CYAN = (0, 255, 255) | |
PURPLE = (128, 0, 128) | |
cpx.pixels.brightness = 0.2 | |
cpx.pixels.fill(BLACK) | |
def color_switcher(new_color): | |
switcher = { | |
1: RED, | |
2: GREEN, | |
3: BLUE, | |
4: YELLOW, | |
5: CYAN, | |
6: PURPLE, | |
7: BLACK, | |
} | |
# get the color from switcher dictionary with yellow as default | |
return switcher.get(new_color, (255,255,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 11 | |
counter %= 11 | |
print("Button A pressed!") | |
time.sleep(.2) | |
elif cpx.button_b: | |
current_color %= color_mode | |
current_color += 1 | |
print("Button B pressed!") | |
time.sleep(.2) | |
# next line optional | |
#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[counter:10] = [(BLACK)] * abs(10 - counter) | |
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