|
| 1 | +# SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | + |
| 7 | +import board |
| 8 | +import neopixel |
| 9 | + |
| 10 | +try: |
| 11 | + import urandom as random # for v1.0 API support |
| 12 | +except ImportError: |
| 13 | + import random |
| 14 | + |
| 15 | +numpix = 36 # Number of NeoPixels |
| 16 | +pixpin = board.D1 # Pin where NeoPixels are connected |
| 17 | +strip = neopixel.NeoPixel(pixpin, numpix, brightness=1.0) |
| 18 | + |
| 19 | +mode = 0 # Current animation effect |
| 20 | +offset = 0 # Position of spinner animation |
| 21 | +color = [160, 0, 160] # RGB color - purple |
| 22 | +prevtime = time.monotonic() # Time of last animation mode switch |
| 23 | + |
| 24 | +while True: # Loop forever... |
| 25 | + |
| 26 | + if mode == 0: # Random sparkles - lights just one LED at a time |
| 27 | + i = random.randint(0, numpix - 1) # Choose random pixel |
| 28 | + strip[i] = color # Set it to current color |
| 29 | + strip.write() # Refresh LED states |
| 30 | + # Set same pixel to "off" color now but DON'T refresh... |
| 31 | + # it stays on for now...bot this and the next random |
| 32 | + # pixel will be refreshed on the next pass. |
| 33 | + strip[i] = [0, 0, 0] |
| 34 | + time.sleep(0.008) # 8 millisecond delay |
| 35 | + elif mode == 1: # Spinny wheel (4 LEDs on at a time) |
| 36 | + for i in range(numpix): # For each LED... |
| 37 | + if ((offset + i) & 7) < 2: # 2 pixels out of 8... |
| 38 | + strip[i] = color # are set to current color |
| 39 | + else: |
| 40 | + strip[i] = [0, 0, 0] # other pixels are off |
| 41 | + strip.write() # Refresh LED states |
| 42 | + time.sleep(0.08) # 80 millisecond delay |
| 43 | + offset += 1 # Shift animation by 1 pixel on next frame |
| 44 | + if offset >= 8: |
| 45 | + offset = 0 |
| 46 | + # Additional animation modes could be added here! |
| 47 | + |
| 48 | + t = time.monotonic() # Current time in seconds |
| 49 | + if (t - prevtime) >= 8: # Every 8 seconds... |
| 50 | + mode += 1 # Advance to next mode |
| 51 | + if mode > 1: # End of modes? |
| 52 | + mode = 0 # Start over from beginning |
| 53 | + strip.fill([0, 0, 0]) # Turn off all pixels |
| 54 | + prevtime = t # Record time of last mode change |
0 commit comments