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
Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: NEOPIXEL brightness control

Post by Tissi_2 »

I have 3 neopixel rings 16. I am trying to combine the color wheel function with the breathing effect, but I don´t get it working. I dont know how to implement the brightness table code.

Code: Select all

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Code: Select all

void rainbowCycleRing1(uint8_t wait) {
    
    for(i=0; i<16; i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / 16) + j) & 255));
    }
    for(i=16; i<48; i++) {
      strip.setPixelColor(i, strip.Color(0,0,0));
    }
    strip.show();
Does the table need a specific number of values? As I modified the table values by adding, erasing or editing values, the pixels kind of moved clockwise. Is there a chance of changing the brightness of all pixels and also being able to receive button inputs (multitasking)?

User avatar
BulldogLowell
 
Posts: 7
Joined: Thu Oct 30, 2014 10:39 pm

Re: NEOPIXEL brightness control

Post by BulldogLowell »

adafruit_support_rick wrote:You're welcome!
"Breathing" new life to this thread...

I'm looking for a prototype class for "breathing" a subset of a neopixel array.

i.e fix the color of pixel 0 through 9 and breathe pixels 10-15.

my thinking is to create an array of elements to breathe:

Code: Select all

const uint8_t breathyBit[8] = {10,11,12,13,14,15};
and address that array from a function:

Code: Select all

void breathSegment(const uint8_t * segment)
{
    fadeSegment( )
}
leaving the state of the other pixels unchanged.

User avatar
BulldogLowell
 
Posts: 7
Joined: Thu Oct 30, 2014 10:39 pm

Re: NEOPIXEL brightness control

Post by BulldogLowell »

OK, so I figured out a quick way...

Code: Select all

void breatheUpdate(const uint8_t * segment, const uint32_t increment, const uint8_t step)
{
  static uint32_t lastTimeChange = 0;
  static uint8_t direction = 1;
  static uint8_t value;
  if(millis() - lastTimeChange > increment)
  {
    value += (direction * step);
    if (value <=0 || value >= 255)
    {
      direction = direction * -1;
    }
    for(uint8_t i = 0; i < sizeof(segment) * 2; i++)
    {
      myPixels.setPixelColor(segment[i], myPixels.Color(0, 0, value));
    }
    myPixels.show();
    lastTimeChange += increment;
  }
}
and put this in your loop() function

Code: Select all

void loop()
{
  breatheUpdate(currentWeather, BREATH_RATE, 1);

User avatar
mjemlemm
 
Posts: 1
Joined: Tue Oct 06, 2015 2:32 pm

Re: NEOPIXEL brightness control

Post by mjemlemm »

I want to fade my neopixel and I used this code:

Code: Select all

#define TOTAL_LEDS 60

void loop()
{
  breathe(strip.Color(255, 0, 0), 255, 0.008, 5);
  breathe(strip.Color(0, 255, 0), 255, 0.008, 5);
  breathe(strip.Color(0, 0, 255), 255, 0.008, 5);
}

void breathe(uint32_t color, 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, color);
    }

    strip.show();
    //Wait a bit before continuing to breathe
    delay(StepDelay);
  }
}
it worked! but I don't know what to change to go from low brightness to high brightness in stead off from high to low, that's what he is doing at the moment.

Can someone help me? I am very new at this
Last edited by adafruit_support_bill on Tue Oct 06, 2015 2:38 pm, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

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

Re: NEOPIXEL brightness control

Post by adafruit_support_bill »

change this line:

Code: Select all

    strip.setBrightness(intensity);
To:

Code: Select all

    strip.setBrightness(255-intensity);

User avatar
rbbtebo
 
Posts: 57
Joined: Tue Aug 18, 2015 9:12 am

Re: NEOPIXEL brightness control

Post by rbbtebo »

What is the difference between:
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(60, PIN); and
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN);

User avatar
BulldogLowell
 
Posts: 7
Joined: Thu Oct 30, 2014 10:39 pm

Re: NEOPIXEL brightness control

Post by BulldogLowell »

rbbtebo wrote:What is the difference between:
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(60, PIN); and
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN);
in the first, you are naming your instance of an Adafruit_NeoPixel object "pixels" and in the second you are naming it "strip"

so, further down the road, you will access member functions and variables using the pixels or strip name like this"

Code: Select all

pixels.show();
or

Code: Select all

strip.show();

User avatar
rbbtebo
 
Posts: 57
Joined: Tue Aug 18, 2015 9:12 am

Re: NEOPIXEL brightness control

Post by rbbtebo »

Confirming then....

It could be ANY name - Dog, cat, mouse, etc. as long as I followed the that convention in the rest of the program....

User avatar
jimblue
 
Posts: 71
Joined: Wed Oct 23, 2013 5:54 pm

Re: NEOPIXEL brightness control

Post by jimblue »

rbbtebo wrote:Confirming then....

It could be ANY name - Dog, cat, mouse, etc. as long as I followed the that convention in the rest of the program....
Yes. The program looks for name matches. If you use 'dog' in the program, but never defined it, you will get an error.

User avatar
rbbtebo
 
Posts: 57
Joined: Tue Aug 18, 2015 9:12 am

Re: NEOPIXEL brightness control

Post by rbbtebo »

Adafruit is ALWAYS AWESOME with their help. Thank you!

User avatar
kit_ho
 
Posts: 7
Joined: Mon Mar 14, 2016 4:08 am

Re: NEOPIXEL brightness control

Post by kit_ho »

Hey guys,
in regards with the breathing code posted at the beginning of this thread, I tried to make a Rainbow breathing strip. Is there a way to change the minimum brightness to 40 or other value instead of fading to 0? I don't know anything about coding.. so, thank you in advance if anyone is able to help.

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 12

Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);

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

void loop() {
  float MaxBrightness = 255;
  float SpeedFactor = 0.008;
  float wait = 25;

  for(int b=0;b<65535;b++) {
    float intensity = MaxBrightness/2.0*(1.0+sin(SpeedFactor*b));
    strip.setBrightness(intensity);
    for (uint8_t i=0;i<strip.numPixels();i++){
      strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
    }
    strip.show();
    delay(wait);
  }
}


//Neopixel Wheel - Rainbow
uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

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

Re: NEOPIXEL brightness control

Post by adafruit_support_bill »

Change your loop to something like this:

Code: Select all

void loop() 
{
  float MinBrightness = 40;
  float MaxBrightness = 255 - MinBrightness;
  float SpeedFactor = 0.008;
  float wait = 25;

  for(int b=0;b<65535;b++) 
  {
    float intensity = MaxBrightness/2.0*(1.0+sin(SpeedFactor*b)) + MinBrightness;
    strip.setBrightness(intensity);
    for (uint8_t i=0;i<strip.numPixels();i++)
    {
      strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
    }
    strip.show();
    delay(wait);
  }
}

User avatar
kit_ho
 
Posts: 7
Joined: Mon Mar 14, 2016 4:08 am

Re: NEOPIXEL brightness control

Post by kit_ho »

Thanks a lot! that did the job. :)

User avatar
kit_ho
 
Posts: 7
Joined: Mon Mar 14, 2016 4:08 am

Re: NEOPIXEL brightness control

Post by kit_ho »

I've sketched a breathing code. Take a look (:
Strip can fade in and out or stay lit for awhile before fading and you can set how many loops for each color. Plus it doesn't use complex calculation that I don't understand xD
Most of the code were found on the net and put together with some tweaks here and there.
Let me know what you guys think, this is my very first project. Does everything looks good and can it be refined and made simpler?

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 12
#define NUMPIXEL 30   //how many pixel on the strip.

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXEL, PIN, NEO_GRB + NEO_KHZ800);

int MinBrightness = 30;       //value 0-255  
int MaxBrightness = 170;      //value 0-255--equation (maxBrightness*maxBrightness/255) hence maxBrightness^2/255 hence the value 170 actually equal to 113.33333 .

int numLoops1 = 4;
int numLoops2 = 2;
int numLoops3 = 5;
int numLoops4 = 3;            //add new integer and value for different colors if needed.

int fadeHoldWait = 300;       //how many steps to stay lit before dimming again.
int fadeInWait = 15;          //lighting up speed, steps.
int fadeOutWait = 30;         //dimming speed, steps.

//---------------------------------------------------------------------------------------------------//

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

