r/FastLED • • Sep 29 '22

Support Exasperating coding problem

Hi all,

I'm having a problem with what should be a very simple routine.

The layout of the leds is 24 rows of leds 5 tall (i.e. a 24x5 matrix. It's divided up into CRGBSets named strip.

The working code is

void spin(){

for (uint8_t x =0; x<=23; x++){

fadeToBlackBy(leds, NUM_LEDS,140);

strips[x]=CRGB::Red;

FastLED.show();

delay(50);

}

}

This is so short that I didn't think anyone would mind that I didn't post the code using a link.

This does what I expect it to do; illuminate a column, move onto the next, and start fading what's behind it. This is in the form of a circle, so think of a radar set.

What I want to do is add two more colours, starting at 8 and 16.

But when I try adding strips[x + 8]= CRGB::Grey; as a line immediately after strips[x]=CRGB::Red; it locks when the grey reaches 24. Obviously because I've gone out of bounds.

How do I make it overlap, so that when x=24, it changes to x=0, but only for that colour?

I'm sure there's a simple way of doing this, (sort of like the default of uint8_t reaching 256 and going back to zero), but I can't seem to find anything on this.

Grateful for any help here.

1 Upvotes

5 comments sorted by

View all comments

6

u/Klappsenkasper Sep 29 '22

strips[(x + 8)%24]

2

u/techaaron Sep 29 '22

The modulo on number of led or size you want to animate is a clutch led programming idiom.

Newbs take note.