FASTLED help with splitting a strip into segments

hi guys,

I want to split a single strip of WS2812B LEDs into 4 sections and 4 (or more) instances of the same fire pattern on each section. My code is based on the fastled 2012 example.

I have managed to rewrite the fire function (now called engine) so I can define the start and end of the 8 LED sections and everything works when I call it up to three times but when I add the fourth call to the function it stops working.

if I remove the first 3 calls to ENGINE() which work and leave only ENGINE(EngineSize4,EngineStart4,EngineEnd4); I can only get it to work when the EngineEnd4 variable is 24 or below. if it is above 24 the whole strip fails to light up.

I would love some help to identify my mistake please.

#include <FastLED.h>

#define LED_PIN     2
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS    36       //LEDS on strip 

#define ENGINE_1  8
#define ENGINE_2  8 


///////Engine 1///////
#define EngineSize1 8
#define EngineStart1 0
#define EngineEnd1 8

///////Engine 2///////
#define EngineSize2 8
#define EngineStart2 8
#define EngineEnd2 16

///////Engine 3///////
#define EngineSize3 8
#define EngineStart3 16
#define EngineEnd3 24

///////Engine 4///////
#define EngineSize4 8
#define EngineStart4 24
#define EngineEnd4 32



#define BRIGHTNESS  255
#define FRAMES_PER_SECOND 100

bool gReverseDirection = false;

CRGB leds[NUM_LEDS];


CRGBPalette16 gPal;

void setup() {
  delay(300); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  
  FastLED.setBrightness( BRIGHTNESS );

  gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::OrangeRed );

FastLED.show(); // display this frame
}

void loop()
{
  // Add entropy to random number generator; we use a lot of it. 
  random16_add_entropy( esp_random());                                        // <<< use random() for ardunino esp_random() for ESP32

  //ENGINE(EngineSize1,EngineStart1,EngineEnd1);
  //ENGINE(EngineSize2,EngineStart2,EngineEnd2);
  //ENGINE(EngineSize3,EngineStart3,EngineEnd3);
  ENGINE(EngineSize4,EngineStart4,EngineEnd4);
  
  FastLED.show(); // display this frame
  FastLED.delay(1000 / FRAMES_PER_SECOND);
}


// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames.  More cooling = shorter flames.
// Default 55, suggested range 20-100 
#define COOLING  20

// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire.  Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 200


///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
void ENGINE(int EngineSize, int EngineStart, int EngineEnd)
{
// Array of temperature readings at each simulation cell
  static uint8_t heat[8];

  // Step 1.  Cool down every cell a little
    for( int i = EngineStart; i < EngineEnd; i++) {
      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / EngineSize) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= EngineEnd - 1; k >= EngineStart; k--) {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(EngineStart,EngineEnd);
      heat[y] = qadd8( heat[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = EngineStart; j < EngineEnd; j++) {
      // Scale the heat value from 0-255 down to 0-240
      // for best results with color palettes.
      uint8_t colorindex = scale8( heat[j], 240);
      CRGB color = ColorFromPalette( gPal, colorindex);
      int pixelnumber;
      if( gReverseDirection ) {    //if( gReverseDirection ) { if( random(0,1) ) { //
        pixelnumber = (NUM_LEDS-1) - j;
      } else {
        pixelnumber = j;
      }
      leds[pixelnumber] = color;
    }
}
///////////////////////////////////////////////////////////////////////////////////////////

so to clarify a little more. when i make the total length of the segments less thank 24 i can get 4 instances of the ENGINE() function to work...

still cant figure out why longer than 24 is an issue...



///////Engine 1///////
#define EngineSize1 6
#define EngineStart1 0
#define EngineEnd1 6

///////Engine 2///////
#define EngineSize2 6
#define EngineStart2 6
#define EngineEnd2 12

///////Engine 3///////
#define EngineSize3 6
#define EngineStart3 12
#define EngineEnd3 18

///////Engine 4///////
#define EngineSize4 6
#define EngineStart4 18
#define EngineEnd4 24

any advice would be helpful :slight_smile:

can you post the code where there would be an issue?

as a side note

      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;

➜ you need to make sure that k-1 and k-2 stay with the array. is that always the case for Engine1?

when you do this

      int y = random8(EngineStart,EngineEnd);
      heat[y] = qadd8( heat[y], random8(160,255) );

y will be pretty high for Engine4 for example - start is 18, end is 24 but the heat array only has 8 entries... you overflow...

hi Jackson,

here is a version of the code that fails

#include <FastLED.h>

#define LED_PIN     2
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS    36       //LEDS on strip 


///////Engine 1///////
#define EngineSize1 8
#define EngineStart1 0
#define EngineEnd1 8

///////Engine 2///////
#define EngineSize2 8
#define EngineStart2 8
#define EngineEnd2 16

///////Engine 3///////
#define EngineSize3 8
#define EngineStart3 16
#define EngineEnd3 24

///////Engine 4///////
#define EngineSize4 8
#define EngineStart4 24
#define EngineEnd4 32



#define BRIGHTNESS  255
#define FRAMES_PER_SECOND 100

bool gReverseDirection = false;

CRGB leds[NUM_LEDS];


CRGBPalette16 gPal;

void setup() {
  delay(300); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  
  FastLED.setBrightness( BRIGHTNESS );

  gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::OrangeRed );

FastLED.show(); // display this frame
}

