LED Run Repeats Unexpectedly

I'm trying to program an LED run where, starting with LED 1 and ending with LED 50 they light up in sequence and stay on. For some reason, after the run completes the first time, the lights turn off and the run repeats. Then everything stays lit. I cannot for the life of me figure out why.

I'm using an Uno R3 board and a WS2812B string of 150 LEDs.

Small update: It's only the first time after I send the code to the board that it throws an error. Hitting reset on the board doesn't have this problem.

#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    51


CRGB leds[NUM_LEDS];

void setup() {

int SL = 0;


  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  FastLED.clear();
  FastLED.show();

while (SL <=49) {

  SL=SL+1;


    leds[SL] = CRGB ( 255, 255, 255);
    FastLED.show();
    delay (20);}
}
void loop() {
}

Any good reason for not using a for loop?

If something in setup() repeats, it’s because the board has reset

you have 50 LEDs, 0-49. what happens if you attempt to turn on the 51st LED, #50?

look at your loop

How are you powering all the LEDs? Hopefully not using the Arduino.

I have tried a for loop and got the same result. The LEDs are being powered by a 5V, 15A power brick. I have a string of 150 LEDs, I've declared through 51 in the code and only make use of 0-49.

There seems to be an error that happens when the code is run for the first time after compiling. When I press the reset button on the board the code runs as intended.

#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    51

CRGB leds[NUM_LEDS];

void setup() 
{
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.show();
  for (int SL = 0; SL < 50; SL++) {
    leds[SL] = CRGB ( 255, 255, 255);
    FastLED.show();
    delay (20);
  }
}
void loop() 
{}

Uncompiled, untested.

Please report back

This gives me the same error after compiling. I'm guessing its something to do with the data transfer rather than the code itself, since it only happens the first time after compiling.

Is this error that you refer to a message on the screen of the IDE or just the fact that the code does not do what you want ?

No, I'm sorry - I'm assuming an error because the code doesn't complete running and starts over. There is no message. Poor wording on my part.

Based on your description, the code works just fine when you press the reset?

But it runs again when you compile and upload?

Most likely, that is just an artifact of the uploading process. When you upload code, your PC resets the board so the bootloader on the board has a chance to "see" if the PC is attempting to upload code. If yes, it accepts the new code. If not, it times out and then executes the code already on the board.

After the upload is complete, the board is reset again to begin execution of the new code. Depending on the timing of everything, your board is probably starting to run the existing code and then gets reset to upload the new code and then runs that so you are seeing the old code start and the new code run.

You can test this out by changing the color of the LEDs to something besides 255,255,255. Maybe red (255,0,0).

Make the change and upload the new code and see what you get - white LEDs the first time followed by red LEDs the second time?

Or just have the first line of the setup() function be:

   delay(100);

Thank you for the explanation! Now that I know what's happening, I can work around it. Much appreciated!

Delay did the trick. Thank you!