Search code examples
pythonled

dumb question: Is there a way to make a color pattern repeat every X number of LEDs? Or do I have to write out the pattern for each LED in the strip?


I have a strip of 288 addressable LEDs and it is broken up into segments of 12 LEDs each. I have already written a bunch of code for colors and patterns designed for just one segment. The single colors were easy enough to adjust to fill all of the segments but I haven't quite figured out how to do the patterns without just copy/paste and correct the pixel numbers. Any insight is greatly appreciated, i'll attach a copy of a pattern to use as an example. (all of the patterns are pretty simple, basically two to three colors broken up across the segment)

I've just tried a bit of googling and using what i already know to try to come up with a way to make it work.

import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.D18, 288)

pixels[0] = (25, 255, 255)
pixels[1] = (25, 255, 255)
pixels[2] = (25, 255, 255)
pixels[3] = (25, 255, 255)
pixels[4] = (155, 155, 155)
pixels[5] = (155, 155, 155)
pixels[6] = (0, 0, 255)
pixels[7] = (0, 0, 255)
pixels[8] = (0, 0, 255)
pixels[9] = (0, 0, 255)
pixels[10] = (155, 155, 155)
pixels[11] = (155, 155, 155)

I would like to get this pattern to repeat across the entire strip of 288 LEDs.


Solution

  • Maybe something like this would work for you:

    pixel_config = [
        (25, 255, 255), 
        (25, 255, 255), 
        (25, 255, 255), 
        (25, 255, 255), 
        (155, 155, 155),
        (155, 155, 155),
        (0, 0, 255),
        (0, 0, 255),
        (0, 0, 255),
        (0, 0, 255),
        (155, 155, 155),
        (155, 155, 155)
    ]
    
    runningIdx = 0
    endingIdx = len(pixel_config)
    for i in range(288):
        # Start new sequence if end is detected
        runningIdx = 0 if runningIdx == endingIdx else runningIdx
    
        pixels[i] = pixel_config[runningIdx]
        runningIdx += 1
    

    Essentially utilizes a running index to keep track of which configuration to set for a given pixel and resets as necessary when the final configuration has been set to begin setting the colors for the next sequence of pixels.