void loop()
{
  // Add entropy to random number generator; we use a lot of it. 
  random16_add_entropy( random8());                                        // <<< use random() for ardunino esp_random() for ESP32

  ENGINE(EngineSize1,EngineStart1,EngineEnd1);
  ENGINE(EngineSize2,EngineStart2,EngineEnd2);
  ENGINE(EngineSize3,EngineStart3,EngineEnd3);
  ENGINE(EngineSize4,EngineStart4,EngineEnd4);
  
  FastLED.show(); // display this frame
  FastLED.delay(1000 / FRAMES_PER_SECOND);
}


// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames.  More cooling = shorter flames.
// Default 55, suggested range 20-100 
#define COOLING  20

// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire.  Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 200


///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
void ENGINE(int EngineSize, int EngineStart, int EngineEnd)
{
// Array of temperature readings at each simulation cell
  static uint8_t heat[8];

  // Step 1.  Cool down every cell a little
    for( int i = EngineStart; i < EngineEnd; i++) {
      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / EngineSize) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= EngineEnd - 1; k >= EngineStart; k--) {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(EngineStart,EngineEnd);
      heat[y] = qadd8( heat[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = EngineStart; j < EngineEnd; j++) {
      // Scale the heat value from 0-255 down to 0-240
      // for best results with color palettes.
      uint8_t colorindex = scale8( heat[j], 240);
      CRGB color = ColorFromPalette( gPal, colorindex);
      int pixelnumber;
      if( gReverseDirection ) {    //if( gReverseDirection ) { if( random(0,1) ) { //
        pixelnumber = (NUM_LEDS-1) - j;
      } else {
        pixelnumber = j;
      }
      leds[pixelnumber] = color;
    }
}
///////////////////////////////////////////////////////////////////////////////////////////

p.s. I am just chewing over your comment regarding the heat array...

thanks for your help :slight_smile:

I think you forgot to make a Wokwi project of it :wink: Here is your sketch: TheEngineThatCould.ino - Wokwi Arduino and ESP32 Simulator

lol thanks....

the code seems to work in the simulator but not in reality.

yeah that's an issue

when you call ENGINE(EngineSize4, EngineStart4, EngineEnd4); that's

ENGINE(8, 24, 32);

in the function EngineStart is 24 and EngineEnd is 32

so

  // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  if ( random8() < SPARKING ) {
    int y = random8(EngineStart, EngineEnd);
    heat[y] = qadd8( heat[y], random8(160, 255) );
  }

becomes

  // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  if ( random8() < SPARKING ) {
    int y = random8(24, 32); 
    heat[y] = qadd8( heat[y], random8(160, 255) ); 
  }

so y is worth something between 24 and 31 and when you assign heat[y] = you overflow

