NEOPIXEL brightness control
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- dazer
- Posts: 9
- Joined: Fri Sep 21, 2012 10:02 am
NEOPIXEL brightness control
Hi,
i was wondering if it is possible to tame down the brightness of the LED's in the neo pixel library.
Thanks.
i was wondering if it is possible to tame down the brightness of the LED's in the neo pixel library.
Thanks.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: NEOPIXEL brightness control
You adjust the brightness by adjusting the value you give to each color.
A value of 255 is full brightness, a value of 127 is half brightness, 63 is quarter brightness, etc.
setPixelColor takes the three basic colors of red, green, and blue as arguments. So, half-bright blue would be
Here are a few other examples:
A value of 255 is full brightness, a value of 127 is half brightness, 63 is quarter brightness, etc.
setPixelColor takes the three basic colors of red, green, and blue as arguments. So, half-bright blue would be
Code: Select all
strip.setPixelColor(i, (0, 0, 127));
Code: Select all
strip.setPixelColor(i, (0, 63, 0)); //1/4 bright green
strip.setPixelColor(i, (255, 0, 0)); //full-bright red
strip.setPixelColor(i, (0, 255, 255)); //full-bright cyan
strip.setPixelColor(i, (127, 127, 0)); //half-bright yellow
strip.setPixelColor(i, (255, 192, 255)); //orange
strip.setPixelColor(i, (63, 63, 63)); //1/4-bright white
- mild lee interested
- Posts: 7
- Joined: Fri Oct 28, 2011 3:06 am
Re: NEOPIXEL brightness control
I know that this is resurrecting an old post, but there is an easier way which gives you better colour control.
The command "strip.setBrightness()" will re-map the maximum for for the whole strip. For example:
will make the pixel glow a bright pink for one second, then glow dimly the same shade of pink for one second.
You therefore have 8 bit control of red, green and blue and can then set the brightness of the whole strip seperately.
So, you can set the RGB combination for each pixel with strip.setPixelColor() to get the exact shade that you want and then set the brightness of the whole strip with strip.setBrightness(). This lets you choose your overall pixel colour and then apply effects like "breathing" (slowly fading the brightness up and down) without having to recalculate values for red, green and blue for each step of the fade.
What I would really like is a strip.setBrightness() command that can be applied to an individual pixel, rather than the whole strip...
The command "strip.setBrightness()" will re-map the maximum for for the whole strip. For example:
Code: Select all
void loop()
{
strip.setBrightness(255);
strip.setPixelColor(n, 255,0,100);
strip.show();
delay(1000);
strip.setBrightness(50);
strip.show();
delay(1000);
}
You therefore have 8 bit control of red, green and blue and can then set the brightness of the whole strip seperately.
So, you can set the RGB combination for each pixel with strip.setPixelColor() to get the exact shade that you want and then set the brightness of the whole strip with strip.setBrightness(). This lets you choose your overall pixel colour and then apply effects like "breathing" (slowly fading the brightness up and down) without having to recalculate values for red, green and blue for each step of the fade.
What I would really like is a strip.setBrightness() command that can be applied to an individual pixel, rather than the whole strip...
- adafruit_support_bill
- Posts: 86295
- Joined: Sat Feb 07, 2009 10:11 am
Re: NEOPIXEL brightness control
Not quite that easy. The setBrightness() function as currently implemented operates directly on the pixel array and is 'lossy'. Since the original brightness value is not retained, once dimmed, you can never return to the exact original brightness value. The more you dim, the more information you lose.This lets you choose your overall pixel colour and then apply effects like "breathing" (slowly fading the brightness up and down) without having to recalculate values for red, green and blue for each step of the fade.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: NEOPIXEL brightness control
Bill is correct. The way to do what you want is as follows:
Call it this way
Code: Select all
uint16_t brightness;
void loop()
{
brightness = 255;
strip.setPixelColor(n, (brightness*255/255) , (brightness*0/255), (brightness*100/255));
strip.show();
delay(1000);
brightness = 50;
strip.setPixelColor(n, (brightness*255/255) , (brightness*0/255), (brightness*100/255));
strip.show();
delay(1000);
}
You can, of course, make your own function, and add it to the library. Something like this, perhaps:Mild Lee Interested wrote:What I would really like is a strip.setBrightness() command that can be applied to an individual pixel, rather than the whole strip...
Code: Select all
Adafruit_NeoPixel::setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness) {
setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
Code: Select all
void loop()
{
strip.setPixelColor(n, 255,0,100, 255);
strip.show();
delay(1000);
strip.setPixelColor(n, 255,0,100, 50);
strip.show();
delay(1000);
}
- mild lee interested
- Posts: 7
- Joined: Fri Oct 28, 2011 3:06 am
Re: NEOPIXEL brightness control
Thanks very much to Bill for correcting me. Since my ill informed post, I've experimented with multiple calls of "strip.setBrightness()" and I see what you mean if you go try to go back up from a low level.
But why is the following code (that I havn't actually tried, as I don't have any hardware here at the moment) a bad idea?
I'm not much of a programmer, so feel free to tell me just how badly this sucks. This is what I meant by my comment about fading. Sorry if I lead anybody astray. 
But why is the following code (that I havn't actually tried, as I don't have any hardware here at the moment) a bad idea?
Code: Select all
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
int fadeControl = 255;//will hold the current brightness level
int fadeDirection = -1;//change sigen to fade up or down
int fadeStep = 10;//delay between updates
void setup()
{
strip.begin();
strip.show();
}
void loop()
{
strip.setPixelColor(0, 255, 192, 255);//set the pixel colour
strip.setBrightness(fadeControl);//set the pixel brightness
strip.show();
fadeControl = fadeControl + fadeDirection;//increment the brightness value
if (fadeControl <0 || fadeControl > 255)
//If the brightness value has gone past its limits...
{
fadeDirection = fadeDirection * -1;//change the direction...
fadeControl = fadeControl + fadeDirection;//...and start back.
}
delay(fadeStep);//wait a bit before doing it again.
}

- adafruit_support_bill
- Posts: 86295
- Joined: Sat Feb 07, 2009 10:11 am
Re: NEOPIXEL brightness control
That should work because you are re-setting the pixel color on each pass through the loop. Doing multiple calls to setBrightness without resetting the pixel color is when you start getting into trouble.
- chargerdude70
- Posts: 47
- Joined: Thu Jan 02, 2014 11:49 am
Re: NEOPIXEL brightness control
Adafruit_NeoPixel::setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness) {
setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
Sorry...noob here. How or where in the library would this code that was posted earlier be added? My guess is it would be inserted in the .h file. Is this correct? Would it be inserted verbatim? Is there a specific area it needs to be?
setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
Sorry...noob here. How or where in the library would this code that was posted earlier be added? My guess is it would be inserted in the .h file. Is this correct? Would it be inserted verbatim? Is there a specific area it needs to be?
- adafruit_support_mike
- Posts: 66617
- Joined: Thu Feb 11, 2010 2:51 pm
Re: NEOPIXEL brightness control
You'd need to modify the library files in multiple places. If you aren't sure about what goes where, it's easier to create an equivalent function in your own code:
That assumes you've created a global object named 'strip', but that's what we do in most of the example sketches, so the function above should work for anything based off the examples.
To use the function, you'd do:
where each of those variables contains the appropriate value.
Code: Select all
void setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness) {
strip.setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
To use the function, you'd do:
Code: Select all
setPixelColor( pixelNumber, rValue, gValue, bValue, brightness );
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: NEOPIXEL brightness control
You would put it in the .cpp file, right underneath the existing setPixelColor function.chargerdude70 wrote:How or where in the library would this code that was posted earlier be added?
You would also have to add the declaration to the the .h file, again, right underneath the existing setPixelColor declaration:
Code: Select all
setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness)
- chargerdude70
- Posts: 47
- Joined: Thu Jan 02, 2014 11:49 am
Re: NEOPIXEL brightness control
I placed void setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness) {
strip.setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
} after Set pixel color from 'packed' 32-bit RGB color: in .cpp. Is this correct?
Then I placed setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness), after setPixelColor(uint16_t n, uint32_t c), in the .h file. Is this correct?
strip.setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
} after Set pixel color from 'packed' 32-bit RGB color: in .cpp. Is this correct?
Code: Select all
// Set pixel color from 'packed' 32-bit RGB color:
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
if(n < numLEDs) {
uint8_t
r = (uint8_t)(c >> 16),
g = (uint8_t)(c >> 8),
b = (uint8_t)c;
if(brightness) { // See notes in setBrightness()
r = (r * brightness) >> 8;
g = (g * brightness) >> 8;
b = (b * brightness) >> 8;
}
uint8_t *p = &pixels[n * 3];
#ifdef NEO_RGB
if((type & NEO_COLMASK) == NEO_GRB) {
#endif
*p++ = g;
*p++ = r;
#ifdef NEO_RGB
} else {
*p++ = r;
*p++ = g;
}
#endif
*p = b;
}
}
//Set pixel brightness individually
void setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness) {
strip.setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
Code: Select all
void
begin(void),
show(void),
setPin(uint8_t p),
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b),
setPixelColor(uint16_t n, uint32_t c),
setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness),
setBrightness(uint8_t);
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: NEOPIXEL brightness control
Looks right to me
- chargerdude70
- Posts: 47
- Joined: Thu Jan 02, 2014 11:49 am
Re: NEOPIXEL brightness control
To expand on the variable brightness. If I wanted to set a pixel color with a variable brightness using "pixels.setPixelColor(15, color ,255) color being (uint_32t c)..shouldn't it be as easy as adding
to .h file and
in .cpp file
when I add the code to the files, I get indivindual errors after compiling a sketch stating..........NeoPixel.cpp920: r, g, b was not declared in this scope
Code: Select all
setPixelColor(uint16_t n, uint32_t c, uint16_t brightness),
to .h file and
Code: Select all
// Set each pixel brightness individually using uint32_t
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c, uint16_t brightness) {
setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
when I add the code to the files, I get indivindual errors after compiling a sketch stating..........NeoPixel.cpp920: r, g, b was not declared in this scope
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: NEOPIXEL brightness control
Right. You'll have to extract the individual r, g, and b values from the uint32_t variable c. Have a look at the setPixelColor(n, c) function to see how that's done.chargerdude70 wrote:when I add the code to the files, I get indivindual errors after compiling a sketch stating..........NeoPixel.cpp920: r, g, b was not declared in this scope
-
- Posts: 3
- Joined: Mon Dec 02, 2013 4:16 pm
Re: NEOPIXEL brightness control
I am fairly new to programming, arduino, and neopixels. I wanted a similar effect. I have a good friend, who is a programmer help me out. This is the code he wrote, and here is a video of what it did. http://www.youtube.com/watch?v=BYrzIdlkVjc
void loop() {
//Written by: Jason Yandell
int TOTAL_LEDS = 60;
float MaximumBrightness = 255;
float SpeedFactor = 0.008; // I don't actually know what would look good
float StepDelay = 5; // ms for a step delay on the lights
// Make the lights breathe
for (int i = 0; i < 65535; i++) {
// Intensity will go from 10 - MaximumBrightness in a "breathing" manner
float intensity = MaximumBrightness /2.0 * (1.0 + sin(SpeedFactor * i));
strip.setBrightness(intensity);
// Now set every LED to that color
for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {
strip.setPixelColor(ledNumber, 0, 0, 255);
}
strip.show();
//Wait a bit before continuing to breathe
delay(StepDelay);
}
}
void loop() {
//Written by: Jason Yandell
int TOTAL_LEDS = 60;
float MaximumBrightness = 255;
float SpeedFactor = 0.008; // I don't actually know what would look good
float StepDelay = 5; // ms for a step delay on the lights
// Make the lights breathe
for (int i = 0; i < 65535; i++) {
// Intensity will go from 10 - MaximumBrightness in a "breathing" manner
float intensity = MaximumBrightness /2.0 * (1.0 + sin(SpeedFactor * i));
strip.setBrightness(intensity);
// Now set every LED to that color
for (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {
strip.setPixelColor(ledNumber, 0, 0, 255);
}
strip.show();
//Wait a bit before continuing to breathe
delay(StepDelay);
}
}
Please be positive and constructive with your questions and comments.