WS2812B how to do Rainbow Breathing effect.

Hi I have been working really hard at trying to get a rainbow breathing effect. What I mean by this is kinda like a fade. Where as it cycles through every like half a second or something it fades off and then fades back in. I have been messing around with this example code (Below). I have tried tons of things from for loops to while loops yet I am stumped still. Please respond if you have any ideas! I also wanted to mention the fact that I am not the most experienced coder and the library I am using is Fast LEDs

#include <FastLED.h>

#define LED_PIN     6
#define NUM_LEDS    240
#define BRIGHTNESS  50
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
//
// These compact palettes provide an easy way to re-colorize your
// animation on the fly, quickly, easily, and with low overhead.
//
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code.  Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
//
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
//
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.



CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;


void setup() {
  delay( 500 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );

  currentPalette = RainbowColors_p;
  currentBlending = LINEARBLEND;
}


void loop()
{
  ChangePalettePeriodically();

  static uint8_t startIndex = 0;
  startIndex = startIndex + 1; /* motion speed */

  FillLEDsFromPaletteColors( startIndex);

  FastLED.show();
  FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
  uint8_t brightness = 255;

  for ( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    colorIndex += 3;
  }
}


// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.  All are shown here.

void ChangePalettePeriodically()
{
  uint8_t secondHand = (millis() / 1000) % 60;
  static uint8_t lastSecond = 99;

  if ( lastSecond != secondHand) {
    lastSecond = secondHand;
    if ( secondHand ==  0)  {
      currentPalette = RainbowColors_p;
      currentBlending = LINEARBLEND;
    }

  }
}

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
  for ( int i = 0; i < 16; i++) {
    currentPalette[i] = CHSV( random8(), 255, random8());
  }
}

// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
  // 'black out' all 16 palette entries...
  fill_solid( currentPalette, 16, CRGB::Black);
  // and set every fourth one to white.
  currentPalette[0] = CRGB::White;
  currentPalette[4] = CRGB::White;
  currentPalette[8] = CRGB::White;
  currentPalette[12] = CRGB::White;

}

// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
  CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  CRGB green  = CHSV( HUE_GREEN, 255, 255);
  CRGB black  = CRGB::Black;

  currentPalette = CRGBPalette16(
                     green,  green,  black,  black,
                     purple, purple, black,  black,
                     green,  green,  black,  black,
                     purple, purple, black,  black );
}


// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM.  A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
  CRGB::Red,
  CRGB::Gray, // 'white' is too bright compared to red and blue
  CRGB::Blue,
  CRGB::Black,

  CRGB::Red,
  CRGB::Gray,
  CRGB::Blue,
  CRGB::Black,

  CRGB::Red,
  CRGB::Red,
  CRGB::Gray,
  CRGB::Gray,
  CRGB::Blue,
  CRGB::Blue,
  CRGB::Black,
  CRGB::Black
};



// Additionl notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color.  You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative.  FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.

Ian_Hickler: What I mean by this is kinda like a fade. Where as it cycles through every like half a second or something it fades off and then fades back in.

You're having trouble because you're not quite yet ready to code on this problem. You have to sit down and exactly define what you want to have happen in very explicit terms. The work like or kinda should be nowhere near it. If you can't write it out in plain English, you'll never be able to write code for it no matter how hard you try. Once you do get it written out in plain English, the code will just about write itself.

So start by seeing if you can describe this effect in terms of a period of time and how the colors and intensity should change through that period of time.

Hey man thanks for the advice after thinking about it I would like it to fade the brightness out all the way down to dark and then around 10-20 milliseconds brighten back up to the normal brightness. This would also mean that the rainbow effect would continue you the entire time as it fades. I understand the basics of the concept. Not sure though how to apply this properly so that it would work for me because I assume it would require nested loops or something of that sort though I am not really good at those kinds of things. One more thing is I need the fade to be decently smooth. Thanks - Ian

Ian_Hickler: Not sure though how to apply this properly so that it would work for me because I assume it would require nested loops or something of that sort though I am not really good at those kinds of things

You're not even ready to start. Don't try to think of it in those terms. Get it out in plain English first. Should the lights be on or off at the beginning? What color should they start? How long should it take to fade through all the colors? Do they go both ways red to purple and back to red or just one way? How many total color steps in that pattern? See how that gets you the time per step? How long should the fade in and out take? How many brightness steps between fully off and fully on and back off again? So how much time per step?

So you'll have two tasks, updating the color and updating the brightness. Have a look at the "Blink Without Delay" example to see how we can set a task to happen every so often (in that case toggling a LED). You'll need to do that twice over unless your time per step just happens to work out the same or some even multiple.

For each task you need to figure out how you would calculate the next color or the next brightness level from either the current value or the step number and apply it.

The things you DON'T need are loops, nested loops, for loops, while loops, or anything else with the word loop in it other than the loop() function itself. Loops are for blocking code. Great for simple examples to show how an API works. Not good for much else.

See all that stuff that needs to be figured out before you start trying to figure out how it would be coded? Once you have those answers your code will be dirt simple to write. You'll see. The only previous knowledge you will need to have is the ability to deal with clocks and time in your day to day life. I'm sure you understand how all that works. So this will be a piece of cake.

I am also working on a sort of project similar to this. I am definetly interested in this so please keep us updated?

I will give it a try myself and post some code. There is also a fade example that I think can be applied.

with long hours of research and trial, i stumbled here so i want to share how i make my led breath and do a spectrum color wave.

relatively i want to help others who may end up with this thread.

FastLED_Trial_4_fading_gradient.ino.ino (1.06 KB)