Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import digitalio
- import board
- import time
- import neopixel
- import adafruit_led_animation
- from adafruit_led_animation.animation.rainbowcomet import RainbowComet
- from adafruit_led_animation.animation.SparklePulse import SparklePulse
- from adafruit_led_animation.animation.colorcycle import ColorCycle
- from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
- from adafruit_led_animation.animation.pulse import Pulse
- from adafruit_led_animation.animation.solid import Solid
- from adafruit_led_animation.sequence import AnimationSequence
- from adafruit_led_animation.color import RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, PURPLE, MAGENTA, WHITE, BLACK, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE
- NUM_PIXELS = 31
- NEOPIXEL_PIN = board.D5
- POWER_PIN = board.D10
- ONSWITCH_PIN = board.A1
- COMET_DURATION = 3.7 #8.3 gives 3 full runs on 48 pixels
- LOW_BRIGHTNESS_PCT = 0.25
- strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=False)
- strip.fill(0)
- strip.show()
- enable = digitalio.DigitalInOut(POWER_PIN)
- enable.direction = digitalio.Direction.OUTPUT
- enable.value = True
- sparkle_pulseRed = SparklePulse(strip, speed=0.05, period=2, color=(255, 0, 0))
- sparkle_pulseYellow = SparklePulse(strip, speed=0.05, period=10, color=(255, 255, 0))
- sparkle_pulseRedLow = SparklePulse(strip, speed=0.05, period=2, color=(255, 0, 0), max_intensity = LOW_BRIGHTNESS_PCT)
- sparkle_pulseYellowLow = SparklePulse(strip, speed=0.05, period=10, color=(255, 255, 0), max_intensity = LOW_BRIGHTNESS_PCT)
- rainbow_comet = RainbowComet(strip, speed=0.03, tail_length=NUM_PIXELS, bounce=False)
- rainbow_cometBounce = RainbowComet(strip, speed=0.03, tail_length=NUM_PIXELS, bounce=True, colorwheel_offset=170)
- rainbow_sparkle = RainbowSparkle(strip, speed=0.3, step=1, num_sparkles=round(NUM_PIXELS * 0.10))
- colorcycleGreenYellow = ColorCycle(strip, 2, colors=[GREEN, YELLOW])
- colorcycleAll = ColorCycle(strip, 1.5, colors=[YELLOW, GREEN, MAGENTA, OLD_LACE, RED,
- AMBER, PINK, JADE, PURPLE, WHITE, AQUA, GOLD, BLUE, ORANGE, CYAN, TEAL])
- pulseOrange = Pulse(strip, speed=0.03, color=ORANGE, period=3)
- animationsRandom = AnimationSequence(
- sparkle_pulseRed, sparkle_pulseYellow, rainbow_comet, rainbow_cometBounce, colorcycleGreenYellow, colorcycleAll,
- pulseOrange, rainbow_sparkle,
- advance_interval=10, auto_clear=True, random_order=True
- )
- solidWhite = Solid(strip, (255, 255, 255))
- # Run animations for COMET_DURATION seconds on boot
- start_time = time.monotonic()
- while time.monotonic() - start_time < COMET_DURATION:
- rainbow_comet.animate()
- # Define the button
- button = digitalio.DigitalInOut(ONSWITCH_PIN)
- button.direction = digitalio.Direction.INPUT
- button.pull = digitalio.Pull.UP
- # Initialize state
- state = 2 # 0: Off, 1: 40% brightness, 2: 100% brightness
- last_button_state = True
- debounce_time = 0.5 # Debounce time in seconds
- def toggle_state():
- global state
- state = (state + 1) % 11
- #print("Ready for button")
- while True:
- # Read button state
- current_button_state = button.value
- # Check if button state has changed
- if current_button_state != last_button_state:
- # Debounce: wait for debounce_time before considering a new button press
- time.sleep(debounce_time)
- # Check button state again
- if current_button_state == button.value:
- toggle_state()
- print(state)
- last_button_state = current_button_state
- # Update LED animations based on state
- if state == 0:
- strip.fill(0) # Turn off LEDs
- strip.show()
- elif state == 1:
- sparkle_pulseRedLow.animate()
- sparkle_pulseYellowLow.animate()
- elif state == 2:
- sparkle_pulseRed.animate()
- sparkle_pulseYellow.animate()
- elif state == 3:
- pulseOrange.animate()
- elif state == 4:
- colorcycleGreenYellow.animate()
- elif state == 5:
- colorcycleAll.animate()
- elif state == 6:
- rainbow_comet.animate()
- elif state == 7:
- rainbow_cometBounce.animate()
- elif state == 8:
- rainbow_sparkle.animate()
- elif state == 9:
- animationsRandom.animate()
- elif state == 10:
- solidWhite.animate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement