0

NEOPIXEL brightness control
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: NEOPIXEL brightness control

by kit_ho on Tue Mar 15, 2016 8:08 am

*poowfftt* mind blown, why didn't I thought about that! Something as simple as that, is very enlightening, thanks again! XD

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

Re: NEOPIXEL brightness control

by UnknownBoyPT on Fri Jun 24, 2016 12:49 pm

Hello Guys,

I'm also very new at this subject like arduino and leds but I have to do a work for my master degree so I really have to figure something out.

I've this code, and I'd like to make it "breathe" in another color but I couldn't figure it out. Right now it just breathes in white. What should I change?

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6
#define LEDS 24

const int  CYCLE_MILLISECONDS = 5000; // 5 second breathing cycle.
const uint8_t KEYFRAMES[]  = {
  // Rising
  20, 21, 22, 24, 26, 28, 31, 34, 38, 41, 45, 50, 55, 60, 66, 73, 80, 87, 95,
  103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220, 228, 236,
  242, 247, 251, 254, 255,

  // Falling
  254, 251, 247, 242, 236, 228, 220, 211, 202, 192, 182, 172, 161, 151, 141,
  131, 121, 112, 103, 95, 87, 80, 73, 66, 60, 55, 50, 45, 41, 38, 34, 31, 28,
  26, 24, 22, 21, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
};

unsigned long lastBreath = 0;
int keyframePointer = 0;

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

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

void loop() {
  int numKeyframes = sizeof(KEYFRAMES) - 1;
  float period = CYCLE_MILLISECONDS / numKeyframes;
  unsigned long now = millis();

  if ((now - lastBreath) > period) {
    lastBreath = now;

    for (int i = 0; i < strip.numPixels(); i++) {
      uint8_t color = (127 * KEYFRAMES[keyframePointer]) / 256;
      strip.setPixelColor(i, strip.Color(KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer]));
    }
    strip.show();

    // Increment the keyframe pointer.
    if (++keyframePointer > numKeyframes) {
      // Reset to 0 after the last keyframe.
      keyframePointer = 0;
    }
  }

}
Last edited by adafruit_support_bill on Fri Jun 24, 2016 1:11 pm, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

UnknownBoyPT
 
Posts: 5
Joined: Fri Jun 24, 2016 12:47 pm

Re: NEOPIXEL brightness control

by adafruit_support_bill on Fri Jun 24, 2016 1:15 pm

I'd like to make it "breathe" in another color but I couldn't figure it out. Right now it just breathes in white. What should I change?


This line sets the R, G and B component of the color to all the same value, so it is white.
Code: Select all | TOGGLE FULL SIZE
      strip.setPixelColor(i, strip.Color(KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer]));


If you want a different color, first you need to define what the RGB components of that color are, then scale the values proportionally for the fade.

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

Re: NEOPIXEL brightness control

by UnknownBoyPT on Fri Jun 24, 2016 2:22 pm

adafruit_support_bill wrote:
I'd like to make it "breathe" in another color but I couldn't figure it out. Right now it just breathes in white. What should I change?


This line sets the R, G and B component of the color to all the same value, so it is white.
Code: Select all | TOGGLE FULL SIZE
      strip.setPixelColor(i, strip.Color(KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer]));


If you want a different color, first you need to define what the RGB components of that color are, then scale the values proportionally for the fade.


Could you please explain me how could I do that? Because I already changed that line to something similar to this:

strip.setPixelColor(i, 255, 0, 0);

But then the lights just stay in fixed red color and not breathing anymore

UnknownBoyPT
 
Posts: 5
Joined: Fri Jun 24, 2016 12:47 pm

Re: NEOPIXEL brightness control

by adafruit_support_bill on Fri Jun 24, 2016 2:39 pm

Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, KEYFRAMES[keyframePointer], 0, 0);

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

Re: NEOPIXEL brightness control

by UnknownBoyPT on Fri Jun 24, 2016 3:07 pm

adafruit_support_bill wrote:
Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, KEYFRAMES[keyframePointer], 0, 0);


Thank you very much! And what should I do to change it to other colors? Like Yellow, Pink, or other colors with RGB color code? Because I was trying with some of them but it didn't work.

UnknownBoyPT
 
Posts: 5
Joined: Fri Jun 24, 2016 12:47 pm

Re: NEOPIXEL brightness control

by adafruit_support_bill on Sat Jun 25, 2016 5:38 am

The general formula is:

Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, (Red * KEYFRAMES[keyframePointer]) / 255, (Green* KEYFRAMES[keyframePointer]) / 255, (Blue* KEYFRAMES[keyframePointer]) / 255);

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

Re: NEOPIXEL brightness control

by UnknownBoyPT on Sat Jun 25, 2016 10:43 am

adafruit_support_bill wrote:The general formula is:

Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, (Red * KEYFRAMES[keyframePointer]) / 255, (Green* KEYFRAMES[keyframePointer]) / 255, (Blue* KEYFRAMES[keyframePointer]) / 255);


Thank you so much! It worked perfectly!

One last question, is it possible to make it "breathe" with different breathing? In another words, as we human, we always breathe differently, one more intense or less, and so on. Is is possible to do the same here? To make it look more "natural" instead of just fade in and fade out?

UnknownBoyPT
 
Posts: 5
Joined: Fri Jun 24, 2016 12:47 pm

Re: NEOPIXEL brightness control

by adafruit_support_bill on Sat Jun 25, 2016 11:20 am

One thing that would be fairly straightforward to do is to randomize the timing a bit. Changing CYCLE_MILLISECONDS will alter the rhythm of the breathing. The random function is documented here: https://www.arduino.cc/en/Reference/Random

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

Re: NEOPIXEL brightness control

by UnknownBoyPT on Sat Jun 25, 2016 11:29 am

adafruit_support_bill wrote:One thing that would be fairly straightforward to do is to randomize the timing a bit. Changing CYCLE_MILLISECONDS will alter the rhythm of the breathing. The random function is documented here: https://www.arduino.cc/en/Reference/Random


Thank you! I'll check on that!

UnknownBoyPT
 
Posts: 5
Joined: Fri Jun 24, 2016 12:47 pm

Re: NEOPIXEL brightness control

by kenan03 on Fri Oct 21, 2016 1:15 pm

Hello,

this is my first project and it will include a strip segment of neopixel strip as well as two nexopixel rings (the 16 pixel version). I was wanting to make the entire set of lights have a light blue color breathing effect. I was directed to @adafruit_support_rick who provided a very helpful code for a breathing light pattern (quoted below). How would I need to modify this code to make it work with the setup I have above (one strip and two rings all connected). Thank you in advance!

Albert


adafruit_support_rick wrote:You guys might want to have a look at the breathing sketch I wrote a while ago.

BreatheRightStrip.ino:

Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_NeoPixel.h"
#include "SPI.h"

//Demonstrates "breathing" effect on NeoPixel Strip

#define LED_PIN  6   
#define N_LEDS  16   

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);


uint8_t LED_Breathe_Table[]  = {   80,  87,  95, 103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220,
              228, 236, 242, 247, 251, 254, 255, 255, 254, 251, 247, 242, 236, 228, 220, 211,
              202, 192, 182, 172, 161, 151, 141, 131, 121, 112, 103,  95,  87,  80,  73,  66,
               60,  55,  50,  45,  41,  38,  34,  31,  28,  26,  24,  22,  20,  20,  20,  20,
               20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,
               20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  22,  24,  26,  28,
               31,  34,  38,  41,  45,  50,  55,  60,  66,  73 };


#define BREATHE_TABLE_SIZE (sizeof(LED_Breathe_Table))
#define BREATHE_CYCLE    6000      /*breathe cycle in milliseconds*/
#define BREATHE_UPDATE    (BREATHE_CYCLE / BREATHE_TABLE_SIZE)


void setup() {
  // Start up the LED strip
  strip.begin();

  // Update the strip, to start they are all 'off'
  strip.show();
}


void loop() {
  int cycle;
  for (cycle=0; cycle < 4; cycle++) {
    uniformBreathe(LED_Breathe_Table, BREATHE_TABLE_SIZE, BREATHE_UPDATE, 256, 256, 256);
  }
  for (cycle=0; cycle < 4; cycle++) {
    sequencedBreathe(LED_Breathe_Table, BREATHE_TABLE_SIZE, BREATHE_UPDATE, 256, 256, 256);
  }
}

void uniformBreathe(uint8_t* breatheTable, uint8_t breatheTableSize, uint16_t updatePeriod, uint16_t r, uint16_t g, uint16_t b)
{
  int i;
  uint8_t breatheIndex = 0;
  uint8_t breatheRed;
  uint8_t breatheGrn;
  uint8_t breatheBlu;
 
  for (breatheIndex = 0; breatheIndex < breatheTableSize; breatheIndex++) {
    for (i=0; i < strip.numPixels(); i++) {
      breatheRed = (r * breatheTable[breatheIndex]) / 256;
      breatheGrn = (g * breatheTable[breatheIndex]) / 256;
      breatheBlu = (b * breatheTable[breatheIndex]) / 256;
      strip.setPixelColor(i, breatheRed, breatheGrn, breatheBlu);
    }
    strip.show();   // write all the pixels out
    delay(updatePeriod);
  }
}

void sequencedBreathe(uint8_t* breatheTable, uint8_t breatheTableSize, uint16_t updatePeriod, uint16_t r, uint16_t g, uint16_t b)
{
  int i;
  uint8_t breatheIndex = 0;
  uint8_t breatheRed;
  uint8_t breatheGrn;
  uint8_t breatheBlu;
  uint8_t sequenceIndex;
 
  for (breatheIndex = 0; breatheIndex < breatheTableSize; breatheIndex++) {
    for (i=0; i < strip.numPixels(); i++) {
      sequenceIndex = (breatheIndex + (i*4)) % breatheTableSize;
      breatheRed = (r * breatheTable[sequenceIndex]) / 256;
      breatheGrn = (g * breatheTable[sequenceIndex]) / 256;
      breatheBlu = (b * breatheTable[sequenceIndex]) / 256;
      strip.setPixelColor(i, breatheRed, breatheGrn, breatheBlu);
    }
    strip.show();   // write all the pixels out
    delay(updatePeriod);
  }
}

kenan03
 
Posts: 5
Joined: Mon Oct 06, 2014 9:09 am

Re: NEOPIXEL brightness control

by adafruit_support_bill on Fri Oct 21, 2016 1:29 pm

The only thing you need to change is this line:
Code: Select all | TOGGLE FULL SIZE
#define N_LEDS  16 

Change N_LEDS to the total number of pixels in your strip plus the rings.

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

Re: NEOPIXEL brightness control

by Bazeman on Tue May 02, 2017 6:29 pm

Hi,

I have been trying to get a effect with the neopixels but I won't get it right... At page 4 adafruit_support_rick responded to Chargerdude70 who had added some code to the library individually control each pixel in brightness.. I have tried to add this code also to the library but then afterwards the sketch won't compile anymore.. Can someone exactly tell what lines I have to add were to make this sketch to work:

Code: Select all | TOGGLE FULL SIZE
//chaseFade....Make lights fade from low to high intensity while lighting one after another
#include <Adafruit_NeoPixel.h>
const int ledNum = 13; // Number of LED's
const int ledPin = 6; // ATtiny85 LED signal output pin #
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(ledNum, ledPin);
uint32_t Blue = pixels.Color(0, 255, 0);
uint16_t i, l;
#define DELAY 30 // Time delay between Pixels

void setup() {
  pixels.begin();
  pixels.show();
  pinMode(ledPin, OUTPUT);
}

// chaseFade
void loop() {
  int chase = 0;
  uint8_t brightIncrement = 255 / pixels.numPixels();
  for (chase; chase < 5; chase++) {
    for (i = 0; i < ledNum; i++) {
      pixels.setPixelColor(i, Blue, ((i + 1) * brightIncrement));
      pixels.show();
      delay(DELAY);
    }
    for (i = 0; i < ledNum; i++) {
      pixels.setPixelColor(i, 0, 0, 0);
      pixels.show();
    }
    delay(1000);
  }
}


I hope you guys can help

Bazeman
 
Posts: 3
Joined: Tue May 02, 2017 6:18 pm

Re: NEOPIXEL brightness control

by adafruit_support_rick on Tue May 02, 2017 7:57 pm

You should post the errors you're getting instead of just saying "it doesn't compile".
This line won't compile: There is no setPixelColor function that takes 3 arguments:
Code: Select all | TOGGLE FULL SIZE
      pixels.setPixelColor(i, Blue, ((i + 1) * brightIncrement));

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

Re: NEOPIXEL brightness control

by Bazeman on Wed May 03, 2017 6:12 am

Hi Rick,

Thanks for the reply.

I know that the line in the code won't compile because it is not set in the standard Neopixel library. My question foremost is where exactly I have to put the extra arguments in the .h and .ccp file.

If I'm correct this should be added to .h somewhere:
Code: Select all | TOGGLE FULL SIZE
setPixelColor(uint16_t n, uint32_t c, uint16_t brightness),


And this should be added to .ccp somewhere:
Code: Select all | TOGGLE FULL SIZE
// Set pixel brightness individually using seperate R,G,B components:
void 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));
}

// Set pixel brightness individually using a 'packed' 32-bit RGB color:
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c, uint16_t brightness) {
   uint8_t
      r = (uint8_t)(c >> 16),
      g = (uint8_t)(c >>  8),
      b = (uint8_t)c;
  setPixelColor(n, r, g, b, brightness);
}


Sorry for the noob question.... :-{ But thanks for any help!

Bazeman
 
Posts: 3
Joined: Tue May 02, 2017 6:18 pm

Please be positive and constructive with your questions and comments.