I’m new to FastLED and to coding. I’ve got a successful program going, but would like to make one change that I haven’t been able to figure out. Perhaps someone here would be able to help me?
I would like a blue LED to fade through a set of green LEDs from left to right, and then repeat. I’ve gotten that to happen, but every time it cycles through, on the leftmost LED the blue pops on instead of fading on. I’d like it to fade on. Does anyone know how I could get that to happen?
Here’s the code on Gist:
(Note: From GitHub Gist I Shared the gist URL instead of Embedding it. Please let me know if I should have Embedded it instead.)
I’m using FastLED 3.1.0, Arduino Uno, Mac OS 10.9.5, NeoPixels from Adafruit.
I think what you’re seeing is when x is at it’s max then leds[x+1] is going to be invalid since it will be trying to reference a pixel beyond what is defined as the total number of pixels. To fix this you need some sort of check to prevent it from going beyond the end, and instead “wrap” back around to the beginning of the strip. You could use an if statement to check this and then adjust accordingly. Another way is to use modulo. https://www.arduino.cc/en/Reference/modulo
Try changing leds[x+1]
to
leds[(x+1) % NUM_LEDS]
(Edit: corrected this to a % sign, not &)
Here’s an example using modulo (setup to also allow reverse direction).