Help with Breathing/Fade coding

Hello,

I'm a beginner at coding for the Arduino and I would like some help with my current project. I'm trying to make my LED strip fade in and out like its breathing. Through my research, I have found some code that works for me, but I'm trying to make the fade in and out times longer. I've successfully been able to delay the time in between the fade in and fade out. Also, I have been able to make the fade-out time longer. However, the fade-in time is still too short for my liking and that's where I need some help. I'm trying to make the fade-in time longer and not as abrupt. I'm using an Arduino Mini Pro with a WS2812B LED strip. Here is the code that I have been working with.

#include <FastLED.h>
#define NUM_LEDS 100
CRGB leds[NUM_LEDS];
#define PIN 3

void setup()
{
  FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}

void loop() {
  FadeInOut(0xff, 0x00, 0x00);
}

void FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
     
  for(int k = 0; k < 100; k=k+1) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    showStrip();
  }
     
  for(int k = 100; k >= 0; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll(r,g,b);
    delay(100);    /* effects the fade out time */
    showStrip();
  }
delay(1000);      /* effects the time inbetween fades */ 
}

void showStrip() {

   FastLED.show();

}

void setPixel(int Pixel, byte red, byte green, byte blue) {

 #ifndef ADAFRUIT_NEOPIXEL_H
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

Please let me know if there is a better way or different functions that allow for the fade in and out. Thank you

What is the difference between your for() loop that fades IN vs. your for() loop that fades OUT? I see one statement - 'delay(100)' Did you try putting that into your fade in loop?

Also, your fade in loop increments by 1 but your fade out loop decrements by 2

You also call showStrip() inside your setAll() function but also call it in your for() loops which means you are basically calling it twice with the same data. You don't need to do that.

+1 Karma for your first post and using code tags!

This is not ideal, because it blocks - but it works:

const uint32_t DURATION = 1000;
const byte RED = 50;
const byte GREEN = 125;
const byte BLUE = 255;

uint32_t start = millis(), elapsed;

do
{

  elapsed = millis() - start;
  float percent = (float)elapsed / (float)DURATION;
  setColor(
    round((float)RED * percent),
    round((float)GREEN * percent),
    round((float)BLUE * percent)
  );

} while (elapsed < DURATION);

You go on and figure that one out! :wink:

blh64:
What is the difference between your for() loop that fades IN vs. your for() loop that fades OUT? I see one statement - 'delay(100)' Did you try putting that into your fade in loop?

Also, your fade in loop increments by 1 but your fade out loop decrements by 2

You also call showStrip() inside your setAll() function but also call it in your for() loops which means you are basically calling it twice with the same data. You don't need to do that.

+1 Karma for your first post and using code tags!

Thank you for your help. I didn't remember changing the fade out loop decrements, but that was one of the keys to my problem. I had originally added the delay to the fade in loop, but because the increment was smaller it stayed at full brightness for too long. Thanks again