you should do heat[y-EngineStart] to make sure you stay within the bounds or use

    int y = random8(0, EngineSize);

to be within normal index range

1 Like

The ESP8266/ESP32 gives a data-signal of 3.3V and the ledstrip needs a data-signal of 5V. It is a known problem that it will not work (or barely work). You have to amplify the data signal.

thanks ill give it a try now...

thanks but this is not related to my issue i have logic level converter in place and other fastled code works fine.

1 Like

ok thanks Jackson.

I understand I am going out of bounds of the heat array.

this didn't work with your example so I need to experiment more.

int y = random8(0, EngineSize);
#include <FastLED.h>

#define LED_PIN     2
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS    36       //LEDS on strip 


///////Engine 1///////
#define EngineSize1 8
#define EngineStart1 0
#define EngineEnd1 8

///////Engine 2///////
#define EngineSize2 8
#define EngineStart2 8
#define EngineEnd2 16

///////Engine 3///////
#define EngineSize3 8
#define EngineStart3 16
#define EngineEnd3 24

///////Engine 4///////
#define EngineSize4 8
#define EngineStart4 24
#define EngineEnd4 32



#define BRIGHTNESS  255
#define FRAMES_PER_SECOND 100

bool gReverseDirection = false;

CRGB leds[NUM_LEDS];


CRGBPalette16 gPal;

void setup() {
  delay(300); // sanity delay
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  
  FastLED.setBrightness( BRIGHTNESS );

  gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::OrangeRed );

FastLED.show(); // display this frame
}

void loop()
{
  // Add entropy to random number generator; we use a lot of it. 
  random16_add_entropy( random8());                                        // <<< use random() for ardunino esp_random() for ESP32

  ENGINE(EngineSize1,EngineStart1,EngineEnd1);
  ENGINE(EngineSize2,EngineStart2,EngineEnd2);
  ENGINE(EngineSize3,EngineStart3,EngineEnd3);
  ENGINE(EngineSize4,EngineStart4,EngineEnd4);
  
  FastLED.show(); // display this frame
  FastLED.delay(1000 / FRAMES_PER_SECOND);
}


// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames.  More cooling = shorter flames.
// Default 55, suggested range 20-100 
#define COOLING  20

// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire.  Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 200


///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
void ENGINE(int EngineSize, int EngineStart, int EngineEnd)
{
// Array of temperature readings at each simulation cell
  static uint8_t heat[8];

  // Step 1.  Cool down every cell a little
    for( int i = EngineStart; i < EngineEnd; i++) {
      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / EngineSize) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= EngineEnd - 1; k >= EngineStart; k--) {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
       int y = random8(0, EngineSize);
       //int y = random8(EngineStart,EngineEnd);
      heat[y] = qadd8( heat[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = EngineStart; j < EngineEnd; j++) {
      // Scale the heat value from 0-255 down to 0-240
      // for best results with color palettes.
      uint8_t colorindex = scale8( heat[j], 240);
      CRGB color = ColorFromPalette( gPal, colorindex);
      int pixelnumber;
      if( gReverseDirection ) {    //if( gReverseDirection ) { if( random(0,1) ) { //
        pixelnumber = (NUM_LEDS-1) - j;
      } else {
        pixelnumber = j;
      }
      leds[pixelnumber] = color;
    }
}
///////////////////////////////////////////////////////////////////////////////////////////

what did not work? still crashing? or not doing what you wanted?

yeah still crashing... not one led lights up

you still have

  for ( int k = EngineEnd - 1; k >= EngineStart; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  }

when you call for the first Engine between the bounds 0 and 8, k in your loop goes from 7 to 0 inclusive. when k is 1 you are accessing heat[-1]and when k is 0 you are accessing heat[-2]... that's an issue

also when calling with higher numbers, you have the same issue with heat[k] than before, k is out of range. that's an even bigger issue as you write in an unknown memory location

➜ you need to review what you try to do with the heat array and make sure you always access indexes between 0 and 7 since you allocated a static 8 entries array

1 Like

thanks I will have to figure it out tomorrow.

thanks for the help!

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