I have a problem. I am controlling my WS2812B using an Arduino, but I have a problem with the breathing effect. I created a class in the code that looks like this:
String breathDirection = "Down";
void breath_effect()
{
fill_solid(leds, TOTAL_LEDS, primary);
float currentBrightness = FastLED.getBrightness();
if (currentBrightness <= 1)
{
breathDirection = "Up";
}
else if (currentBrightness >= brightness)
{
breathDirection = "Down";
}
float brightnessCorrection = static_cast<float>(brightness) / 200;
if (breathDirection == "Down")
{
currentBrightness = currentBrightness - brightnessCorrection;
FastLED.setBrightness(currentBrightness);
}
else if (breathDirection == "Up")
{
Serial.println("Binnen");
Serial.println(currentBrightness);
currentBrightness = currentBrightness + brightnessCorrection;
Serial.println(currentBrightness);
FastLED.setBrightness(currentBrightness);
}
}
Now the breathing effect only works when the brightness
variable is set to 200. If I set it on a different value below 200, it goes down, but never comes up. The currentBrightness is each round the same value.
What is going wrong here?
You need to set global variables:
//Global scope:
const float _brightInterval = 1.0f;
const float _maxBright = 255.0f; //i'm not sure whats your max bright here
const float _minBright = 1.0f;
float _brightness = _maxBright;
And instead of strings i would use states. It's faster and saves memory.
enum states
{
hold,
down,
up
};
states _state;
then in the loop do something like this:
switch (_state)
{
case states::down:
_brightness -= _brightInterval;
break;
case states::up:
_brightness += _brightInterval;
break;
case states::hold:
_brightness -= _brightInterval / 10.0f; //hold makes it go down but in a lower rate.
default:
break;
}
if (_brightness >= _maxBright)
_state = states::hold;
else if (_brightness <= (_maxBright - 50.0f) && _state == states::hold)
_state = states::down;
else if (_brightness <= _minBright)
_state = states::up;
FastLED.setBrightness(_brightness);
Try to change those values to achieve your desired effect. The (_maxBright - 50.0f) in the second if, is the step when it starts to accelerate down. change it to maybe (_maxBright - 10.0f)