0

NEOPIXEL brightness control
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: NEOPIXEL brightness control

by adafruit_support_bill on Wed Mar 11, 2015 3:09 pm

It may start to get a little tight for space with 86 pixels on a trinket. It depends on how complex your code gets.

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

Re: NEOPIXEL brightness control

by Rated_M on Fri Mar 13, 2015 2:50 pm

Okay, I spent several hours trying to understand and manipulate the code in the Multitasking Part 3 tutorial, but alas much of it appears to be beyond my comprehension, and also far more complex than needed for what I am trying to accomplish.

I don't need to control multiple neopixel segments independently and simultaneously, just one. (My 86 neopixels is actually only 2 strips of 43 doing the same function for front/back display)
I only need black(0,0,0) plus orange(255,128,0), and the brightness/color levels in between for fade up/down.
The IDLE pattern just does fade up/down, the breathing sketch is perfect for this.
The Button1 press CHARGE pattern needs to do fade up, then fast flash continuously until Button2 press.
The Button2 press FIRE pattern is just one flash of max brightness followed by an ascending fast scanner/cylon then back to IDLE.

I understand the premise of getting the patterns outside the loop and just using the loop to check status of the inputs, so it sounds like I will need to do something like that to get the results I want, but is there a simpler way? If not can you or anyone cite some other less complex example sketches available that I could try to model after?

Any guidance appreciated.

Thanks again.

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

Re: NEOPIXEL brightness control

by adafruit_support_bill on Fri Mar 13, 2015 4:19 pm

Start simple. Take it one step at a time.

If you only have one set of pixels, only define one.
Code: Select all | TOGGLE FULL SIZE
NeoPatterns Pixels(43, 5, NEO_GRB + NEO_KHZ800, &PixelsComplete);

Get the fade working first. That is trivial.
Code: Select all | TOGGLE FULL SIZE
    // Initialize all the pixelStrips
    Pixels.begin();

    // Kick off a pattern
    Pixels.Fade(Pixels.Color(0,0,0), Pixels.Color(255,128,0), 100, 1);  // black to orange

Next, implement button 1.
When that is working, implement button 2.

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

Re: NEOPIXEL brightness control

by Tissi_2 on Mon Jul 20, 2015 7:36 pm

I want to get back to the "breathing code" that was posted earlier and is still related to the main topic of this thread ( I hope so ). If I load this code up to the arduino, the arduino does not respond to any other inputs from a button or a rotary encoder. Why is it like this and how do I get out of that loop? I tried it with a for loop:
Code: Select all | TOGGLE FULL SIZE
for(int rep = 0; rep <5; rep++){ "breathing code"}

Are you working on an implementation of "new features" like brightness control for each pixel into you neopixel.h?

Tissi_2
 
Posts: 20
Joined: Mon Jun 22, 2015 10:46 am

Re: NEOPIXEL brightness control

by adafruit_support_bill on Tue Jul 21, 2015 5:40 am

If I load this code up to the arduino, the arduino does not respond to any other inputs from a button or a rotary encoder. Why is it like this and how do I get out of that loop?

If you want your code to remain responsive to inputs while updating the pixel patterns, you need a completely different approach.

See this guide for details: https://learn.adafruit.com/multi-taskin ... 3/overview

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

Re: NEOPIXEL brightness control

by sergneri on Thu Aug 06, 2015 6:44 pm

Anyone figure out how to use breathe and change color? That sine loop is endless, which is great, if one could just change colors at the bottom of the loop or every 5 iterations.
Merci Bien!

sergneri
 
Posts: 35
Joined: Mon Jan 20, 2014 9:00 pm

Re: NEOPIXEL brightness control

by adafruit_support_rick on Fri Aug 07, 2015 8:24 am

You mean this breathe function?
Code: Select all | TOGGLE FULL SIZE
#define TOTAL_LEDS 60

void 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);
  }
}


What about doing this?
Code: Select all | TOGGLE FULL SIZE
#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);
  }
}

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

Re: NEOPIXEL brightness control

by sergneri on Fri Aug 07, 2015 7:27 pm

Yes, that is the one, thank you.
In my many failed attempts to do this, I didn't use the strip.Color(255, 0, 0) argument, rather, just tried to set variables to color values and passed them along, which didn't work. I did type them correctly, normally using the uint32_t type.
Also, I see that the value for the i loop can be reduced from 65535 for quicker runs. I'm using 5530 at the moment. With the quicker loop time,
Great thread, thanks everyone.

