Candy Cane pattern for LED Strip Lights

So I’ve been struggling trying to figure out how to code an alternating pattern of white and red with a space between each for my roofline.
I started with the example code

 export function render(index) {
   if (index == 1)   
     rgb(1, 1, 1)   // white 
   else if (index == 3) {
    rgb (1, 0, 0)   // red
   } else {
     rgb(0, 0, 0)     
   }
 }

But I no matter what i’ve tried I can’t figure out how to get index 5 to be white and 7 to be red etc etc. WHILE keeping 1 and 3 positions on. Is there an if (index == 1, 5, 9.…etc) way to do it??
Anyone know to do this?

Hi @Jsharkbite!

I’ll also help you with your code, but first I wanted to let you know about a pattern I made to do stuff exactly like this using Pixelbalze’s built-in UI controls.

First, grab the “Custom sequences” pattern. It’s currently near the bottom of the first page in the pattern library.

Then configure it for these settings:

Viola:


The ones that look purple in this pic are actually fully off

OK, but what fun is Pixelblaze if we’re not relearning a little math that we all used to know once upon a time? Let’s figure out how to make your code make index 1, 5, 9, etc white, and 3, 7, 11 etc all red. You’re very close.

It all comes down to this: What do 1, 5, 9, 13 etc all have in common?

Remember the idea of a reminder from math? If we divide all those numbers above by 4, the fractional part will be .25. But with integer math in a system with a decimal point, like Pixelblaze, we can instead say that when you divide any of those numbers by 4, the remainder is 1. For example, 13 is made up of 3 groups of 4, and then 1 more. In Pixelblaze, we get the remainder by using the percent sign operator: %.

( 9 % 4 == 1)  // This is a true condition
( 10 % 4 == 2) // Also true
( 12 % 4 == 0) // True
( 13 % 4 == 3) // False

So, armed with this, I think you could add just a few characters to your code and get what you want!

3 Likes

Well I’ll be a monkey’s uncle…I love the custom UI on that pattern! I now have a twinkling candy cane thanks to your code!

Also, very appreciative of the challenge. I may not be the smartest…but I do enjoy achievable challenges. I can now add the % concept to my toolbelt.

export function render(index) {
   if (index %4 == 1)   
     rgb(1, 1, 1)   // white 
   else if (index %4 == 3) {
    rgb (1, 0, 0)   // red
   } else {
     rgb(0, 0, 0)     
   }
 }

Thanks again Jeff!!!

3 Likes

If you want the pattern to shift (slow, fast, reverse, whatever), just add a offset variable (set it in beforeRender(), changing based on time() or delta, etc). And then use (index+offset)%

And it’ll change position. The static look is good too, but most folks like some changing to their lights.