I have a problem with my code and I can’t see where the problem is.
Right before the void loop starts over, all the LED’s are turned red for like 2 sec. Also I can’t add more code, it’s always the same spot it turns red and starts over. As you can see at the end of the code, the first led should turn to this white´ish color for 10 sec, but it doesn’t.
I’ve already tried that, but with no luck. I just found out, that there is some sort of limitation. If I delete some code and write something else instead, it actually shows that, but then it doesn’t show the code after that again.
There will be a point where test will equal 60. leds[60] is outside of your array - which means you are overwritting memory used by something else (this is called a buffer overflow).
You have a few places in your code where you do this.
Here you’re going to over write the led array by nearly 600 bytes. You’re basically going to walk all over the world - I’m not surprised that things eat themselves.
Ok, I didn’t know that. But it’s ok to start some leds in minus? Also the last example you mentioned, was where I tried to make a int. determine the color and change it on the way. Do you know a way to do this?
I just started yesterday, so bare with me :-/
As Daniel pointed out you don’t want to write to a pixel index outside your leds array range. If you have loops or something else generating index values outside the valid range then you’ll want to put checks in place to deal with this. Something like an “if” statement could be used:
if (dot > NUM_LEDS-1) {
dot = dot - NUM_LEDS;
}
leds[dot] = CRGB::Red
or if you just wanted to clamp it to the max pixel
if (dot > NUM_LEDS-1) {
dot = NUM_LEDS-1;
}
If you want to have a loop (or some other variable) vary the color then some sort of variable needs to be part of the color assignment and change the R,G,B values. Something like:
for (int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = CRGB( dot6, 0, 255-(dot4) ); // Changes Red and Blue values.
FastLED.show();
leds[dot] = CRGB::Black;
delay(80);
}
Sometimes instead of using RGB it’s easier to work in HSV, but it depends on what makes sense to you and the project. Learn about using HSV here:
Here’s the above loop effecting the color using Hue instead of RGB:
Note: When assigning RGB or HSV values, if you assign a value outside of 0-255 it automatically will wrap it back into the 0-255 range. So while this won’t “break” anything (do anything bad in memory), it might suddenly give you colors you weren’t expecting.
Thank you so much, for that well described “tutorial”, that helped me a lot.
I Tried something similar to your color example, but with no luck. I had two different for loops, so it was a bit different though.