sergneri
 
Posts: 35
Joined: Mon Jan 20, 2014 9:00 pm

Re: NEOPIXEL brightness control

by chargerdude70 on Sat Aug 08, 2015 7:30 pm

With code attached, I expect it to run 5 times and then move onto another sequence but it doesn't. Could someone explain why and suggest a correction?


Code: Select all | TOGGLE FULL SIZE
void loop() {
  // Breathing effect
  int breathIng = 0;
  for (breathIng; breathIng < 5; breathIng ++) {
    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));
      pixels.setBrightness(intensity);
      // Now set every LED to that color
      for (int ledNumber = 0; ledNumber < NUMPIXELS; ledNumber++) {
        pixels.setPixelColor(ledNumber, REDo);
      }
      pixels.show();
      //Wait a bit before continuing to breathe
      delay(StepDelay);

    }

  }

chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: NEOPIXEL brightness control

by sergneri on Sat Aug 08, 2015 10:11 pm

I believe the number of loops is set here for (int i = 0; i < 65535; i++)
I am playing with values in the 2200 range and seeing 3 to 4 loops, 5800 gave me around 8.
Trick for me is to now get a color change during the minimum intensity value - so one can play with SpeedFactor as well but I'm essentially guessing when the minimum will occur. This code is not right, color change can happen on ramp up or down but is not synced.
Code: Select all | TOGGLE FULL SIZE
   #include <Adafruit_NeoPixel.h>
    #ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
     #include <avr/power.h>
    #endif
    #define PIN 1
    Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN);
    uint32_t color  = (0,0,0); // Start dark
    #define TOTAL_LEDS 40
   
void setup() {
  // put your setup code here, to run once:
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
      if(F_CPU == 16000000) clock_prescale_set(clock_div_1);
    #endif
      strip.begin();
      strip.setBrightness(0); // 1/3 brightness
      strip.show();
}

void loop() {
  // 0.08 speedfactor is fast blinking
  // 0.018 is medium fast
  uint8_t r = random(0x22, 0xff);
  uint8_t g = random(0x22, 0xff);
  uint8_t b = random(0x22, 0xff);
      breathe(strip.Color(r, g, b), 50, 0.008, 5);
//      breathe(strip.Color(0, 255, 0), 105, 0.010, 5);
//      breathe(strip.Color(0, 0, 255), 105, 0.010, 5);
}

 void breathe(uint32_t color, float MaximumBrightness, float SpeedFactor, float StepDelay)
    {
      float intensity;
      // Make the lights breathe
      // 5530 = 8 iterations
      for (int i = 0; i < 2200; 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);
      }
    }

sergneri
 
Posts: 35
Joined: Mon Jan 20, 2014 9:00 pm

Re: NEOPIXEL brightness control

by adafruit_support_rick on Sun Aug 09, 2015 9:46 am

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);
  }
}

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

Re: NEOPIXEL brightness control

by chargerdude70 on Mon Aug 10, 2015 1:09 pm

@ adafruit_support_rick

I tried to compile your "BreatheRightStrip" code and receive this error in Arduino 1.0.5

"no matching function for call to Adafruit_NeoPixel::Adafruit_NeoPixel(int,int,int)"

and this error in 1.6.5

Arduino-V1.6.5-r2\libraries\SPI/SPI.h:67:16: error: 'SPIE' was not declared in this scope
SPCR &= ~_BV(SPIE);

Can you assist?

chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: NEOPIXEL brightness control

by adafruit_support_rick on Mon Aug 10, 2015 2:38 pm

It compiles for me in 1.6.5 with Uno as the target. What's your target processor?

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

Re: NEOPIXEL brightness control

by chargerdude70 on Mon Aug 10, 2015 3:05 pm

ATtiny85,is there something I can change for it to work?

And just verified that it does compile on 1.6.5 with UNO as target

chargerdude70
 
Posts: 47
Joined: Thu Jan 02, 2014 11:49 am

Re: NEOPIXEL brightness control

by adafruit_support_rick on Tue Aug 11, 2015 7:32 am

Delete the line #include "SPI.h"

I don't know how that got in there in the first place...

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

Please be positive and constructive with your questions and comments.