Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.

## NEOPIXEL brightness control Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: NEOPIXEL brightness control

Nice!

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

Re: NEOPIXEL brightness control

I'm wondering if it is possible to condense this down and place it in a void and call it in the loop.
Code: Select all | TOGGLE FULL SIZE
`void loop() {//Written by: Jason Yandellint TOTAL_LEDS = 60;float MaximumBrightness = 255;float SpeedFactor = 0.008; // I don't actually know what would look goodfloat StepDelay = 5; // ms for a step delay on the lights// Make the lights breathefor (int i = 0; i < 65535; i++) {// Intensity will go from 10 - MaximumBrightness in a "breathing" mannerfloat intensity = MaximumBrightness /2.0 * (1.0 + sin(SpeedFactor * i));strip.setBrightness(intensity);// Now set every LED to that colorfor (int ledNumber=0; ledNumber<TOTAL_LEDS; ledNumber++) {strip.setPixelColor(ledNumber, 0, 0, 255);}strip.show();//Wait a bit before continuing to breathedelay(StepDelay);}}`
justinw3115

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

Re: NEOPIXEL brightness control

Like this?
Code: Select all | TOGGLE FULL SIZE
`#define TOTAL_LEDS 60void loop(){  breathe(255, 0.008, 5);}void breathe(float MaximumBrightness, float SpeedFactor, float StepDelay){  // 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);  }}`

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

Re: NEOPIXEL brightness control

Yes, just like that, thanks!
justinw3115

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

Re: NEOPIXEL brightness control

Hi! I'm new with Arduino and I'm totally in love with neopixels. I'm working with a NeoPixel Stick - 8 x WS2812 5050 RGB LED with Integrated Drivers.

I'm using a simple button to change stick's color but I want to add a potentiometer to dim brightness without changing the color. Is it possible? Would you help me?

This is the code I am using:

Code: Select all | TOGGLE FULL SIZE
`#include <Adafruit_NeoPixel.h>#define BUTTON_PIN 2#define PIXEL_PIN 6#define PIXEL_COUNT 8Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);bool oldState = HIGH;int showType = 0;void setup() {  pinMode(BUTTON_PIN, INPUT_PULLUP);  strip.begin();  strip.show();}void loop() {  // Get current button state.  bool newState = digitalRead(BUTTON_PIN);    // Check if state changed from high to low (button press).  if (newState == LOW && oldState == HIGH) {    // Short delay to debounce button.    delay(20);    // Check if button is still low after debounce.    newState = digitalRead(BUTTON_PIN);    if (newState == LOW) {      showType++;      if (showType > 8)        showType=0;      startShow(showType);    }  }    oldState = newState;}void startShow(int i) {  switch(i){    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Off            break;    case 1: colorWipe(strip.Color(255, 255, 255), 50);  // White            break;    case 2: colorWipe(strip.Color(255, 0, 0), 50);  // Red            break;    case 3: colorWipe(strip.Color(255, 90, 0), 50);  // Orange            break;    case 4: colorWipe(strip.Color(255, 255, 0), 50);  // Yellow            break;    case 5: colorWipe(strip.Color(0, 255, 0), 50);  // Green            break;    case 6: colorWipe(strip.Color(0, 0, 255), 50);  // Blue            break;    case 7: colorWipe(strip.Color(255, 0, 255), 50);  // Purple            break;    case 8: colorWipe(strip.Color(255, 255, 255), 50);  // White            break;  }}void colorWipe(uint32_t c, uint8_t wait) {  for(uint16_t i=0; i<strip.numPixels(); i++) {      strip.setPixelColor(i, c);      strip.show();      delay(wait);  }}`

escazu005

Posts: 3
Joined: Mon Feb 09, 2015 9:07 pm

Re: NEOPIXEL brightness control

The general method is to change the 255's to a number between 1..255 to be the brightness level, based on the potentiometer. Basically the last 3 arguments to the 4 argument setPixelColor are 0..255 that are the brightness for the red, green, and blue LEDs inside of each neopixel. Similarly for the 2 argument setPixelColor, the uint32 argument encodes the 3 colors and their brightness level.

michaelmeissner

Posts: 1461
Joined: Wed Aug 29, 2012 12:40 am

Re: NEOPIXEL brightness control

Thanks Michael! but I'm pretty new on Arduino and Neopixels. Could you give more details?

escazu005

Posts: 3
Joined: Mon Feb 09, 2015 9:07 pm

Re: NEOPIXEL brightness control

You are going to have to sit down, and learn the rudiments of programming. Unfortunately, most of the texts I've seen over-simplify it and only talk about the appropriate syntax for the language (you need that, but you need more than that). Most people need a spark of insight to go from desire to code. I've been programming for 40 years now, and I really don't know how to get people past that threshold, since my own spark happened so long ago, and coding is now so instinctual to me. In addition, most people tend to need a live person to help them start coding, or else they just take fragments of code and make changes, without understanding why they are making the changes. Sorry. Is there somebody near by that has recently started Arduino programming, and is a little bit further up the ladder that you can ask? Is there a maker space that might have courses or open nights that you can ask questions of?

Or maybe people who just started out with no programming experience can chime in and say what worked for them.

michaelmeissner

Posts: 1461
Joined: Wed Aug 29, 2012 12:40 am

Re: NEOPIXEL brightness control

One other thing. Don't defeat yourself. Don't say 'I can't do this' without adding the words 'right now'. If you keep telling yourself that you can't do something, then it will be a self-fulfilling prophesy. While if you say 'I can't do this right now', it should spur you to figure out what the next step is.

michaelmeissner

Posts: 1461
Joined: Wed Aug 29, 2012 12:40 am

Re: NEOPIXEL brightness control

Thanks Michael I'll take your advice :)

escazu005

Posts: 3
Joined: Mon Feb 09, 2015 9:07 pm

Re: NEOPIXEL brightness control

You might want to start with our Learn Arduino series:

Once you get your analog input working, you can use the setBrightness function of the neopixel library to set the overall brightness level of the pixels.

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

Re: NEOPIXEL brightness control

I've been working on a small library which does not require any modification to the stock Adafruit library. It simply provides a rgbl() function.
It takes into account color levels of primary & secondary colors (since secondary colors require up to 3x leds).
I would be stoked to see parts of it included in the Adafruit NeoPixel library by default.

I have posted the initial version to github: https://github.com/yesnoio/yesnoio-rgbl

yesnoio

Posts: 6
Joined: Sat Nov 30, 2013 8:08 am

Re: NEOPIXEL brightness control

Hi, so I'm a complete noob with Arduino, Neopixels, and any kind of coding, but I'm a pretty quick study.
I am using a Trinket, and I modified the code below to suit my purpose one action of my project (the "Idle" action), now I need to do two more. The second could maybe be a variation of this, but it must start with all LEDs at zero then fade up to max (defined) brightness, then fast flicker like it's about too explode. :)
Can this code be easily modified to do this, or can you provide some guidance as to how best?

For some context, I am making Ornstein's Dragonslayer Spear from the video game Dark Souls for a cosplay. It's a lightning spear, that will charge up, then fire a bolt of lightning. The 'blade' section of the spear will have about 86 neopixels (43 each back to back) inside a polycarbonite tube cast into translucent urethane resin.

The spear will have three modes of operation:

1) Idle - just powered on, the spear will do the modified breathe code below with color 255,128,0, maxbrightness 128, speedfactor .012, delay 4 (Done)

2) Charge up - upon a button press (I assume I need to use another trinket pin and do pullup or something, haven't read into it that far yet) it will start from all LEDs off, then fade up to 192 brightness (same color as 1 above), then flicker at a high rate (This mode I need help with)

3) Firing - upon another button press (not sure if this can be the same button or will have to be another on a separate pin) It will jump to max white brightness 255 then a quick chase from back to front (This I can do I think)

The other issue I need to account for is that modes 2 and 3 can't loop. Mode 2 must hold at the flicker until mode 3 is activated, and mode 3 must return to mode 1 automatically after the one sequence.

Thanks in advance, this stuff is really cool!

Code: Select all | TOGGLE FULL SIZE
`#define TOTAL_LEDS 60void loop(){  breathe(255, 0.008, 5);}void breathe(float MaximumBrightness, float SpeedFactor, float StepDelay){  // 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);  }}`

Rated_M

Posts: 10
Joined: Wed Mar 11, 2015 1:31 pm

Re: NEOPIXEL brightness control

Posts: 82481
Joined: Sat Feb 07, 2009 10:11 am

Re: NEOPIXEL brightness control

Okay wow, that's a little more involved, but thanks. :)
So once I get the code for my three patterns sorted out, and manipulate the multitasking code to fit what I'm after, will the Trinket have enough memory/capabilities to run it or will I need an Arduino?

Thanks

Rated_M

Posts: 10
Joined: Wed Mar 11, 2015 1:31 pm

Please be positive and constructive with your questions and comments.