I want to create a very simple sketch, light "travels" up a led strip and then back down. Getting it to travel up, works with no issue. I light up each LED in a loop and wait a second and then turn it off. Going back down is proving to be a problem. The sketch "craps out" on it's way back down. Lights just freeze. Here's loop from the sketch, I'm pretty sure I'm just missing some small:
void loop()
{
//Lighting the way up...
for(int i = 0; i < NUM_LEDS; i=i++ )
{ leds[i] = CRGB(0,0,255);
FastLED.show();
delay (5);
leds[i-1] = CRGB(0,0,0);
FastLED.show();
}
///Lighting the way back...
for(int dot = 0; dot < NUM_LEDS; dot++)
{
leds[NUM_LEDS - dot] = CRGB::Blue;
FastLED.show();
delay(5);
leds[NUM_LEDS + dot] = CRGB::Black;
FastLED.show();
}
}
Whole lotta writing to non-existent LEDs in there...
In your first loop, you're writing leds[i-1], which writes (non-existent) leds[-1] when i == 0.
In your second loop, you're writing leds[NUM_LEDS + dot], which writes (non-existent) leds[NUM_LEDS] through leds[NUMLEDS + NUM_LEDS), depending on the value of dot.
Any idea how to resolve properly, do I have to write a seperate if statement to prevent i from going to "non-existent leds, like led -1. Would something like this work...
for(int i = 0; i < NUM_LEDS; i=i++ )
{ leds = CRGB(0,0,255);
You could also get rid of the for loop if all you are doing is flashing an LED back and forward on an LED strip
int i = 0;
bool ledDirection = true;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
led[i] = CRGB::Blue;
FastLED.show();
//We can immediately assign the value to black but it won't show
//until we call FastLED.show() again
led[i] = CRGB::Black;
if(ledDirection){i++;}
else{i--;}
if(i == NUM_LEDS){
ledDirection = false;
i = NUM_LEDS - 2;
}
if(i == -1){
ledDirection = true;
i = 1;
}
delay(50);
}