3

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.

Z B
  • 43
  • 3

2 Answers2

2

Let's first set the pattern for us to use in each of the following solutions and a helper function:

pattern = [
    (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)
    ]

def roundup(numerator, denominator):
    return (numerator + denominator - 1) // denominator

The roundup function is based on this answer which is applicable here since it's being applied to variables derived from length, which will be ints.

1) You can pack the pattern up very easily like this, which should be flexible to differing length patterns or LED strips:

len_strip = 288
len_pattern = len(pattern)

pixels = (pattern * roundup(len_strip,len_pattern))[:len_strip]

2) Here is a way you can loop over this pattern (rather than the whole array):

pixels = [0]*288

for i in range(len(pixels)//12):
    pixels[12*i:12*i+12] = pixel_pattern

This packs it up so you are looping over the pattern instead of looping over each pixel.

3) If you want to pack it up to be deal with a varying length of pattern or of LED array, you could try something more flexible like this:

pixels = [0]*288
strip_len = len(pixels)

pattern_len = len(pixel_pattern )

for i in range(roundup(len(pixels),pattern_len)):
    chunk_start = pattern_len*i
    chunk_end = chunk_start + pattern_len
    if chunk_end<strip_len:
        pixels[chunk_start:chunk_end] = pixel_pattern
    else:
        chunk_end = strip_len
        pixels[chunk_start:chunk_end] = pixel_pattern[0:chunk_end-chunk_start]

4) If you wanted to address each one while still looping over the pattern, you can also do it this way:

pixels = [0]*288

for i in range(len(pixels)//12):
    pixels[12*i+0] = (25, 255, 255)
    pixels[12*i+1] = (25, 255, 255)
    pixels[12*i+2] = (25, 255, 255)
    pixels[12*i+3] = (25, 255, 255)
    pixels[12*i+4] = (155, 155, 155)
    pixels[12*i+5] = (155, 155, 155)
    pixels[12*i+6] = (0, 0, 255)
    pixels[12*i+7] = (0, 0, 255)
    pixels[12*i+8] = (0, 0, 255)
    pixels[12*i+9] = (0, 0, 255)
    pixels[12*i+10] = (155, 155, 155)
    pixels[12*i+11] = (155, 155, 155)
1

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.

Adrian
  • 177
  • 13