CODE QUESTION: Increasing brightness on Neopixels

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

CODE QUESTION: Increasing brightness on Neopixels

Post by joshuakane »

Hello:

what would be a good way to have the brightness of the NEOPixels increase? What I am looking to do, is to push a momentary button, and then as long as it is pushed the brightness increases to the MAX value then discharges and turns off until the button is released again.

Thanks,

Joshua

User avatar
SeiRruf
 
Posts: 82
Joined: Thu Sep 04, 2014 3:07 am

Re: CODE QUESTION: Increasing brightness on Neopixels

Post by SeiRruf »

The adafruit NeoPixels library comes packaged with setBrightness();

Here is an example of something you could do in a loop.

Code: Select all

uint8_t brightness = 0; //global variable

  boolean button = digitalRead ( 7 ); //pin 7 (you change this)
  
  //if button is pressed and we're under the max of 255 brightness,
  if ( button && brightness < 256 ) brightness++; //+1, max of 255
  
  //or if the button is released, and we are under the minimum 0 brightness,
  else if ( !button && brightness > 0 ) brightness--; //-1, min of 0
  
  //do your animation stuff here, or re-set your pixels to full-values, in this example, full blue.
  for ( uint8_t i=0; i=strip.numPixels(); i++ ) strip.setPixelColor ( i, 0, 0, 255 );
  
  //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;
  strip.setBrightness ( brightness );
  
  //then push the data to the pixels.
  strip.show ();
  
 //if you don't add some delay, the brightness will crawl up super fast
  delay ( 25 );
Last edited by SeiRruf on Fri Sep 19, 2014 5:27 pm, edited 2 times in total.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: CODE QUESTION: Increasing brightness on Neopixels

Post by joshuakane »

Thanks SeiRruf!

I will give it a shot this weekend.

-- Joshua

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

Re: CODE QUESTION: Increasing brightness on Neopixels

Post by adafruit_support_bill »

That is not likely to work very well. SetBrightness operates directly on the pixel data in memory and therefore is 'lossy'. Once they are dimmed, the colors may not be the same when you brighten them again. Once the brightness goes to zero, all data is lost and you can't recover anything at all.

Better to re-write the pixels at full value, then call setBrghtness() before calling show().

User avatar
SeiRruf
 
Posts: 82
Joined: Thu Sep 04, 2014 3:07 am

Re: CODE QUESTION: Increasing brightness on Neopixels

Post by SeiRruf »

adafruit_support_bill wrote:Better to re-write the pixels at full value, then call setBrghtness() before calling show().
I apologize, Thanks Bill!
Bill is absolutely correct, I completely forgot about this in the example I gave.

Briefly explained.
In simple, you can't "dim" NeoPixels. What you can do however, is lower the color values. setBrightness(uint8_t) multiplies each color value by your set dim value as a percentage.

The dim value percentage is calculated as: (brightness divided by 255 ) where 0/255 = 0% and 255/255 = 100% brightness and of course, 127/255= Roughly 50% brightness (or 0.498).

Multiplying this value by the current color value of your pixel gives you that brightness. Say full red at 255 with 50% brightness.
R = 255 * (127/255); == roughly 127, which is 50%.

This explains why if you set brightness to 0, it completely destroys your color values. 255 * 0 = 0; so going from 0 to anything above would result in your pixels continuing to be off.
To avoid this (as bill suggested), simply re-set the original color values of your pixels *before* assigning the brightness each time.

I updated my source code in my original first post with additional comments/examples on how to use it in your sketch to fix this issue.

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

Return to “Arduino”