NEOPIXEL brightness control

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dazer
 
Posts: 9
Joined: Fri Sep 21, 2012 10:02 am

NEOPIXEL brightness control

Post by dazer »

Hi,
i was wondering if it is possible to tame down the brightness of the LED's in the neo pixel library.
Thanks.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: NEOPIXEL brightness control

Post by adafruit_support_rick »

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

Code: Select all

strip.setPixelColor(i, (0, 0, 127));
Here are a few other examples:

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

User avatar
mild lee interested
 
Posts: 7
Joined: Fri Oct 28, 2011 3:06 am

Re: NEOPIXEL brightness control

Post by mild lee interested »

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:

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);
}
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...

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: NEOPIXEL brightness control

Post by adafruit_support_bill »

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.
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.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: NEOPIXEL brightness control

Post by adafruit_support_rick »

Bill is correct. The way to do what you want is as follows:

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);
}
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...
You can, of course, make your own function, and add it to the library. Something like this, perhaps:

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));
}
Call it this way

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);
}

User avatar
mild lee interested
 
Posts: 7
Joined: Fri Oct 28, 2011 3:06 am

Re: NEOPIXEL brightness control

Post by mild lee interested »

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?

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.
}
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. :oops:

User avatar
adafruit_support_bill
 
Posts: 88086
Joined: Sat Feb 07, 2009 10:11 am

Re: NEOPIXEL brightness control

Post by adafruit_support_bill »

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.

User avatar
chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: NEOPIXEL brightness control

Post by chargerdude70 »

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?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: NEOPIXEL brightness control

Post by adafruit_support_mike »

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:

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));
}
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:

Code: Select all

    setPixelColor( pixelNumber, rValue, gValue, bValue, brightness );
where each of those variables contains the appropriate value.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: NEOPIXEL brightness control

Post by adafruit_support_rick »

chargerdude70 wrote:How or where in the library would this code that was posted earlier be added?
You would put it in the .cpp file, right underneath the existing setPixelColor function.

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)

User avatar
chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: NEOPIXEL brightness control

Post by chargerdude70 »

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?

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));
}
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?

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); 

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: NEOPIXEL brightness control

Post by adafruit_support_rick »

Looks right to me

User avatar
chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: NEOPIXEL brightness control

Post by chargerdude70 »

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

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));
}
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

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: NEOPIXEL brightness control

Post by adafruit_support_rick »

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
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.

User avatar
justinw311
 
Posts: 8
Joined: Mon Dec 02, 2013 4:16 pm

Re: NEOPIXEL brightness control

Post by justinw311 »

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);

}
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”