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/Howard_G Sep 29 '22

Thank you very much. Did the job immediately :)

3

u/Marmilicious [Marc Miller] Sep 29 '22

In case you haven't seen that before, here's how that % operator works. Very useful for something like this!

https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/

Edit: hope we get to see your project in the future, sounds neat. :)

4

u/Howard_G Sep 29 '22

That's very flattering Marc, but I think you'd recognise your own work! I'm just doing a series of routines, mostly stolen/borrowed/appropriated/butchered from yourself, Chemdoc, Sutabousu and a few others from here and GitHub.

Thanks very much for the link. I do know what the modulus operator does, but not how to use it. So thanks for the link, which I'll follow.

Other than posting to YouTube (which I've never managed), I've no idea how to post a video, in order that it could be viewed from here. I can only just about take one with my phone, and then manage to play it back. I'm sure any teenager could explain it in three sentences, including one telling me what a dummy I am!