NeoPixel Digital RGB LED Brightest / Fade

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
anakinjay
 
Posts: 4
Joined: Mon Jan 07, 2013 4:28 pm

NeoPixel Digital RGB LED Brightest / Fade

Post by anakinjay »

Just got my led strip (http://adafruit.com/products/1138) , and love it... hooked everything up to my arduino mega using the library at https://github.com/adafruit/Adafruit_NeoPixel

I can't figure out how to fade the brightness of leds though, only changing the color at full brightness.

I'd like to create a fading effect where the led fades to off, then slowly fades to full brightness, then back.... Can anyone help me out with this? Thanks in advance!!

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

To fade, just decrease the value of the R, G and B values. For example: This should fade pixel 0 from white to black.

Code: Select all

  for (int i = 255; i > 0; i--)
  {
      strip.setPixelColor(0, color(i,i,i));
      strip.show();
      delay(10);
  }

Capeman
 
Posts: 1
Joined: Thu Feb 14, 2013 8:00 am

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by Capeman »

Hi there, how can i make the strip fade from one color into another?

I know there is the color wheel, but i cant figure out how to set it up to just fade from red to blue!

Andy

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

This is the formula. There is room for optimization if you are looking for performance.

Code: Select all

for(int i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition.
{
   Rnew = Rstart + (Rend - Rstart) * i / n;
   Gnew = Gstart + (Gend - Gstart) * i / n;
   Bnew = Bstart + (Bend - Bstart) * i / n;
 // set pixel color here
}

Snakin
 
Posts: 13
Joined: Wed Feb 27, 2013 1:59 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by Snakin »

Hi folks. The initial example above makes sense in of itself, but I am a completely new Arduino user, so please tell me what goes around the code. I can't seem to paste it into any of the example sketches without getting a compile error. Much appreciated!.'.

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

If you download the neopixel library: https://github.com/adafruit/Adafruit_NeoPixel
and open one of the examples, it will have all the basic code structure you need. What is inside the "loop()" function is what you can modify to alter the patterns. To run the example above, just clean out everything in the loop function and drop it in:

Code: Select all

void loop()
{
  for (int i = 255; i > 0; i--)
  {
      strip.setPixelColor(0, color(i,i,i));
      strip.show();
      delay(10);
  }
}

Snakin
 
Posts: 13
Joined: Wed Feb 27, 2013 1:59 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by Snakin »

Thanks so much!.'.

User avatar
asta
 
Posts: 1
Joined: Fri Oct 04, 2013 3:14 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by asta »

I'm trying to achieve a similar effect (fade from white to dark), but having trouble with these examples. Notably the last one where it's suggested to replace everything in void loop in one of the example sketches included with the NeoPixel library (I'm using the strandtest example, which works beautifully when unedited) throws up this error with the code above:

Code: Select all

'color' was not defined in this scope
strandtest.pde: In function 'void loop()':
strandtest:24: error: 'color' was not declared in this scope
Kind of rusty with Arduino, any ideas?

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

'color' was not defined in this scope
Try "strip.Color()" In the current library, 'Color()' is a member function of the strip class.

User avatar
gotnull
 
Posts: 32
Joined: Sun Oct 21, 2012 6:43 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by gotnull »

adafruit_support_bill wrote:This is the formula. There is room for optimization if you are looking for performance.

Code: Select all

for(int i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition.
{
   Rnew = Rstart + (Rend - Rstart) * i / n;
   Gnew = Gstart + (Gend - Gstart) * i / n;
   Bnew = Bstart + (Bend - Bstart) * i / n;
 // set pixel color here
}
Thank you so much for this code.

Are you able to elaborate a little bit more on what the R variables are?

Let's say I wanted to fade from 255, 0, 0 (red) to 0, 255, 0 (green) for example.

How would I go about using these values and that formula?

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

Let's say I wanted to fade from 255, 0, 0 (red) to 0, 255, 0 (green) for example.
Rstart is the red value you want to start with (255)
Rend is the red value you want to end with (0)

Same for the green (0 and 255) and blue (0 and 0)

User avatar
gotnull
 
Posts: 32
Joined: Sun Oct 21, 2012 6:43 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by gotnull »

adafruit_support_bill wrote: Rstart is the red value you want to start with (255)
Rend is the red value you want to end with (0)

Same for the green (0 and 255) and blue (0 and 0)
Let's say I have the following function where command is equal to an RGB color (e. g. 255, 0, 0):

Code: Select all

int setColour(String command)
{
    // Clear strip.
    strip.show();

    int commaIndex = command.indexOf(',');
    int secondCommaIndex = command.indexOf(',', commaIndex+1);
    int lastCommaIndex = command.lastIndexOf(',');

    int red = command.substring(0, commaIndex).toInt();
    int grn = command.substring(commaIndex+1, secondCommaIndex).toInt();
    int blu = command.substring(lastCommaIndex+1).toInt();

    int Rstart = ?, Gstart = ?, Bstart = ?;
    int Rend = ?, Gend = ?, Bend = ?;
    int Rnew = ?, Gnew = ?, Bnew = ?;
    int n = 200;

    for (int i = 0; i < n; i++) // larger values of 'n' will give a smoother/slower transition.
    {
        Rnew = Rstart + (Rend - Rstart) * i / n;
        Gnew = Gstart + (Gend - Gstart) * i / n;
        Bnew = Bstart + (Bend - Bstart) * i / n;
        
        // Set pixel color here.
        strip.setPixelColor(0, strip.Color(Rnew, Gnew, Bnew));

        return 1;
    }
    return -2;
}
How would I go about ensuring that any RGB color fades from old to new. Am I on the right track with the above code? Would I have to store the old value somewhere so the algorithm knows what to fade to? I'm trying to better understand how that loop works from color to color.

Keep in mind I have a total of 72 LED's that I need to light up at any given time as well.

Thanks.

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

Any reason why you are passing the command as a string? If you pass it as 3 integers Rend, Gend & Bend, you can eliminate the parsing step.
Also, your function does not take a pixel number as a parameter. It only works on pixel #0.
To find the current color of any pixel, you can call getPixelColor(n) where 'n' is the pixel number.
The start numbers for Rstart, Gstart & Gstart would be whatever the current pixel color is. The end numbers would be whatever you pass in.

User avatar
gotnull
 
Posts: 32
Joined: Sun Oct 21, 2012 6:43 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by gotnull »

adafruit_support_bill wrote:Any reason why you are passing the command as a string? If you pass it as 3 integers Rend, Gend & Bend, you can eliminate the parsing step.
Also, your function does not take a pixel number as a parameter. It only works on pixel #0.
To find the current color of any pixel, you can call getPixelColor(n) where 'n' is the pixel number.
The start numbers for Rstart, Gstart & Gstart would be whatever the current pixel color is. The end numbers would be whatever you pass in.
The reason why I'm passing the command as a string is because that's how I first implemented it. I'll change it once I get this function working the way I want it.

I still can't seem to get it working. This is what I've currently got:

Code: Select all

int Rstart = 0, Gstart = 0, Bstart = 0;
int Rnew = 0, Gnew = 0, Bnew = 0;
int tinkerSetColour(String command)
{
    // Clear strip.
    strip.show();

    int commaIndex = command.indexOf(',');
    int secondCommaIndex = command.indexOf(',', commaIndex+1);
    int lastCommaIndex = command.lastIndexOf(',');

    int red = command.substring(0, commaIndex).toInt();
    int grn = command.substring(commaIndex+1, secondCommaIndex).toInt();
    int blu = command.substring(lastCommaIndex+1).toInt();

    int Rend = red, Gend = grn, Bend = blu;

    // Larger values of 'n' will give a smoother/slower transition.
    int n = 200;
    for (int i = 0; i < n; i++)
    {
        Rnew = Rstart + (Rend - Rstart) * i / n;
        Gnew = Gstart + (Gend - Gstart) * i / n;
        Bnew = Bstart + (Bend - Bstart) * i / n;
        
        // Set pixel color here.
        strip.setPixelColor(0, strip.Color(Rnew, Gnew, Bnew));
    }
    
    Rstart = red, Gstart = grn, Bstart = blu;
    
    return 1;
}

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

What does it do?
Print out the parsed values to make sure you are parsing them correctly.

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

Return to “General Project help”