NeoPixel fade - Trinket and LEDs

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
Sytar
 
Posts: 21
Joined: Sat Aug 08, 2015 12:28 am

NeoPixel fade - Trinket and LEDs

Post by Sytar »

I have a package of the 5 NeoPixel LEDs and a Trinket (currently have 3 connected). I have been working on my code to get a fade from one color to another. When I run the code I get more of a jumping from one color to the next rather then a smooth fade. I hope that someone can look at my code and point out what I am doing wrong.

Code: Select all

#include <Adafruit_NeoPixel.h>
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
#include <avr/power.h>
#endif

#define PIN 1
#define Pixels 3

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.


float fadeRate = 20;
int MaxBrightness = 120;


void setup() {
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  strip.begin();
  for (int i = 0; i < Pixels; i++)
  {
    strip.setPixelColor(i, random(0, MaxBrightness), random(0, MaxBrightness), random(0, MaxBrightness));
  }
  strip.show(); // Initialize all pixels to a Random Color.

}


void loop () {
  int redCur;
  int redNew;
  int blueCur;
  int blueNew;
  int greenCur;
  int greenNew;

  if (random(40) == 1) // Wait random time to start changing next pixel
  {
    uint16_t i = random(Pixels);  // Pick a random pixel
    // Get the colors of the current pixel.
    uint16_t c = strip.getPixelColor(i);
    uint8_t  redCur = (uint8_t)(c >> 16);
    uint8_t  greenCur = (uint8_t)(c >>  8);
    uint8_t  blueCur = (uint8_t)c;
    //Pick a new random color
    uint8_t redNew = random(0, MaxBrightness);
    uint8_t greenNew = random(0, MaxBrightness);
    uint8_t blueNew = random(0, MaxBrightness);
    // Start fade between the two colors
    while (redCur != redNew && greenCur != greenNew && blueCur != blueNew)
    {
      if (redNew > redCur) {
        redCur++;
      }
      else if (redNew < redCur) {
        redCur--;
      }
      if (greenNew > greenCur) {
        greenCur++;
      }
      else if (greenNew < greenCur) {
        greenCur--;
      }
      if (blueNew > blueCur) {
        blueCur++;
      }
      else if (blueNew < blueCur) {
        blueCur--;
      }
      // Sets the pixel to the color adjusted in the fade
      strip.setPixelColor(i, greenCur, blueCur, redCur);
      strip.show();
      delay(fadeRate);
    }
  }
}

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

Re: NeoPixel fade - Trinket and LEDs

Post by adafruit_support_bill »

The speed of the transition should be controlled by your 'fadeRate' variable. But the total time of the transition depends on the greatest brightness difference, so it will vary considerably. Also, the R, G and B will all take different times to complete the transition.

If the difference between the old Red and new Red is 20, the Red will transition in 20 iterations.
But if the difference in the old and new Blue is 100, the Blue will take 5 times as long to complete the transition.

The code below specifies a total transition time, then makes all 3 LEDs complete the transition in the same time span.
https://learn.adafruit.com/multi-taskin ... rt-3/fader

User avatar
Sytar
 
Posts: 21
Joined: Sat Aug 08, 2015 12:28 am

Re: NeoPixel fade - Trinket and LEDs

Post by Sytar »

Bill, from looking through the code and trying to figure out how to implement it into my code, I can only guess that this is more of a concept and not actual code that you can cut and paste into a program?

I have tried to put it straight into my code and adjust some settings, but the call functions don't seem to match up. My next step was to try and set it up as an .h file that I can include, and that seemed to be better, but still is seemed like things are missing.

I am going to hack away at the code a bit more, but in the end I may end up with extracting parts of it to get it to work unless I am missing something.

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

Re: NeoPixel fade - Trinket and LEDs

Post by adafruit_support_bill »

To use the code as-is, you need to read through the whole tutorial and understand how it fits into the multitasking model. The code at the end of the tutorial works as-is and includes the fader and other effects. You can use as much or as little as you need. https://learn.adafruit.com/multi-taskin ... ot-dot-dot

The same fade algorithm could be re-worked to function inside a conventional loop as well - without the multitasking capabilities.

User avatar
Sytar
 
Posts: 21
Joined: Sat Aug 08, 2015 12:28 am

Re: NeoPixel fade - Trinket and LEDs

Post by Sytar »

Thanks Bill, I will go back and look through the whole tutorial. I was able to use the same formulas and get an affect that was close to what I wanted, but the color jumps and then fades. I have tried a few ways to make sure that I have all my variables correct. I suppose it could be a side effect of the way the RGB works in the NeoPixels.

What I see is it sets the colors to (for example) Yellow, Green, Blue. The Blue gets randomly selected first, and it will jump from Blue to Orange, and then fade to Red.

