I am trying to turn on LEDs one after the other so that I can start at one end and slowly turn the LEDs until it reaches the middle of the strip. The code works coming from the end of the LED strip, but not when coming from the start of the strip. From the start of the strip, it just turns on all the LEDs up until the midpoint.

int midpoint = 30;
int R = 255;
int G = 0;
int B = 0;
for(int j = 0; j<midpoint; j++){
     //starting from the end, filling towards the middle
        for(int k = NUM_LEDS-j; k < NUM_LEDS; k++){
           leds[k] = CRGB (R, G, B);
//there's some other code here in the interim
     //starting from the beginning, filling towards the middle
     //this is the section of code that doesn't work correctly
        for(int l = j - 1; l < midpoint; l++){
           leds[l] = CRGB (R, G, B);
//more interim code
  • which number does the for loop start with now? .... change it so that it coun ts in the opposite direction
    – jsotola
    May 18, 2018 at 6:43
  • Ooo I just thought about how l is increasing through the loop instead of decreasing. I solved it by changing it to for(int l = j; l < midpoint & l > -1; l--)
    – David
    May 18, 2018 at 6:51
  • If you have solved it, you can post an answer to your own question. If you don't, the question will stay here, unanswered.
    – Nick Gammon
    May 18, 2018 at 7:06
  • You should fix your indentation. It's very hard to tell what code is inside the outer j loop since some of that code isn't indented.
    – Duncan C
    Sep 12, 2019 at 0:28
  • You need to add more explanation. What framework you're using, and how the leds array relates to your FastLED.show() function. Does it show the LEDs that are in your leds array? What turns the LEDs off again?
    – Duncan C
    Sep 12, 2019 at 0:31

1 Answer 1


Okay so i figured it out. The problem is that the for loop is starting at 0 and counting all the way up to the midpoint. What I wanted is for it to light all the LEDs that are at j and below. So I changed it to iterate down instead of up. Here's the new loop:

for(int l = j; l < midpoint & l > -1; l--){
   leds[l] = CRGB (R, G, B);

The extra qualifier l > -1 is to make sure that it doesn't try to go below leds[0]

  • l >= 0 is more natural than l > -1, the compiler will probably optimise it to the same thing. May 18, 2018 at 12:10
  • 1
    +1 for taking the time to post the solution to your problem. Most new members don't do that.
    – Duncan C
    Sep 12, 2019 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.