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!!
NeoPixel Digital RGB LED Brightest / Fade
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
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);
}
-
- Posts: 1
- Joined: Thu Feb 14, 2013 8:00 am
Re: NeoPixel Digital RGB LED Brightest / Fade
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
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
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
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
}
-
- Posts: 13
- Joined: Wed Feb 27, 2013 1:59 pm
Re: NeoPixel Digital RGB LED Brightest / Fade
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!.'.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
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:
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);
}
}
-
- Posts: 13
- Joined: Wed Feb 27, 2013 1:59 pm
Re: NeoPixel Digital RGB LED Brightest / Fade
Thanks so much!.'.
- asta
- Posts: 1
- Joined: Fri Oct 04, 2013 3:14 pm
Re: NeoPixel Digital RGB LED Brightest / Fade
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:
Kind of rusty with Arduino, any ideas?
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
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
Try "strip.Color()" In the current library, 'Color()' is a member function of the strip class.'color' was not defined in this scope
- gotnull
- Posts: 32
- Joined: Sun Oct 21, 2012 6:43 pm
Re: NeoPixel Digital RGB LED Brightest / Fade
Thank you so much for this code.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 }
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?
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
Rstart is the red value you want to start with (255)Let's say I wanted to fade from 255, 0, 0 (red) to 0, 255, 0 (green) for example.
Rend is the red value you want to end with (0)
Same for the green (0 and 255) and blue (0 and 0)
- gotnull
- Posts: 32
- Joined: Sun Oct 21, 2012 6:43 pm
Re: NeoPixel Digital RGB LED Brightest / Fade
Let's say I have the following function where command is equal to an RGB color (e. g. 255, 0, 0):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)
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;
}
Keep in mind I have a total of 72 LED's that I need to light up at any given time as well.
Thanks.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
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.
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.
- gotnull
- Posts: 32
- Joined: Sun Oct 21, 2012 6:43 pm
Re: NeoPixel Digital RGB LED Brightest / Fade
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.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.
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;
}
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: NeoPixel Digital RGB LED Brightest / Fade
What does it do?
Print out the parsed values to make sure you are parsing them correctly.
Print out the parsed values to make sure you are parsing them correctly.
Please be positive and constructive with your questions and comments.