Code: Select all

#include <Adafruit_NeoPixel.h>
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
#include <avr/power.h>
#endif

#define PIN 1
#define Pixels 3

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

int fadeRate = 80;
int MaxBrightness = 120;
int TotalSteps = 100;

void setup() {
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  strip.begin();
  for (int i = 0; i < Pixels; i++)
  {
    strip.setPixelColor(i, random(0, MaxBrightness), random(0, MaxBrightness), random(0, MaxBrightness));
  }
  strip.show(); // Initialize all pixels to a Random Color.
}


void loop () {

  int i;

   uint16_t LED = random(Pixels);  // Pick a random pixel
    // Get the colors of the current pixel.
    uint16_t c = strip.getPixelColor(LED);
    uint8_t  blueCur = (uint8_t)(c >> 16);
    uint8_t  redCur = (uint8_t)(c >>  8);
    uint8_t  greenCur = (uint8_t)c;
    //Pick a new random color
    uint8_t redNew = random(0, MaxBrightness);
    uint8_t greenNew = random(0, MaxBrightness);
    uint8_t blueNew = random(0, MaxBrightness);
    // Start fade between the two colors
    for (int i = 1; i < TotalSteps; i++)
    {
      uint8_t red = (((redCur * (TotalSteps - i)) + (redNew * i)) / TotalSteps);
      uint8_t green = (((greenCur * (TotalSteps - i)) + (greenNew * i)) / TotalSteps);
      uint8_t blue = (((blueCur * (TotalSteps - i)) + (blueNew * i)) / TotalSteps);

      // Sets the pixel to the color adjusted in the fade
      strip.setPixelColor(LED, green, red, blue);
      strip.show();
      delay(fadeRate);
    }
  delay(10);
}

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

Re: NeoPixel fade - Trinket and LEDs

Post by adafruit_support_bill »

Your code to extract the RGB from the randomly selected pixel has the colors scrambled.

See the code here:
https://learn.adafruit.com/multi-taskin ... -functions

User avatar
Sytar
 
Posts: 21
Joined: Sat Aug 08, 2015 12:28 am

Re: NeoPixel fade - Trinket and LEDs

Post by Sytar »

I see that now, I adjusted those and also fixed another small issue which was that I was using Uint16 and Uint32, I would only get two of the three colors in the request.

My next problem that I found was that I had my colors mixed up in the set. I was doing Green, Red, Blue. I based this off the setting in the upper part of the code:
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800);
I figured it was needed to do Green first, rather then Red.

After watching the change I found that when it was Green, and started the fade it Jumped to Red then faded to Blue. Because of that I figured it was mixing the two colors and did the set wrong.

Here is what I have now, and it appears to be working!! Thanks for your help Bill, and now I just need to work on the physical part of my project - http://www.thingiverse.com/thing:930147

All parts printing and will adjust sizes for the small Trinket. :)

Code: Select all

#include <Adafruit_NeoPixel.h>
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
#include <avr/power.h>
#endif

#define PIN 1
#define Pixels 3

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

int fadeRate = 80;
int MaxBrightness = 90;
int TotalSteps = 100;

void setup() {
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  strip.begin();
  for (int i = 0; i < Pixels; i++)
  {
    strip.setPixelColor(i, random(0, MaxBrightness), random(0, MaxBrightness), random(0, MaxBrightness));
  }
  strip.show(); // Initialize all pixels to a Random Color.
}


void loop () {

  uint8_t LED = random(Pixels);  // Pick a random pixel
  // Get the colors of the current pixel.
  uint32_t c = strip.getPixelColor(LED);
  uint8_t  redCur = (c >> 16) & 0xFF;
  uint8_t  greenCur = (c >>  8) & 0xFF;
  uint8_t  blueCur = c & 0xFF;
  //Pick a new random color
  uint8_t redNew = random(0, MaxBrightness);
  uint8_t greenNew = random(0, MaxBrightness);
  uint8_t blueNew = random(0, MaxBrightness);
  // Start fade between the two colors
  for (int i = 1; i < TotalSteps; i++)
  {
    uint8_t red = (((redCur * (TotalSteps - i)) + (redNew * i)) / TotalSteps);
    uint8_t green = (((greenCur * (TotalSteps - i)) + (greenNew * i)) / TotalSteps);
    uint8_t blue = (((blueCur * (TotalSteps - i)) + (blueNew * i)) / TotalSteps);
    // Sets the pixel to the color adjusted in the fade
    strip.setPixelColor(LED, red, green, blue);
    strip.show();
    delay(fadeRate);
  }
  delay(10);
}


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

Re: NeoPixel fade - Trinket and LEDs

Post by adafruit_support_bill »

Looks like a nice project. Be sure to post photos when you are done!

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

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