StatusLED function (FastLED)

Hi all,

In a project i'd like to be able to change the color of a (WS2812B) 'status LED' easily.
In other words, i'm using a (single) ws2812B to show the current status of my code, i.e.:
Connecting (Orange)
Running (Green)
Error (Red)

As the status will change throughout several parts of my main code, I'd like to make a simple function to easily make a call to change the color of the LED.
Also, I'd like to define the exact colors in the header of my code to make it easy to adapt (or add) colors later on.

So, I was thinking of doing something like this:

In the header define:

int ConnectingColor[3] {255, 105, 0}; //Orange color in RGB format
int RunningColor[3] {0, 255, 0};   //Green color in RGB format
int ErrorColor[3] {255, 0, 0};  //Red color in RGB format

Then, write some function to do the following:

void ChangeColor(MachineStatus){
    switch (MachineStatus){
      case Connecting: 
        leds[0] = ConnectingColor; //Make status LED orange
      break;
      //
      case Running: 
        leds[0] = RunningColor; //Make status LED green
      break;
      //
      case Error: 
        leds[0] = ErrorColor; //Make status LED red
      break;
    }
      FastLED.show();
 }

Then, within my main code I'd like to be able to call the function like this:

ChangeColor(Error)

To make the LED change to the appropriate color.

First questions:

  1. How to define the colors in the header? Did i do it the right way?
  2. How to declare the variable used as argument for the 'ChangeColor' function (the 'MachineStatus' argument), as this will be used as some kind of word/keyword.
  3. How to use the color definitions in the header to actually change the color (with fastled, default is something like: 'CRGB::Red', but now I'd like to use the personally defined RGB mix.)

Thanks in advance

FastLED has a method to set the color array that you declare.
All you have to do is supply the correct color values as arguments to that method.
With only three colors there is no reason to mess with arrays.

You can use a uint32_t rather than an array of 'int's:

const uint32_t ConnectingColor = 0x00FF6900;

and assign it thusly:

led[0] = ConnectingColor;

You don't want to define variables in a header file because you'll get multiple definition errors should that .h file get included in more than one .cpp (or .ino) file.

You could also make an array of uint32_t colors and index it by the current machine state.

This page provides some aliases that FastLED already defined for colors. Go all the way down the end.