I am new to coding so forgive my ignorance. I made a LED strip brightness breathe (WS2812B) function that goes from brightness 50 to 200 using the sin() function with the Neopixel Library.
Now I want it to breathe and have a brightness gradient from brightest in the center to darker toward the edges. I tried a bunch of things that did not work. I have a 100 led strip. I want the middle two LEDS (59,60) to breathe 50 to 200 and the rest breathe a fraction less bright on a gradient. How would I accomplish this? Thanks for the newbie help.
Welcome to the forum
Please post you best effort and describe the problems
Please use code tags when you post the sketch
You could subtract the output of the following function to obtain a brightness gradient.
uint16_t dim(int16_t const x, float const gradient, int16_t const center) {
return abs(gradient * (x - center));
}
For example,
for (size_t i = 0; i < 100; i += 10) {
Serial.print(i);
Serial.print(' ');
Serial.println(200 - dim(i, 0.5, 50));
}
gives as output:
0 175
10 180
20 185
30 190
40 195
50 200
60 195
70 190
80 185
90 180
The center (brightest part) of the gradient is controlled by the center parameter, the difference in brightness by the gradient parameter.
Instead of the value 200 in the example, you can use the "breathe" function you already have.
2 Likes
Thanks jfjlaros. I now have a breathe with a gradient. I would never have figured that out. Heres the beginner sketch.
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRBW + NEO_KHZ800);
void setup() {
strip.begin();
strip.setBrightness(50); // Initial brightness 50
strip.show(); //
}
static uint8_t sine8(uint8_t x) {
return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out
};
uint16_t dim(int16_t const x, float const gradient, int16_t const center) {
return abs(gradient * (x - center));
}
void loop()
{
int TOTAL_LEDS = 60;
// Make the lights breathe
for (byte i = 0; i < 255; i++) {
// Testing sine8
float intensity = sine8(i)*.8 +50;
// Now set every LED to that color
for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {
strip.setPixelColor(ledNumber, (intensity-dim(ledNumber, 1.5, 30)), 0, 0);
}
strip.show();
delay(15);
}
}
system
Closed
April 26, 2023, 8:11pm
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.