Noob here and playing with the cylon demo in the FastLED library.

Noob here and playing with the cylon demo in the FastLED library. I’m trying to add a variable that lets me set a particular color rather than going through the rainbow (trying to make something for Mardi Gras with just Purple, Green, and Gold).
Calling the function “colorstreak” it looked like this with the idea that I could set a Hex color in the variable “streakcolorfor” my purple green and gold:
void colorstreak( byte streakcolor) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = streakcolor;
FastLED.show();
fadeall();
delay(10);
}
for (int i = (NUM_LEDS) - 1; i >= 0; i–) {
leds[i] = streakcolor;
FastLED.show();
fadeall();
delay(10);
}
but this doesn’t work. I’ve tried byte, int, uint8_t, uint16_t and they all end up with weird colors. If I don’t try to define the function with a variable and just put the code in the main loop with my colors(for example purple 0x8010ff) then it shows up fine.

I also tried with RGB values broken up (with both byte, and int)
void colorstreak( byte red, bye green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = red, green, blue;
FastLED.show();
fadeall();
delay(10);
}
and I would still get funky colors. Again losing the function and just defining the pixels with RGB 128,16,255 in the main loop worked fine.

I’m sure it’s something as simple as using the proper variable type, but I’ve been at it for a while and getting frustrated. Thanks in advance for any help.

Do you want it to have the same effect as the cylon example? Also, I am not really sure what streakcolor is. “leds[i] = streakcolor;” It does not look like a color is being selected.

Yeah–same thing as cylon but be able to assign a color variable. “streakcolor” is just the name of that variable I’m trying assign, so I can put in the main loop:
streakcolor(0x8010ff);
streakcolor(0x2cb004);
streakcolor(0xff4a00);
to make it just cycle through just the approximations I’ve come up with for Mardi Gras purple, green, and gold colored cylon effects. Right now this works, but the colors are bizarre which is why I suspect the full hex color info isn’t being passed in the variable. Again, I just don’t want it to go through the entire rainbow which the example code does.
Thanks.

Look at the demo100 example. That shows a lot of ways to use the fastled library.

Just checking, have you confirmed the correct color order for your LED strip?

Yes, thanks Marc. Was my first thought. But if I explicity state the color code with leds[i] = 0x8010ff rather than trying to use a variable it shows me the colors I’m expecting. That’s why I still think the value of my variable is being truncated or changed because I’m using the wrong kind.

Thanks Dushyant. I have been going through all the examples (pretty much how I’m learning all this on the fly) and haven’t come across this issue or an example of how it can be done. Appreciate the thought though.

Can you share your code on http://gist.github.com ?

Done Marc:

(Here’s the full thing too:)
#include <FastLED.h>
#define NUM_LEDS 80
#define DATA_PIN 6
#define BRIGHTNESS 80
#define MGPu 0x8010ff // Mardi Gras Purple
#define MGGr 0x2cb004 // Green
#define MGGo 0xff4a00 // Gold
CRGB leds[NUM_LEDS]; //creates RGB array called ‘leds’ of NUM_LEDS in size

void setup() {
// put your setup code here, to run once:
delay(1000); //power on safety delay
FastLED.setBrightness(BRIGHTNESS);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

}

void fadeall() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(250);
}
}
void cylon(byte streakcolor) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = streakcolor;
FastLED.show();
fadeall();
delay(10);
}
for (int i = (NUM_LEDS) - 1; i >= 0; i-) {
leds[i] = streakcolor;
FastLED.show();
fadeall();
delay(10);
}
}
void loop() {
cylon(0x8010ff); //Purple
cylon(0x2cb004); //Green
cylon(0xff4a00); //Gold
//above code produces very strange colors. I tried defining “cylon(byte red, byte green, byte blue)” then calling with the respect RBG colors
//from a conversion program, still odd.
//If I just write it all out in long hand declaring each individual color, then it shows the colors I want:
/
void loop() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = MGPu;
FastLED.show();
fadeall();
delay(10);
}
for (int i = (NUM_LEDS) - 1; i >= 0; i-) {
leds[i] = MGPu;
FastLED.show();
fadeall();
delay(10);
}

for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = MGGr;
FastLED.show();
fadeall();
delay(10);
}
for (int i = (NUM_LEDS) - 1; i >= 0; i-) {
leds[i] = MGGr;
FastLED.show();
fadeall();
delay(10);
}
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = MGGo;
FastLED.show();
fadeall();
delay(10);
}
for (int i = (NUM_LEDS) - 1; i >= 0; i-) {
leds[i] = MGGo;
FastLED.show();
fadeall();
delay(10);
}
}
/

Woops–wrong file: the strip parameters is actually:
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);

You can see how badly G+ mangles code pasted here. Don’t do it! :slight_smile:

Your code was close. When defining the color make it a CRGB color. Here’s a fixed working example.

@marmil YES! Thank you. I knew it was something trivially simple with the variable (at least to someone with experience) but was driving me crazy. Works perfectly now, thanks a ton.
Damn variables…