Looping smoothly from one end of a ring to another when using distance from a variable to draw LEDs

Hopefully this question makes sense. I have a ring of LEDs (a strip that has its ends touching). I’m trying to do the typical if (abs(index-variable1) < variable2 to determine which LEDs are within a set distance from an index number, but it doesn’t jump smoothly around the corner where the indices go from 299 to 0.

I’m guessing this is a really common problem and there is a relatively simple way of doing the math so that it doesn’t matter, but I end up with a convoluted mess that doesn’t actually work.
This is where I got before deciding that there must be a better way to do this and to just post here:

I know that fastpulse running on a 1D strip does what I’m talking about, but looking at the code I’m not sure the reason it does applies to what I am trying to do.

I haven’t tried it, but how about something like:

dist = abs(index - i)
if (dist <= range || (pixelCount - dist) <= range) { ... }
3 Likes

Thank you so much. That very much works and was really the last thing I needed to make my entire prototype actually work. With about 20 minutes to spare before game night (this is for a turn indicator for my boardgame table).

2 Likes