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;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
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
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.
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