FastLED and adding a new animation function to an existing sketch

Hoping someone skilled can help point me in the right direction. I'm trying to merge the effects found here: FX with this Arduino sketch: OutsideLEDs_PublicOTA2.ino. I have the lights all set up and hung after following this tutorial: Customizable Animated LED Christmas (& Every Holiday) Lights.

For example, the snow sparkle effect:
Here is the raw code:

void loop() {
  SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));
}

void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {

  setAll(red,green,blue);

  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,0xff,0xff,0xff);
  showStrip();
  delay(SparkleDelay);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
}

Here is what I would think I'd need to modify to make it play with the existing sketch:

void loop() {
    SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));
  }

if (setEffect == "snowsparkle") {   


  void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {

    setAll(red,green,blue);

    int Pixel = random(NUM_LEDS);
    setPixel(Pixel,0xff,0xff,0xff);
    showStrip();
    delay(SparkleDelay);
    setPixel(Pixel,red,green,blue);
    showStrip();
    delay(SpeedDelay);
  }
}

But, when added to the OutsideLEDs_PublicOTA2.ino code, it's not compiling. Also, every other LED animation function in OutsideLEDs_PublicOTA2.ino seems to reference a palette, whereas this does not... That may or may not be related to the issue.

In case it matters, my arduino is the Makerfocus D1 Mini NodeMcu (ESP8266).

Please point me in the right direction.

void loop() {
    SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));
  } // <== This is the end of loop. No code goes outside of any function.

if (setEffect == "snowsparkle") {

So, the existing code has it's own void loop, so would that mean I'd just exclude the void loop for this function and place it within the existing code's void loop? An example would be outstanding. :slight_smile: If I can make one of these functions play with the existing code, I can make them all work I presume.

For example, the OutsideLEDs_PublicOTA2.ino code has a place specifically for adding custom functions:

  if (setEffect == "Holly Jolly") {
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* higher = faster motion */

    fill_palette( leds, NUM_LEDS,
                  startIndex, 16, /* higher = narrower stripes */
                  HJPalettestriped, 255, LINEARBLEND);
  }

/////////////////End DrZzs effects/////////////
///////////////////////////////////////////////

////////Place your custom effects below////////////




/////////////end custom effects////////////////

///////////////////////////////////////////////
/////////fastLED & Bruh effects///////////////
/////////////////////////////////////////////
  
  if (setEffect == "Sinelon") {
    fadeToBlackBy( leds, NUM_LEDS, 20);
    int pos = beatsin16(13, 0, NUM_LEDS);
    leds[pos] += CRGB(Rcolor, Gcolor, Bcolor);
  }

So, maybe I'd add it like this?:

  if (setEffect == "Holly Jolly") {
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* higher = faster motion */

    fill_palette( leds, NUM_LEDS,
                  startIndex, 16, /* higher = narrower stripes */
                  HJPalettestriped, 255, LINEARBLEND);
  }

/////////////////End DrZzs effects/////////////
///////////////////////////////////////////////

////////Place your custom effects below////////////

if (setEffect == "snowsparkle") {   
SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));

void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {

  setAll(red,green,blue);

  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,0xff,0xff,0xff);
  showStrip();
  delay(SparkleDelay);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
}
}

/////////////end custom effects////////////////

///////////////////////////////////////////////
/////////fastLED & Bruh effects///////////////
/////////////////////////////////////////////
  
  if (setEffect == "Sinelon") {
    fadeToBlackBy( leds, NUM_LEDS, 20);
    int pos = beatsin16(13, 0, NUM_LEDS);
    leds[pos] += CRGB(Rcolor, Gcolor, Bcolor);
  }

But...it seems like this line...

SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));

...is how you call it, so not sure where to put this...

For example, the OutsideLEDs_PublicOTA2.ino code has a place specifically for adding custom functions:

In that area, you'd add something like:

   if(setEffect == "You picked a fine time to leave me, Lucille")
   {
       // Put some code here...
   }

Post ALL of the code you are trying to add the SnowSparkle() function to.

Tried to post complete code, but failed due to this error:

The message has the following error or errors that must be corrected before continuing: The message exceeds the maximum allowed length (9000 characters).

However, the complete code is here: OutsideLEDs_PublicOTA2.ino

In that code, there is a callback registered. Something outside of the Arduino is doing something that triggers that callback function. You need to make sure that whatever that is will tell the Arduino that it should do the SnowSparkle effect.

Then, you modify the callback() function to map whatever the callback receives to engage the SnowSparkle effect to having setEffect contain "SnowSparkle".

Then, you modify loop() to deal with the case where setEffect equals "SnowSparkle". When it does, call the SnowSparkle() function that you add at the end of the code.

You are right. The code works in tandem with Home Assistant (running on a Raspberry Pi). Inside home assistant there is a toggle to turn on the lights and select different FX from a dropdown. That drop down holds the names. When toggled, the corresponding arduino code matching the effect string fires.

So, regarding my question in response #2, do I have it right in terms of where to add the function and the function itself?

You have it right in terms of where to call the function, which is just this much:

if (setEffect == "snowsparkle") {  
   SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));
}

As for this part:

void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {

  setAll(red,green,blue);

  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,0xff,0xff,0xff);
  showStrip();
  delay(SparkleDelay);
  setPixel(Pixel,red,green,blue);
  showStrip();
  delay(SpeedDelay);
}

that is a function that cannot be inside another function, so cut and paste it to the end of the file. I think you'll have to copy and paste in other functions, such as showStrip() and setAll() from the from the other file.

Ah! I get what you're saying. Yes, adding those functions at the end would clearly be necessary and something I overlooked... I think this is going to work... I'll report back when I've had a chance to test.