LED Strip Sketch breaking...

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();   
  }

  
}

Any help would be appreciated.

I expect that this is writing off the end of your array:

       leds[NUM_LEDS + dot] = CRGB::Black;

Probably intended:

       leds[NUM_LEDS - dot + 1] = CRGB::Black;

But that writes off the end on the first iteration of that loop too. As does the first loop.

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.

Regards,
Ray L.

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);

  • FastLED.show();*
  • delay (5);*
    if (i>=1) {
  • leds[i-1] = CRGB(0,0,0);*
  • FastLED.show();*
  • }*
    }

Getting rid of that FastLED in favor of a library that does that error checking for you (and manages the buffer itself) like neopixel would work.

I think this should do it:

void loop()
{
  //Lighting the way up...
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(0, 0, 255);
    if (i != 0) leds[i - 1] = CRGB(0, 0, 0);
    FastLED.show();
    delay(50);
  }

  ///Lighting the way back...
  for (int dot = NUM_LEDS - 1; dot >= 0 ; dot--) {
    leds[dot] = CRGB::Blue;
    if (dot != NUM_LEDS) leds[dot + 1] = CRGB::Black;
    FastLED.show();
    delay(50);
  }
}

You don't need to FastLED.show each time as your delay(5) is too small for you to see anything. Just do the show when all leds are set.

Anyways, you should add some delays inside the loops to slow all the thing down. I used 50 ms, set it as you want.

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);
}

Untested and possibly bugged.