This code functions correctly, but is missing the intended seeker effect for group 5.
import digitalio
import board
import neopixel
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.helper import PixelSubset
NUM_PIXELS = 126
NEOPIXEL_PIN = board.D5
POWER_PIN = board.D10
ONSWITCH_PIN = board.A1
ORDER = neopixel.GRBW
pixels = neopixel.NeoPixel(
NEOPIXEL_PIN, NUM_PIXELS, brightness=0.1, auto_write=False, pixel_order=ORDER
)
group1 = PixelSubset(pixels, 0, 7)
group2 = PixelSubset(pixels, 7, 31)
group3 = PixelSubset(pixels, 31, 38)
group4 = PixelSubset(pixels, 38, 68)
group5 = PixelSubset(pixels, 68, 96)
group6 = PixelSubset(pixels, 96, 126)
rainbow1 = Rainbow(group2, speed=0.05, period=2)
rainbow2 = Rainbow(group5, speed=0.05, period=2)
pulse1 = Pulse(group1, speed=0.1, color=(0, 0, 0, 255), period=3)
pulse2 = Pulse(group3, speed=0.1, color=(0, 0, 0, 255), period=3)
pulse3 = Pulse(group4, speed=0.1, color=(0, 0, 0, 255), period=3)
pulse4 = Pulse(group6, speed=0.1, color=(0, 0, 0, 255), period=3)
enable = digitalio.DigitalInOut(POWER_PIN)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True
while True:
rainbow1.animate()
rainbow2.animate()
pulse1.animate()
pulse2.animate()
pulse3.animate()
pulse4.animate()
This code is what I have been using to create the rainbow seeker/loading bar effect. It works almost correctly. The random
interval function does not create the intended effect. I want the number of white pixels to increase consistently by one
until all pixels in group 5 are white, then reset to rainbow and start the process over again.
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def rainbow_cycle(wait):
howmany = randint(68, 96)
for x in range((96-1), (howmany-1), -1):
pixels[x] = (255, 255, 255)
pixels.show()
for j in range(255):
for i in range(howmany):
pixel_index = (i * 256 // howmany) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
while True:
rainbow_cycle(0.001)
This code shows my incorrect attempt to combine the codes. The rainbow_cycle code interrupts the other groups
and causes several issues. I have been told the problem is in the time.sleep function, and I need to use
time.monotonic, but I don't have the knowledge to do it correctly.
import digitalio
import board
import neopixel
import time
from random import randint
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.helper import PixelSubset
NUM_PIXELS = 126
NEOPIXEL_PIN = board.D5
POWER_PIN = board.D10
ONSWITCH_PIN = board.A1
ORDER = neopixel.GRBW
pixels = neopixel.NeoPixel(
NEOPIXEL_PIN, NUM_PIXELS, brightness=0.1, auto_write=False, pixel_order=ORDER
)
group1 = PixelSubset(pixels, 0, 7)
group2 = PixelSubset(pixels, 7, 31)
group3 = PixelSubset(pixels, 31, 38)
group4 = PixelSubset(pixels, 38, 68)
group5 = PixelSubset(pixels, 68, 96)
group6 = PixelSubset(pixels, 96, 126)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def rainbow_cycle(wait):
howmany = randint(68, 96)
for x in range((96-1), (howmany-1), -1):
pixels[x] = (0, 0, 0, 255)
pixels.show()
for j in range(255):
for i in range(howmany):
pixel_index = (i * 256 // howmany) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
rainbow1 = Rainbow(group2, speed=0.05, period=2)
pulse1 = Pulse(group1, speed=0.1, color=(0, 0, 0, 255), period=3)
pulse2 = Pulse(group3, speed=0.1, color=(0, 0, 0, 255), period=3)
pulse3 = Pulse(group4, speed=0.1, color=(0, 0, 0, 255), period=3)
pulse4 = Pulse(group6, speed=0.1, color=(0, 0, 0, 255), period=3)
enable = digitalio.DigitalInOut(POWER_PIN)
enable.direction = digitalio.Direction.OUTPUT
enable.value = True
while True:
rainbow_cycle(0.001)
rainbow1.animate()
pulse1.animate()
pulse2.animate()
pulse3.animate()
pulse4.animate()
This is just a rough diagram of the circuit I am working with to visualize the specific groups I've made.
This is a visual of the seeker/loading bar effect I'm trying to create in group 5. If necessary to made the code work,
it is fine for the rainbow and white pixels to be reversed (white overtakes rainbow, or rainbow overtakes white).
As long as it is moving at a consistent speed, one pixel at a time, from left to right.