was_connected = True if uart.in_waiting: # Check to see if any data is available. try: packet = Packet.from_stream( uart) # Create the packet object. except ValueError: continue if isinstance(packet, ColorPacket): # If the packet is color packet... if mode == 0: # And mode is 0... animations.color = packet.color # Update the animation to the color. print("Color:", packet.color) animation_color = packet.color # Keep track of the current color... elif mode == 1: # Because if mode is 1... animations.color = animation_color # Freeze the animation color. print("Color:", animation_color) elif isinstance( packet, ButtonPacket): # If the packet is a button packet... if packet.pressed: # If the buttons in the app are pressed... if packet.button == ButtonPacket.LEFT: # If left arrow is pressed... print("A pressed: animation mode changed.") animations.next() # Change to the next animation. elif packet.button == ButtonPacket.RIGHT: # If right arrow is pressed... mode += 1 # Increase the mode by 1. if mode == 1: # If mode is 1, print the following: print("Right pressed: color frozen!") if mode > 1: # If mode is > 1... mode = 0 # Set mode to 0, and print the following: print("Right pressed: color changing!")
if buttons[i].fell: MODE = 1 # If not idle mode if MODE >= 1: pixels.brightness = 1 check_buttons(mpr121.touched_pins) time.sleep(0.1) if MODE == 2: # mode 2 is individual notes if audio.playing: pixels.show() while audio.playing: check_buttons(mpr121.touched_pins) time.sleep(0.07) else: MODE = 0 # Return to idle mode if MODE == 3: SONG = SONG + 1 animations.next() if SONG == 3: MODE = 0 SONG = 0 else: MODE = 0 # Return to idle mode if MODE == 9: # MODE 9 is "off" mode, listening for a new button press to wake up. # for button in buttons: # button.update() for i in range(12): if buttons[i].fell: MODE = 1
from adafruit_led_animation.color import RED, BLUE # Update to match the pin connected to your NeoPixels pixel_pin = board.D6 # Update to match the number of NeoPixels you have connected pixel_num = 32 # Update to matchpin connected to button that connect logic high when pushed button_pin = board.D3 pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False) button = DigitalInOut(button_pin) button.direction = Direction.INPUT button.pull = Pull.UP solid_blue = Solid(pixels, color=BLUE) solid_red = Solid(pixels, color=RED) animation_sequence = AnimationSequence(solid_blue, solid_red, auto_clear=True) while True: animation_sequence.animate() # Pressing the button pauses the animation permanently if not button.value: animation_sequence.next() while button.value: time.sleep(0.1) # Used for button debouncing