Usage of CRGB::Black

Hi I posted earlier regarding some code that would not work. I have continued to try and understand the issue and the more i dig and tweak the code to understand the issue, the more I think i have isolated it around the checking to see if an LED is lit, by using the code if(leds[5]=CRGB::Black)

I have posted two almost identical sketches below. The first sketch sets led number 5 to blue in void setup and then in void loop it checks to see if that led is blue and if it is it turns it green. This sketch works as expected.

The second sketch set led number 5 to Black in void setup (this turns it off as expected ) and then in void loop if checks to see if the same led is black and if it is, it turns it Green.

This second sketch does not work…the led is turned off (if it was previously on) but doesn’t turn green.

Could someone enlighten me???

Sketch 1:

#include <FastLED.h>

#define NUM_LEDS 300
#define DATA_PIN 3
CRGB leds[NUM_LEDS];



void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  leds[5] = CRGB::Blue; //set LED 5 to colour to check for to ensure it is not in some unknow colour
  FastLED.show();       //Light LED
  delay(5000); //pause to see color
}

void loop() {
  if (leds[5] = CRGB::Blue) { //if Blue
    leds[5] = CRGB::Green;    //Change to Green
    FastLED.show();
  }


}

Sketch2:

#include <FastLED.h>

#define NUM_LEDS 300
#define DATA_PIN 3
CRGB leds[NUM_LEDS];



void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  leds[5] = CRGB::Black; //set LED 5 to colour to check for to ensure it is not in some unknow colour
  FastLED.show();       //Light LED
  delay(5000); //pause to see color
}

void loop() {
  if (leds[5] = CRGB::Black) { //if Black
    leds[5] = CRGB::Green;    //Change to Green
    FastLED.show();
  }


}

I hope this makes sense and thanks for any help

= is not the same as ==

i did think that and tried ==...and i got the compiler error

exit status 1 ambiguous overload for 'operator==' (operand types are 'CRGB' and 'CRGB::HTMLColorCode')

also this wouldn't explain why it works with blue...

== is used when making comparison

sorted it now by using this

if (leds[5] == CRGB(0x000000) )

works fine but still stand that =Blue works and =Black doesn't but thanks for pointing me in the right direction

I think you are overlooking something.

All you have to do is take the sketch that works and change the two values and upload.

ieee48...yes i tried that...no joy. There must be something odd about Black.

There must be something odd about Black

Black is RGB 0 value. So x=Black will evaluate to false always.

Blue is non-zero, so x=Blue will always evaluate as true (as it is not = zero).

As for why == does not work, that is a different question. Could simple be a bug in the library - I teried it with WS2182B hardware and got the same error.

Did some more digging. Mystery solved.

The overloaded == operator is defined in pixeltypes.h as

inline __attribute__((always_inline)) bool operator== (const CRGB& lhs, const CRGB& rhs)
{
    return (lhs.r == rhs.r) && (lhs.g == rhs.g) && (lhs.b == rhs.b);
}

This means that it cannot work with a constant on the RHS of the operator. Changing the code to the code below works for Blue and Black with no errors.

#include <FastLED.h>

#define NUM_LEDS 30
#define DATA_PIN 3
CRGB leds[NUM_LEDS];

const CRGB TEST_COLOR = CRGB::Black;

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  leds[5] = TEST_COLOR; //set LED 5 to colour to check for to ensure it is not in some unknow colour
  FastLED.show();       //Light LED
  delay(5000); //pause to see color
}

void loop() {
  if (leds[5] == TEST_COLOR) 
  { //if matches
    leds[5] = CRGB::Green;    //Change to Green
    FastLED.show();
  }
}