0

NEOPIXEL brightness control
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

NEOPIXEL brightness control

by dazer on Tue Jun 25, 2013 4:58 am

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

dazer
 
Posts: 9
Joined: Fri Sep 21, 2012 10:02 am

Re: NEOPIXEL brightness control

by adafruit_support_rick on Tue Jun 25, 2013 10:31 am

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 | TOGGLE FULL SIZE
strip.setPixelColor(i, (0, 0, 127));


Here are a few other examples:
Code: Select all | TOGGLE FULL SIZE
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

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: NEOPIXEL brightness control

by Mild Lee Interested on Mon Jan 27, 2014 5:51 am

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 | TOGGLE FULL SIZE
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...
Mild Lee Interested
 
Posts: 7
Joined: Fri Oct 28, 2011 3:06 am

Re: NEOPIXEL brightness control

by adafruit_support_bill on Mon Jan 27, 2014 7:37 am

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.

adafruit_support_bill
 
Posts: 70493
Joined: Sat Feb 07, 2009 10:11 am

Re: NEOPIXEL brightness control

by adafruit_support_rick on Mon Jan 27, 2014 9:32 am

Bill is correct. The way to do what you want is as follows:
Code: Select all | TOGGLE FULL SIZE
 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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
void loop()
{
   strip.setPixelColor(n, 255,0,100, 255);
   strip.show();
   delay(1000);
   strip.setPixelColor(n, 255,0,100, 50);
   strip.show();
   delay(1000);
}

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: NEOPIXEL brightness control

by Mild Lee Interested on Thu Jan 30, 2014 11:27 am

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 | TOGGLE FULL SIZE
#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:
Mild Lee Interested
 
Posts: 7
Joined: Fri Oct 28, 2011 3:06 am

Re: NEOPIXEL brightness control

by adafruit_support_bill on Thu Jan 30, 2014 11:47 am

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.

adafruit_support_bill
 
Posts: 70493
Joined: Sat Feb 07, 2009 10:11 am

Re: NEOPIXEL brightness control

by chargerdude70 on Fri Jan 31, 2014 8:23 pm

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?

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

Re: NEOPIXEL brightness control

by adafruit_support_mike on Sat Feb 01, 2014 2:30 am

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 | TOGGLE FULL SIZE
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 | TOGGLE FULL SIZE
    setPixelColor( pixelNumber, rValue, gValue, bValue, brightness );

where each of those variables contains the appropriate value.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

adafruit_support_mike
 
Posts: 54144
Joined: Thu Feb 11, 2010 2:51 pm

Re: NEOPIXEL brightness control

by adafruit_support_rick on Sat Feb 01, 2014 12:13 pm

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 | TOGGLE FULL SIZE
setPixelColor( uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness)

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: NEOPIXEL brightness control

by chargerdude70 on Mon Feb 03, 2014 9:24 am

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 | TOGGLE FULL SIZE
// 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 | TOGGLE FULL SIZE
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);

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

Re: NEOPIXEL brightness control

by adafruit_support_rick on Mon Feb 03, 2014 10:39 am

Looks right to me

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: NEOPIXEL brightness control

by chargerdude70 on Mon Feb 03, 2014 1:49 pm

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 | TOGGLE FULL SIZE
setPixelColor(uint16_t n, uint32_t c, uint16_t brightness),


to .h file and

Code: Select all | TOGGLE FULL SIZE
// 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

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

Re: NEOPIXEL brightness control

by adafruit_support_rick on Mon Feb 03, 2014 2:48 pm

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.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: NEOPIXEL brightness control

by justinw3115 on Fri Feb 07, 2014 3:19 pm

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

}
}
justinw3115
 
Posts: 3
Joined: Mon Dec 02, 2013 4:16 pm

Please be positive and constructive with your questions and comments.