# Candy Cane pattern for LED Strip Lights

**URL:** <https://forum.electromage.com/t/candy-cane-pattern-for-led-strip-lights/1806>\
**Category:** Uncategorized\
**Created:** [December 7, 2021, 3:40am UTC](https://forum.electromage.com/t/candy-cane-pattern-for-led-strip-lights/1806 "2021-12-07T03:40:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jsharkbite](https://forum.electromage.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@Jsharkbite](https://forum.electromage.com/u/Jsharkbite)\
**Post date:** [December 7, 2021, 3:40am UTC](https://forum.electromage.com/t/candy-cane-pattern-for-led-strip-lights/1806/1 "2021-12-07T03:40:50Z")

</div>

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

```auto
 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?

---

<div class="post-metadata">

**Author:** ![jeff](https://forum.electromage.com/user_avatar/forum.electromage.com/jeff/32/3473_2.png) [@jeff](https://forum.electromage.com/u/jeff)\
**Post date:** [December 7, 2021, 6:03am UTC](https://forum.electromage.com/t/candy-cane-pattern-for-led-strip-lights/1806/2 "2021-12-07T06:03:49Z")

</div>

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](https://electromage.com/patterns).

Then configure it for these settings:

 ![Railing 2021-12-06 21-40-42](https://forum.electromage.com/uploads/default/original/2X/0/01a3c2090bab0a33e886f4b9f57cf345587e341f.png)

Viola:

 ![IMG_0589 copy](https://forum.electromage.com/uploads/default/original/2X/1/1df74bdd622e6dbe529ef881b8fd29b3dbc6d36a.jpeg)  
_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: `%`.

```javascript
( 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!

---

<div class="post-metadata">

**Author:** ![Jsharkbite](https://forum.electromage.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@Jsharkbite](https://forum.electromage.com/u/Jsharkbite)\
**Post date:** [December 7, 2021, 12:08pm UTC](https://forum.electromage.com/t/candy-cane-pattern-for-led-strip-lights/1806/3 "2021-12-07T12:08:07Z")

</div>

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.

```auto
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!!!

---

<div class="post-metadata">

**Author:** ![Scruffynerf](https://forum.electromage.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@Scruffynerf](https://forum.electromage.com/u/Scruffynerf)\
**Post date:** [December 7, 2021, 2:46pm UTC](https://forum.electromage.com/t/candy-cane-pattern-for-led-strip-lights/1806/4 "2021-12-07T14:46:47Z")

</div>

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.
