NeoPixel Digital RGB LED Brightest / Fade
Moderators: adafruit_support_bill, adafruit

NeoPixel Digital RGB LED Brightest / Fade

by anakinjay on Wed Jan 16, 2013 11:08 pm

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!!
anakinjay
 
Posts: 4
Joined: Mon Jan 07, 2013 4:28 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Thu Jan 17, 2013 6:52 am

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 | TOGGLE FULL SIZE
  for (int i = 255; i > 0; i--)
  {
      strip.setPixelColor(0, color(i,i,i));
      strip.show();
      delay(10);
  }
User avatar
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by Capeman on Thu Feb 14, 2013 8:10 am

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
Capeman
 
Posts: 1
Joined: Thu Feb 14, 2013 8:00 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Thu Feb 14, 2013 9:57 am

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

Code: Select all | TOGGLE FULL SIZE
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
}
User avatar
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by Snakin on Sun Mar 10, 2013 3:15 pm

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
Snakin
 
Posts: 13
Joined: Wed Feb 27, 2013 1:59 pm
Location: Nu Venus Island.'.

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Sun Mar 10, 2013 4:49 pm

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 | TOGGLE FULL SIZE
void loop()
{
  for (int i = 255; i > 0; i--)
  {
      strip.setPixelColor(0, color(i,i,i));
      strip.show();
      delay(10);
  }
}
User avatar
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by Snakin on Mon Mar 11, 2013 2:47 pm

Thanks so much!.'.
User avatar
Snakin
 
Posts: 13
Joined: Wed Feb 27, 2013 1:59 pm
Location: Nu Venus Island.'.

Re: NeoPixel Digital RGB LED Brightest / Fade

by martinharding on Tue Oct 08, 2013 9:52 pm

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 | TOGGLE FULL SIZE
'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?
martinharding
 
Posts: 1
Joined: Fri Oct 04, 2013 3:14 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Wed Oct 09, 2013 5:38 am

'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
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by gotnull on Sat Oct 04, 2014 11:50 am

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

Code: Select all | TOGGLE FULL SIZE
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?

gotnull
 
Posts: 17
Joined: Sun Oct 21, 2012 6:43 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Sat Oct 04, 2014 12:07 pm

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
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by gotnull on Sun Oct 05, 2014 8:40 am

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 | TOGGLE FULL SIZE
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.

gotnull
 
Posts: 17
Joined: Sun Oct 21, 2012 6:43 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Sun Oct 05, 2014 9:29 am

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
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixel Digital RGB LED Brightest / Fade

by gotnull on Sun Oct 05, 2014 9:42 am

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 | TOGGLE FULL SIZE
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;
}

gotnull
 
Posts: 17
Joined: Sun Oct 21, 2012 6:43 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

by adafruit_support_bill on Sun Oct 05, 2014 10:20 am

What does it do?
Print out the parsed values to make sure you are parsing them correctly.
User avatar
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am