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.
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:What does it do?
Print out the parsed values to make sure you are parsing them correctly.

Code: Select all

/*******************************************************************************
 * Function Name  : tinkerSetColour
 * Description    : Sets the strip with the appropriate colour
 * Input          : Pin and value
 * Output         : None.
 * Return         : 1 on success and a negative number on failure
 *******************************************************************************/
int Rstart = 0, Gstart = 0, Bstart = 0;
int Rnew = 0, Gnew = 0, Bnew = 0;

int tinkerSetColour(String command)
{
    sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
    Spark.publish("rgb", rgbString);

    sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
    Spark.publish("rgb", rgbString);

    // 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;

    sprintf(rgbString, "Rend %i, Gend %i, Bend %i", Rend, Gend, Bend);
    Spark.publish("rgb", rgbString);

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

    sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
    Spark.publish("rgb", rgbString);
    
    Rstart = red, Gstart = grn, Bstart = blu;

    sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
    Spark.publish("rgb", rgbString);
    
    return 1;
}
Here's the output selecting RED to begin with:

Code: Select all

Rstart 0, Gstart 0, Bstart 0
Rnew 0, Gnew 0, Bnew 0
Rend 255, Gend 0, Bend 0
Rnew 253, Gnew 0, Bnew 0
Here's the output selecting GREEN directly afterwards:

Code: Select all

Rstart 255, Gstart 0, Bstart 0
Rnew 253, Gnew 0, Bnew 0
Rend 0, Gend 255, Bend 0
Rnew 2, Gnew 253, Bnew 0
And then the output selecting BLUE after that:

Code: Select all

Rstart 0, Gstart 255, Bstart 0
Rnew 2, Gnew 253, Bnew 0
Rend 0, Gend 23, Bend 255
Rnew 0, Gnew 25, Bnew 253

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

You are never setting the color to the end color. You either need to do that explicitly, or change your loop so that it doesn't stop short of the end:

Code: Select all

for (int i = 0; i <= n; i++)

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:You are never setting the color to the end color. You either need to do that explicitly, or change your loop so that it doesn't stop short of the end:

Code: Select all

for (int i = 0; i <= n; i++)
I had already added the <= hoping that was the issue but it still doesn't fade from color to color.

Where would I set the color to the end color?

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

After the loop:

Code: Select all

        strip.setPixelColor(0, strip.Color(Rend, Gendw, Bend));
But if you do the math, it should be the same as using '<=' in your loop.

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by gotnull »

I've modified my code to be the following:

Code: Select all

MotiColor startColor;
MotiColor endColor;

void setup()
{
    // Begin strip.
    strip.begin();

    // Initialize all pixels to 'off'.
    strip.show();

    Serial1.begin(9600);

    startColor = MotiColor(0, 0, 0);
    endColor = MotiColor(0, 0, 0);
}

void loop () {
}

int tinkerSetColour(String command)
{
    strip.show();

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

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

    startColor = MotiColor(red.toInt(), grn.toInt(), blu.toInt());

    int16_t redDiff = endColor.getR() - startColor.getR();
    int16_t greenDiff = endColor.getG() - startColor.getG();
    int16_t blueDiff = endColor.getB() - startColor.getB();

    int16_t _delay = 500;
    int16_t duration = 3500;
    int16_t steps = duration / _delay;

    int16_t redValue, greenValue, blueValue;

    for (int16_t i = steps; i >= 0; i--) {
        redValue = (int16_t)startColor.getR() + (redDiff * i / steps);
        greenValue = (int16_t)startColor.getG() + (greenDiff * i / steps);
        blueValue = (int16_t)startColor.getB() + (blueDiff * i / steps);

        sprintf(rgbString, "%i,%i,%i", redValue, greenValue, blueValue);
        Spark.publish("rgb", rgbString);

        for (uint16_t i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, strip.Color(redValue, greenValue, blueValue));
        }
        
        delay(_delay);
    }

    delay(_delay);

    for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(endColor.getR(), endColor.getG(), endColor.getB()));
    }

    delay(_delay);

    endColor = MotiColor(startColor.getR(), startColor.getG(), startColor.getB());

    return 1;
}
I am seeing the published results correctly:

This is from OFF (0,0,0) -> RED (255,0,0) -> GREEN (0,255,0).

Image

It works fine when I publish the results back to the console via the

Code: Select all

Spark.publish();
event, however the actual Neopixel LED's don't fade from colour to colour as expected. They just change from colour to colour instead of fading.

I'm just wondering where I'm going wrong or how I can improve my code so that I actually see the fading in real time.
Last edited by gotnull on Wed Oct 15, 2014 10:15 pm, edited 1 time in total.

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

What library are you using to control the neopixels? Are you running on a Spark Core?

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:What library are you using to control the neopixels? Are you running on a Spark Core?
I'm using the following library and yes it's running on a Spark Core:

https://github.com/technobly/SparkCore-NeoPixel

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

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_bill »

I'd check with the author of the library. From the data you posted, the color should have faded over a period of 3 seconds for each transition. The only caveat is that you only have about 8 steps per fade. I'd increase the number of steps to be sure that the non-linearity of the led response is responsible for the abrupt transition.

User avatar
gberm
 
Posts: 3
Joined: Mon May 04, 2015 9:08 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by gberm »

Can anyone elaborate on

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
}
I am trying to get this into a sketch but I know I am missing something in order to make it work.

help please!!!!!

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_mike »

The code is attempting to fade between two levels by taking N equal-size steps from one to the other.

The values in play for the red channel are:

- Rstart: the starting value
- Rend: the ending value
- (Rend - Rstart): the distance between the two
- (Rend - Rstart) / n: the size of a single step
- (Rend - Rstart) * i / n: the size of 'i' steps
- Rstart + (Rend - Rstart) * i / n: the starting value plus 'i' steps

That isn't written very well though, because it doesn't make the order of operations clear. A better version would be:

- Rstart + (( Rend - Rstart ) * ( i / n ));

User avatar
gberm
 
Posts: 3
Joined: Mon May 04, 2015 9:08 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by gberm »

So do all of the Rnew, Rstart, Rend etc need to be assigned integer values? If so what values should they be set for?

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by adafruit_support_mike »

Rstart and Rend need to be integer values between 0 and 255. The rest take care of themselves.

User avatar
Trashdude
 
Posts: 4
Joined: Sun Jan 10, 2016 6:59 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by Trashdude »

Does anybody actually have a start to finish WORKING code for fading neo pixels I can copy paste? I have tried to copy
/paste just about everything on this page and still get errors...I have my neo pixels working just fine with the stringtest code and simple code supplied by the Adafruit website...some of the language is a bit over my head on this particular thread....sorry newbie here...

User avatar
sebsebseb
 
Posts: 8
Joined: Tue Feb 23, 2016 1:01 am

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by sebsebseb »

Can someone please post a complete code of this?

cheers.

User avatar
Sparkyduke
 
Posts: 1
Joined: Wed Oct 12, 2016 2:33 pm

Re: NeoPixel Digital RGB LED Brightest / Fade

Post by Sparkyduke »

Bringing up an old post but having a lot of issues trying to fade a random led in and a random led out.
having a lot of issues selecting a random led and fading it

please help
Attachments
City_north_one_button.zip
(1.75 KiB) Downloaded 21 times

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

Return to “General Project help”