CRGB color as a function argument?

I am working with fastLED.h library and 200 WS2811 LEDs.

I have some functions in my project sketch that are identical except for the color. So I pass the color as a hex value.
For example:

The call:

oneLed(0xFFA07A);

The function:

void oneLed(unsigned int myColor) {
  // Move a single led
  for (int ledNum = 0; ledNum < NUM_LEDS; ledNum = ledNum + 1) {
    leds[ledNum] = myColor;
    FastLED.show();
    delay(pixelDelay);

    // Turn our current led back to black for the next loop around
    leds[ledNum] = CRGB::Black;
  }
}

Is there a way to specify the color as an argument to the function so that I don't have to find the hex value for each color I want to use?

For example, oneLed(LightSalmon);

Yes, the is a list of colour names you can use here.

But you cannot use "unsigned int" as the data type for your function parameter. You must use "CRGB" like this

void oneLed(CRGB myColor) { ... }

Then call it like this:

oneLed(CRGB::LightSalmon);
1 Like

Thanks, Paul. So, in the function parameters, I treat CRGB as the data type? Then why are the double colons required in the call? Why wouldn't oneLed(CRGB LightSalmon); work?

The scope resolution operator confuses me to no end. (And I thought pointers would break me, then you, among others, rescued me from String hell).

1 Like

SteveMann:
Then why are the double colons required in the call?

Because LightSalmon is a value of an enumerated type defined within the CRGB class.

SteveMann:
Why wouldn't oneLed(CRGB LightSalmon); work?

When you call a function (or in this case a class method), you don't/can't specify the data type. You can only define the data type when you define the method.

1 Like

As usual, your responses teach me something.

1 Like