ESP32 + FastLED + Task Scheduler + For loop Issue

I just spent about three hours tracking this down. I have this issue narrowed down to the point where my knowledge can no longer continue. and, amazingly I see no similar issues online

So for some background:
I am using an LED stick of 8 WS2812B LED's.
I simply want to turn them all on to either green or red.
then I wait some time and Turn them off repeat.
you know... blink

The following code causes an error (shown below the snippet)

inline void allGreen() {
  digitalWrite( 2, HIGH );
  Serial.println("Trying to set LED's Green");
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i].r = 0;
    leds[i].g = 255;
    leds[i].b = 0;
    Serial.println(i);
  }
  Serial.println("LED's set to Green");
}

22:04:41.498 -> Trying to set LED's Green
22:04:41.498 -> 0
22:04:41.498 -> 1
22:04:41.498 -> 2
22:04:41.498 -> 3
22:04:41.498 -> 4
22:04:41.498 -> 5
22:04:41.498 -> 6
22:04:41.498 -> 7
22:04:41.498 -> 8
22:04:41.498 -> LED's set to Green
22:04:41.498 -> FastLED Show
22:04:41.498 -> Guru Meditation Error: Core  1 panic'ed (InstrFetchProhibited). Exception was unhandled.
22:04:41.498 -> Core 1 register dump:
22:04:41.498 -> PC      : 0x00000081  PS      : 0x00060330  A0      : 0x800d104d  A1      : 0x3ffb1f70  
22:04:41.498 -> A2      : 0x3ffbff50  A3      : 0x00000001  A4      : 0x00000081  A5      : 0x00000003  
22:04:41.498 -> A6      : 0x00000003  A7      : 0x00000000  A8      : 0x800d0f68  A9      : 0x00000102  
22:04:41.498 -> A10     : 0x00000081  A11     : 0x00000081  A12     : 0x00000081  A13     : 0x00000003  
22:04:41.545 -> A14     : 0x00000001  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x00000014  
22:04:41.545 -> EXCVADDR: 0x00000080  LBEG    : 0x40084ed8  LEND    : 0x40084ee7  LCOUNT  : 0x00000000  
22:04:41.545 -> 
22:04:41.545 -> ELF file SHA256: 0000000000000000
22:04:41.545 -> 
22:04:41.545 -> Backtrace: 0x00000081:0x3ffb1f70 0x400d104a:0x3ffb1f90 0x400d1f69:0x3ffb1fb0 0x40086485:0x3ffb1fd0
22:04:41.545 -> 
22:04:41.545 -> Rebooting...

What is interesting here is that it gets past the for loop and gets to the next task in the task scheduler which threw me off for a while but I am certain it is in the for loop because the following works.

inline void oneGreen() {
  digitalWrite( 2, HIGH );
  leds[0].r = 0;
  leds[0].g = 255;
  leds[0].b = 0;
}

I have tried this with the following changes and none work:
commenting out FastLED.show()
Using an integer instead of NUM_LEDS to iterate
Adding a 2ms delay in between each assignment in the for loop
Different methods for assigning the LED colors

Entire code here:

#include <TaskScheduler.h>
#include <FastLED.h>



#define NUM_LEDS 8  //number of leds in LED stick
#define DATA_PIN 23 // data pin for LED Stick I2C bus

Scheduler ts;
CRGB leds[NUM_LEDS];


int LED_state;


void LEDloop(); // <-------------------------------------------|
Task tBlink1 ( 500 * TASK_MILLISECOND, TASK_FOREVER, &LEDloop, &ts        , true );
void foreGround(); // <-------------------------------------------|
Task taskForeground ( 5 * TASK_MILLISECOND, TASK_FOREVER, &foreGround, &ts        , true );



void setup()
{
  pinMode(2, OUTPUT);
  LED_state = 0;
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(10);
  Serial.begin(115200);
}

void loop()
{
  ts.execute();
}


inline void oneOff() {
  digitalWrite( 2, LOW );
  leds[0].r = 0;
  leds[0].g = 0;
  leds[0].b = 0;
}
inline void oneGreen() {
  digitalWrite( 2, HIGH );
  leds[0].r = 0;
  leds[0].g = 255;
  leds[0].b = 0;
}

inline void allRed() {
  Serial.println("Trying to set LED's Red");
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i].r = 255;
    leds[i].g = 0;
    leds[i].b = 0;
    Serial.println(i);
  }
  Serial.println("LED's set to RED");
}

inline void allGreen() {
  digitalWrite( 2, HIGH );
  Serial.println("Trying to set LED's Green");
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i].r = 0;
    leds[i].g = 255;
    leds[i].b = 0;
    Serial.println(i);
  }
  Serial.println("LED's set to Green");
}

inline void allOff() {
  digitalWrite( 2, LOW );
  Serial.println("Trying to set LED's Off");
  for (int i = 0; i <= NUM_LEDS; i++) {
    Serial.println(i);
    leds[i].r = 0;
    leds[i].g = 255;
    leds[i].b = 0;
  }
  Serial.println("LED's set to off");
}



void LEDloop()
{
  Serial.println("LEDloop");


  flashGreen();
  Serial.println("FastLED Show");
  FastLED.show();
}








void flashGreen()
{
  Serial.println("Flash Green");
  if ( LED_state != 1 )
  {
    oneGreen();
    LED_state = 1;
  }
  else
  {
    oneOff();
    LED_state = 0;
  }
}


void foreGround()
{
  //Serial.println("Foreground");

}

This writes NUM_LEDS+1 pixels, probably not what you wanted.

As the index starts with 0, the highest correct index would be NUM_LEDS-1, not NUM_LEDS.

Oi! When you loaded up the ESP32 Exception Decoder and pasted the backtrace info into the ESP32 Exception Decoder, what did it give you?

I've found that this means you are using a unallocated memory address, like going off the end of an array.

AAAAHHHHHH...Argggghhhh, What ESP Exception Decoder? Look it up on the internet.

Whandall
I think this is probably the issue. The symptoms at least make sense. I do have the for loop in another program that does work but the other variables used here must push this around enough that calling outside of it is in forbidden memory locations? I can test tonight. Can you think of why it is able to get past the for loop and only fails after if this is the issue? I struggle with that aspect as there is no parallel operation here.

If you overwrite an array, everything is possible.
You don't know which variables happen to be directly behind the array,
if you destroy them, you will notice it only if these are used,

Yep that simple change did it. Of course it was my issue but I wonder if this can be improved in the FASTLED library? seems it would be easy check to not allow writing of LED Data outside of the defined LED ARRAY?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.