void loop() {
  
  //---strip will stay lit for some time before dimming again.----
  rgbFadeIn_Hold_Out(strip.Color(255,0,0),numLoops1);  //red.
  rainbowFadeIn_Hold_Out(numLoops2); 
  
  //---normal breathing.----
  rgbFadeInOut(strip.Color(60,255,0),numLoops3);      //green.
  rainbowFadeInOut(numLoops4);
}

void rgbFadeIn_Hold_Out(uint32_t c, uint8_t x) {
  for (int j=0;j<x;j++){
    for(uint8_t b=MinBrightness;b<MaxBrightness;b++){
      strip.setBrightness(b*MaxBrightness/255);
      for (uint16_t i=0;i<strip.numPixels();i++){
        strip.setPixelColor(i,c);
      }
      strip.show();
      delay(fadeInWait);
    }
    strip.setBrightness(MaxBrightness*MaxBrightness/255);
    for (uint16_t i=0;i<strip.numPixels();i++){
      strip.setPixelColor(i,c);
      strip.show();
      delay(fadeHoldWait);
    }
    for(uint8_t b=MaxBrightness;b>MinBrightness;b--){
      strip.setBrightness(b*MaxBrightness/255);
      for (uint16_t i=0;i<strip.numPixels();i++){
        strip.setPixelColor(i,c);
      }
      strip.show();
      delay(fadeOutWait);
    }
  }
}

void rainbowFadeIn_Hold_Out(uint8_t x) {
  for (int j=0;j<x;j++){
    for(uint8_t b=MinBrightness;b<MaxBrightness;b++){
      strip.setBrightness(b*MaxBrightness/255);
      for (uint8_t i=0;i<strip.numPixels();i++){
        strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
      }
      strip.show();
      delay(fadeInWait);
    }
    strip.setBrightness(MaxBrightness*MaxBrightness/255);
    for(uint8_t i=0;i<strip.numPixels();i++){
      strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
      strip.show();
      delay(fadeHoldWait);
    }
    for (uint8_t b=MaxBrightness;b>MinBrightness;b--){
      strip.setBrightness(b*MaxBrightness/255);
      for(uint8_t i=0;i<strip.numPixels();i++) {
        strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
      }
      strip.show();
      delay(fadeOutWait);
    }
  }
}

void rgbFadeInOut(uint32_t c, uint8_t x){
  for(int j=0;j<x;j++){
    for(uint8_t b=MinBrightness;b<MaxBrightness;b++){
      strip.setBrightness(b*MaxBrightness/255);
      for(uint16_t i=0;i<strip.numPixels();i++){
        strip.setPixelColor(i,c);
      }
      strip.show();
      delay(fadeInWait);
    }
    for(uint8_t b=MaxBrightness;b>MinBrightness;b--) {
      strip.setBrightness(b*MaxBrightness/255);
      for (uint16_t i=0;i<strip.numPixels();i++) {
        strip.setPixelColor(i,c);
      }
      strip.show();
      delay(fadeOutWait);
    }
  }
}

void rainbowFadeInOut(uint8_t x){
  for(int j=0;j<x;j++){
    for(uint8_t b=MinBrightness;b<MaxBrightness;b++){
      strip.setBrightness(b*MaxBrightness/255);
      for(uint8_t i=0;i<strip.numPixels();i++){
        strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
      }
      strip.show();
      delay(fadeInWait);
    }
    for(uint8_t b=MaxBrightness;b>MinBrightness;b--) {
      strip.setBrightness(b*MaxBrightness/255);
      for (uint8_t i=0;i<strip.numPixels();i++) {
        strip.setPixelColor(i,Wheel(i*256/strip.numPixels()));
      }
      strip.show();
      delay(fadeOutWait);
    }
  }
}


//NeoPixel Wheel for Rainbow-----------------

uint32_t Wheel(byte WheelPos) {
  WheelPos = 127 - WheelPos;       //the value here means - for 255 the strip will starts with red, 127-red will be in the middle, 0 - strip ends with red.
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

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

Re: NEOPIXEL brightness control

Post by adafruit_support_bill »

Looks good. You could simplify by combining the FadeInOut functions and the FadeIn_Hold_Out functions by just passing in the "fadeHoldWait" parameter. If you pass in a zero, there is no hold time.

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

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