Lambda effect does not work - help needed

Hello together,

I’m working on my first lambda effect, but can’t get it work. My c-programming times are already a few years ago - so maybe there is just a stupid mistake…

For this effect I have 10 LEDs. I would like to program a running light. However, the “fade-in” time should be around 500ms. When the brightness of the first LED is at 25, the second should start to get brighter, when this is at 25, the third starts and so on. So that when the first one is approximately at full brightness, the 10th one just starts to get brighter. The first LED is the Last in the Array, so I’ve to count backwards.

This is what I have so far:

  - platform: partition
    name: "LED Partition Stairs"
    id: LedPartitionStairs
    segments:
      - id: FlurGrossRGB
        from: 11
        to: 24
    effects:
     - addressable_lambda:
          name: Staireffect
          update_interval: 2ms # 2ms * 255(max brightness) = 510ms
          lambda: |-
                static int amount  = 0;
                static int x = 0;
                static int led[10] = {0}; //for calculating the brighness per LED channel

                if (initial_run) {
                  amount = it.size() - 1;
                  it.all() = Color(0,0,0);
                }

                for (x = it.size() -1; x >= 0; --x){ //count down from highest led to lowest
                  if(x == it.size() - 1){ //If it is the first (or highest number) just count up
                    ++led[x];
                    if (led[x] >= 255){ //Limit to 255
                      led[x] = 255;
                    }
                  }
                  else{
                    if(led[x+1] > 25){ //If the previous LED is above 25 start counting up
                      ++led[x];
                      if(led[x] >= 255){ //limit to 255
                        led[x] = 255;
                      }
                    }
                  }
                }
                for (x = 0; x <= it.size() - 1; ++x) {
			       it[x] = Color(led[x],led[x],led[x]); //set the calculated value for each led
		        }

What I see is, that only the first LED light up, at full brightness, but then nothing happens anymore. I tried to set update_intervall at 16ms to see some progress, but nothing changed.

What am I missing here? I appreciate every help!!!

Any positive number finishes the loop, any negative makes it infinite.

Sorry, but I don’t get what is wrong with the for Loop?
#1: x = it.size() -1 => 10 LEDs
#2: loop as long as x >= 0
#3: increment by -1

Of course, you are right.
My mistake, shouldn’t post while busy with other things. :roll_eyes:
Try to add some logger print on your code.

Because it’s from 11 to 24, that’s 14 leds. So it.size() returns 14. Which means that you grossly write out of bound for led[].

So instead of it.size() i would use the size of the actual array (sizeof(led)/sizeof(led[0] or just sizeof(led) and make led an int8_t).

And I always find it a bit confusing to use a singed index. I would just index from 0 to it.size() -1 and just only flip it when writing to the leds.

Yeah, I forgot to change the Amount of LEDs - gut this wasn’t the Problem.

The Solution I found, was that the initialization of the int-Array doesn’t seem to work:

static int led[10] = {0};

After debugging I found, that the Array Elements where prefilled with bad values, so what I did was:

if (initial_run) {
  it.all() = Color(0,0,0);

  for (x = 0; x < it.size(); ++x){
    led[x] = 0;
  }
}

After this, the effect started to work without problems!

For those who are interessted, this is the complete effect (a very smooth running lite):

- addressable_lambda:
          name: Treppeneffekt
          update_interval: 2ms
          lambda: |-
                static int x = 0;
                static int led[10] = {0};

                if (initial_run) {
                  it.all() = Color(0,0,0);

                  for (x = 0; x < it.size(); ++x){
                    led[x] = 0;
                  }
                }

                for (x = it.size() -1; x >= 0; --x){
                  if(x == it.size() - 1){
                    ++led[x];
                    if (led[x] >= 255){
                      led[x] = 255;
                    }
                  }
                  else{
                    if(led[x+1] > 25){
                      ++led[x];
                      if(led[x] >= 255){
                        led[x] = 255;
                      }
                    }
                  }
                }
                for (x = 0; x < it.size(); ++x){
                  it[x] = Color(led[x],led[x],led[x]); 
                }

It runs from highest number LED to lowest - if you want to change that, you’ve to change the second for-loop…

would it work if you remove the zero? {}