Advertisement
Guest User

Untitled

a guest
Aug 9th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | Source Code | 0 0
  1. import digitalio
  2. import board
  3. import time
  4. import neopixel
  5. import adafruit_led_animation
  6.  
  7. from adafruit_led_animation.animation.rainbowcomet import RainbowComet
  8. from adafruit_led_animation.animation.SparklePulse import SparklePulse
  9. from adafruit_led_animation.animation.colorcycle import ColorCycle
  10. from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
  11. from adafruit_led_animation.animation.pulse import Pulse
  12. from adafruit_led_animation.animation.solid import Solid
  13. from adafruit_led_animation.sequence import AnimationSequence
  14. from adafruit_led_animation.color import RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, PURPLE, MAGENTA, WHITE, BLACK, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE
  15.  
  16. NUM_PIXELS = 31
  17. NEOPIXEL_PIN = board.D5
  18. POWER_PIN = board.D10
  19. ONSWITCH_PIN = board.A1
  20. COMET_DURATION = 3.7 #8.3 gives 3 full runs on 48 pixels
  21. LOW_BRIGHTNESS_PCT = 0.25
  22.  
  23. strip = neopixel.NeoPixel(NEOPIXEL_PIN, NUM_PIXELS, brightness=1, auto_write=False)
  24. strip.fill(0)
  25. strip.show()
  26.  
  27. enable = digitalio.DigitalInOut(POWER_PIN)
  28. enable.direction = digitalio.Direction.OUTPUT
  29. enable.value = True
  30.  
  31. sparkle_pulseRed = SparklePulse(strip, speed=0.05, period=2, color=(255, 0, 0))
  32. sparkle_pulseYellow = SparklePulse(strip, speed=0.05, period=10, color=(255, 255, 0))
  33. sparkle_pulseRedLow = SparklePulse(strip, speed=0.05, period=2, color=(255, 0, 0), max_intensity = LOW_BRIGHTNESS_PCT)
  34. sparkle_pulseYellowLow = SparklePulse(strip, speed=0.05, period=10, color=(255, 255, 0), max_intensity = LOW_BRIGHTNESS_PCT)
  35. rainbow_comet = RainbowComet(strip, speed=0.03, tail_length=NUM_PIXELS, bounce=False)
  36. rainbow_cometBounce = RainbowComet(strip, speed=0.03, tail_length=NUM_PIXELS, bounce=True, colorwheel_offset=170)
  37. rainbow_sparkle = RainbowSparkle(strip, speed=0.3, step=1, num_sparkles=round(NUM_PIXELS * 0.10))
  38. colorcycleGreenYellow = ColorCycle(strip, 2, colors=[GREEN, YELLOW])
  39. colorcycleAll = ColorCycle(strip, 1.5, colors=[YELLOW, GREEN, MAGENTA, OLD_LACE, RED,
  40.     AMBER, PINK, JADE, PURPLE, WHITE, AQUA, GOLD, BLUE, ORANGE, CYAN, TEAL])
  41. pulseOrange = Pulse(strip, speed=0.03, color=ORANGE, period=3)
  42. animationsRandom = AnimationSequence(
  43.     sparkle_pulseRed, sparkle_pulseYellow, rainbow_comet, rainbow_cometBounce, colorcycleGreenYellow, colorcycleAll,
  44.     pulseOrange, rainbow_sparkle,
  45.     advance_interval=10, auto_clear=True, random_order=True
  46. )
  47. solidWhite = Solid(strip, (255, 255, 255))
  48.  
  49. # Run animations for COMET_DURATION seconds on boot
  50. start_time = time.monotonic()
  51. while time.monotonic() - start_time < COMET_DURATION:
  52.     rainbow_comet.animate()
  53.  
  54.  
  55. # Define the button
  56. button = digitalio.DigitalInOut(ONSWITCH_PIN)
  57. button.direction = digitalio.Direction.INPUT
  58. button.pull = digitalio.Pull.UP
  59.  
  60. # Initialize state
  61. state = 2  # 0: Off, 1: 40% brightness, 2: 100% brightness
  62. last_button_state = True
  63. debounce_time = 0.5  # Debounce time in seconds
  64.  
  65. def toggle_state():
  66.     global state
  67.     state = (state + 1) % 11
  68.  
  69. #print("Ready for button")
  70.  
  71. while True:
  72.     # Read button state
  73.     current_button_state = button.value
  74.  
  75.     # Check if button state has changed
  76.     if current_button_state != last_button_state:
  77.         # Debounce: wait for debounce_time before considering a new button press
  78.         time.sleep(debounce_time)
  79.  
  80.         # Check button state again
  81.         if current_button_state == button.value:
  82.             toggle_state()
  83.             print(state)
  84.  
  85.     last_button_state = current_button_state
  86.  
  87.     # Update LED animations based on state
  88.     if state == 0:
  89.         strip.fill(0)  # Turn off LEDs
  90.         strip.show()
  91.     elif state == 1:
  92.         sparkle_pulseRedLow.animate()
  93.         sparkle_pulseYellowLow.animate()
  94.     elif state == 2:
  95.         sparkle_pulseRed.animate()
  96.         sparkle_pulseYellow.animate()
  97.     elif state == 3:
  98.         pulseOrange.animate()
  99.     elif state == 4:
  100.         colorcycleGreenYellow.animate()
  101.     elif state == 5:
  102.         colorcycleAll.animate()
  103.     elif state == 6:
  104.         rainbow_comet.animate()
  105.     elif state == 7:
  106.         rainbow_cometBounce.animate()
  107.     elif state == 8:
  108.         rainbow_sparkle.animate()
  109.     elif state == 9:
  110.         animationsRandom.animate()
  111.     elif state == 10:
  112.         solidWhite.animate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement