0

Gradually Increase Brightness of NeoPixel Strip over 3sec Pe
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Gradually Increase Brightness of NeoPixel Strip over 3sec Pe

by jordanat2012 on Sun Sep 16, 2018 6:29 pm

Hi all,
What I'm trying to do would be, I thought at least, fairly straightforward. I have a NeoPixel strip that I want to gradually increase the brightness of, automatically, over a 3second period. I would like the color to be just white, and increase from a dim white to a bright white.

I've been looking through posts, and it appears that setBrightness in the NeoPixel library should be modified. Is that correct? Would this be done via a For loop? Any chance someone would be willing to share some sample code?

jordanat2012
 
Posts: 5
Joined: Sun May 20, 2018 8:51 am

Re: Gradually Increase Brightness of NeoPixel Strip over 3se

by kcl1s on Sun Sep 16, 2018 8:23 pm

From the Uberguide https://learn.adafruit.com/adafruit-neopixel-uberguide/the-magic-of-neopixels
setBrightness() was intended to be called once, in setup(), to limit the current/brightness of the LEDs throughout the life of the sketch. It is not intended as an animation effect itself! The operation of this function is “lossy” — it modifies the current pixel data in RAM, not in the show() call — in order to meet NeoPixels’ strict timing requirements. Certain animation effects are better served by leaving the brightness setting alone, modulating pixel brightness in your own sketch logic and redrawing the full strip with setPixel().


So using setBrightness is not the correct way. A better way is to adjust the individual rgb pixel values in a for loop like this.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup()  {
    pixels.begin();
    pixels.show();
   
}

void loop() {
  for(int x = 5; x< 256; x++) {
    for(int i= 0; i<NUMPIXELS; i++) {
      pixels.setPixelColor(i, x,x,x);
    }
    pixels.show();
    delay(12); //3000 ms / 250 steps = 12 ms delay
  } 
}


Code compiles but was not tested on hardware.

Fellow hobbyist
Keith

kcl1s
 
Posts: 952
Joined: Tue Aug 30, 2016 12:06 pm

Re: Gradually Increase Brightness of NeoPixel Strip over 3se

by jordanat2012 on Mon Sep 17, 2018 6:31 am

This is great - thanks Keith! This code performs exactly as I had hoped to see.

I hadn't seen that section in the Uberguide; I must have missed it when skimming. Thank you for the explanation. Being a self-taught coder, I always learn something from this group.

Thanks so much!

jordanat2012
 
Posts: 5
Joined: Sun May 20, 2018 8:51 am

Please be positive and constructive with your questions and comments.