Search code examples
c++arduinoled

FastLED library not working when iterating through the LEDS backwards


Im having issues where my code running up the w2812b strip is working fine but when I run down the strip I get an error.

This is the error

/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c:1443 (xQueueGenericReceive)- assert failed! abort() was called at PC 0x40087f1d on core 1

I have tried looking it up and changing how I iterate through but nothing has worked so far. I have figured out that it breaks on the first run through in for loop within the ledDown function when I call the showStrip function.

The code is being uploaded to a esp32 but I'm pretty sure that doesn't have anything to do with it.

Any help would be amazing.

Here is my code. I currently just have the LedDown function commented out.

#include "FastLED.h"

#define NUM_LEDS 100
#define LEDPIN 26

CRGB leds[NUM_LEDS];


void setup()
{
  // Debug console
  Serial.begin(9600);

  FastLED.addLeds<WS2812B, LEDPIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

void showStrip() {
   FastLED.show();
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

void loop() {

  LedUp();
  //LedDown();
}

void LedDown() {
  for(int i = NUM_LEDS; i > 1; i--){
    Serial.println(i);
    setAll(0,0,0);
    delay(100);
    if(i < 33){
      setPixel(i,0,0xff,0);
    }else if(i < 63){
      setPixel(i,0xff,0,0);
    }else{
      setPixel(i,0,0,0xff);
    }
    showStrip();
  }
}

void LedUp() {
  for(int i = 0; i < NUM_LEDS; i++){
    setAll(0,0,0);
    delay(20);
    if(i < 30){
      setPixel(i,0,0xff,0);
    }else if(i < 60){
      setPixel(i,0xff,0,0);
    }else{
      setPixel(i,0,0,0xff);
    }

    showStrip();
  }
}

Solution

  • for(int i = NUM_LEDS; i > 1; i--)
    

    You need to start from NUM_LEDS - 1 and go to zero:

    for(int i = NUM_LEDS - 1; i >= 0; i--)
    

    Because NUM_LEDS itself is out of range.