Best coding approach to solid colour specific LEDs?

I have a use case to solid colour multiple specific sections of a strip. I will have a number of different patterns doing this that I will switch between.

I have it working, but I wonder if there’s a more efficient way I could or should be doing it - it’s not like I am specifying each individual LED, but I feel like it could be ‘tighter’.

Any advice appreciated.

export function render(index) {
if (index > 15 && index < 29 ) {  
    rgb(0, 1, 0)   
  } 
else if (index > 29 && index < 70 ) {  
    rgb(0, 0, 1)   
  } 
else {
  rgb(0, 0, 0)
}
}

It’s hard to get simpler than that …but I suppose you could run the code once, then just look it up in the render function. This sort of precalculation is handy for intensive patterns, but for static patterns … well I guess you are doing a bit less work per frame so your pixelblaze will be a bit cooler. :sunglasses:

It would go a bit like this:

colors = Array(pixelCount)

for ( index = 0; index < pixelCount; index++ ) {
  if (...) {
    colors[index] = [0,0,1]
  } else {
    ...
  }
}

export function render(index) {
  color = colors[index]
  rgb( color[0], color[1], color[2] )
}
1 Like

If you’re just coloring a small number of regions like this, I think what you did is completely reasonable.

If you’d like to specify the length of the regions in a unit of length instead of via pixel index, you could make a little helper function that converts lengths to indices for your particular pixel density.

If you were coloring a larger number of regions, you could consider an array-based approach. One array could specify the starting index or length of each region, and additional arrays encode whatever H, S, or V information you’d like to render for that segment.