I have been trying very hard to use the blend function but still cant

I have been trying very hard to use the blend function but still cant figure it out. Thanks to Andrew for helping me with this. He wrote me a function using beatsin8(). The problem is I can NOT figure out how this works. Here is the function in the code…

uint8_t speed = beatsin8(6,0,255);
// endclr = blend(CHSV( HUE_PURPLE, 255, 255), CHSV( HUE_GREEN, 255, 255), speed);
// midclr = blend(CHSV( HUE_GREEN, 255, 255), CHSV( HUE_PURPLE, 255, 255), speed);
// cycle();
// FastLED.show();

Could someone please re-write this using a FOR or WHILE loop so I can see how to use the BLEND function? My coding skills are pretty weak and I could use a hand here… THANKS GUYS!

“Speed” is sort of the wrong name for that variable.
Try renaming it “blendAmount” and see if that helps make the code more comprehensible?

Umm, I still dont get it…How would I control the speed of the blending, (like using delay() in a for loop) If I saw the function used in a for or while loop I think I could get some traction on this thing. Thank you VERY much for responding! I am determined to get this to work, but my I am new to coding.

Ah! Yes! This has to be in a loop!

What specifically are you trying to make it look like?

I want to fade between two colors over time…I want to fade between the colors NOT using the color wheel, but just a “straight” fade. I would like to be able to control the speed of the fade using a POT.

Got it, I think.
And you want all the pixels to be the same color? Or is that somethings else?

Well, I ideally I would like this whole blending pattern to scroll slowly down the length of the stip. One end of the strip would be color A and the other end color B. So A would end up replacing B in the strip. I dont really know what the blending function is capable of doing, but I think something like this would be nice. (All using FOR or WHILE loops)… Gee, I dont ask for much do I ?? LOL …Seriously Thank you very much!!!

I Still cant get this to work.

Can anybody else help me figure out the BLEND function in a FOR NEXT or WHILE LOOP format? ANY kind of example would help.

I’ll have full keyboard tomorrow morning and I’ll post example then.

@Mark_Kriegsman OH MAN, Thank you SO much!!! I really appriciate your help. I know you guys are working VERY hard on this lib. you are a credit to the programming community!!!

So I’m looking at this description, and I’m finding myself still unsure of the exact effect you’re looking for. In particular: You said you wanted “to fade between two colors over time” (emphasis mine), but then you also said you wanted the “whole blending pattern to scroll” (again emphasis mine). The first of those describes a change over time, but the second describes a change over space.

I’ll just paste some code and you can play with it!

So first of all, if you want to fill a range of pixels with a smooth gradient of color, you can do this:

CRGB startColor( CRGB::Red);
CRGB endColor( CRGB::Blue);

int startPos = 0; // start gradient at first pixel
int endPos = NUM_LEDS-1; // end gradient at last pixel

fill_gradient_RGB( leds, startPos, startColor, endPos, endColor);
FastLED.show();

And poof: a smooth filled gradient blending from Red to Blue across all the LEDs.

But let’s do it explicitly ourselves with a loop. This is basically what fill_gradient does ( except that fill_gradient does it without using floating point numbers, which aren’t as fast as integers ) …

// Loop over every pixel
for( int i = 0; i < NUM_LEDS; i++) {

// First, figure out what 'percentage' of the way along
// the strip we are at this particular pixel:
float fractionOfTheWayAlongTheStrip = (float)i / (float)(NUM_LEDS-1);

// Now calculate how much of each color we should blend together
// at this particular point along the strip.
// 0 = pure 'start color', 255 = pure 'end color'
uint8_t amountOfBlending = fractionOfTheWayAlongTheStrip * 255;

// Mix up a new color, which is a blend of the start and end colors
// Use the blend function to get the right mix for this particular pixel
CRGB pixelColor = blend( startColor, endColor, amountOfBlending);

// set this pixel to the blended color:
leds[i] = pixelColor;

}
FastLED.show();

Now you also talked about moving a pattern down the row of pixels over time. I think one relatively easy way to do that is (1) first define a color palette that has the colors you want in it, and (2) fill the led array with colors from the palette. That looks like this ( no animation yet ) :

// Define a color palette pre-filled with a gradient
// that goes from startColor, to endColor (in the middle),
// and back to startColor.
CRGBPalette16 myPalette( startColor, endColor, startColor);

// Start with the color at the beginning of the palette,
// and choose colors from along the palette moving by a few
// palette slots per pixel.
uint8_t startIndex = 0;
uint8_t incrementIndex = 128 / NUM_LEDS;

fill_palette( leds, NUM_LEDS,
startIndex, incrementIndex,
myPalette,
255, //full brightness
LINEARBLEND);

FastLED.show();

Now to animate it, you’d change it so that the first color selected from the palette shifted over time:

// Define a color palette pre-filled with a gradient
// that goes from startColor, to endColor (in the middle),
// and back to startColor.
CRGBPalette16 myPalette( startColor, endColor, startColor);

// Start with the color at the beginning of the palette,
// and choose colors from along the palette moving by a few
// palette slots per pixel.
static uint8_t startIndex = 0;
startIndex += 1; // start further down the color palette each time.

uint8_t incrementIndex = 128 / NUM_LEDS;

fill_palette( leds, NUM_LEDS,
startIndex, incrementIndex,
myPalette,
255, //full brightness
LINEARBLEND);

FastLED.show();

Now only one of these uses a ‘for’ loop, so you may want to play around and see what you can do, and then ask some more questions. I’m curious to see what you come up with these as some building blocks. Feel free to post some of your code, if it’ll help!

Yes, Im sure it will help, I will get busy now and see what I can come up with…THANKS AGAIN!!!
The only question I still have is how to change the code from using …
CRGB startColor( CRGB::RED); ,
to using CHSV colors…I find that the colors labeled CRGB::RED, CRGB::BLACK, etc, have a pale look to them and are not as saturated as you can get with the CHSV colors stuff…I think I can figure it out though, If not I will write you back. OH, almost forgot…You wrote this… “This is basically what fill_gradient does ( except that fill_gradient does it without using floating point numbers, which aren’t as fast as integers )” …Did you mean it DOES use floating point numbers? Because in the code it uses floating point stuff. OK, I think thats it for now. :slight_smile:

You can always assign an HSV color into an RGB variable and it will automatically convert:

CRGB rgbColor = CHSV( HUE_ORANGE, 175, 255);
// now rgbColor is set to a light orange

As for floating point, basically none of the FastLED pixel or color functions use floating point math. They all use fixed point math (aka integers of some sort), which is much faster and smaller – and sometimes harder to read… or write…

So: the library uses fixed point for pretty much everything, but one of the examples I wrote above uses floats, because it helps keep the issues simpler.