Setting const values for uint32_t colors

I will have multiple strands of LED's and each will be one out of a set of colors. I want to define specific vales as colors like RED, GREEN, BLUE etc without having to type Color(255,0,0) each time. My current code is an edited version of the NeoPixel strand test.

#include <Adafruit_NeoPixel.h>
// Dropping this, changes the color of later LED's
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define REAR_LED_PIN    2

// How many NeoPixels are attached to the Arduino?
#define REAR_LED_COUNT 24

// Colors


// Declare our NeoPixel rear_strip object:
Adafruit_NeoPixel rear_strip(REAR_LED_COUNT, REAR_LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  rear_strip.begin();           // INITIALIZE NeoPixel rear_strip object (REQUIRED)
  rear_strip.show();            // Turn OFF all pixels ASAP
  rear_strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)
  setColor(rear_strip.Color(255,   0,   0)); // Red

}

void loop() {

}

void setColor(uint32_t color) {
  for(int i=0; i<rear_strip.numPixels(); i++) { // For each pixel in rear_strip...
    rear_strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    rear_strip.show();                          //  Update rear_strip to match
  }
}

Many ways to do it. One is

#define cyan (0,255,255)
[...]
color(cyan);

aarg:
Many ways to do it. One is

#define cyan (0,255,255)

[...]
color(cyan);

So this is interesting. When I define RED

#define RED (255,0,0)

I get nothing here
rear_strip.setPixelColor(i, RED);

When I change the value of "RED"
#define RED (0,255,255)

I get blue. Am I missing something here? I am attaching my updated code to this message.

#include <Adafruit_NeoPixel.h>
// Dropping this, changes the color of later LED's
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

// Which pin on the Arduino is connected to the NeoPixels?
#define REAR_LED_PIN    2

// How many NeoPixels are attached to the Arduino?
#define REAR_LED_COUNT 24

// Colors
#define RED (0,255,255)


// Declare our NeoPixel rear_strip object:
Adafruit_NeoPixel rear_strip(REAR_LED_COUNT, REAR_LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.

  rear_strip.begin();           // INITIALIZE NeoPixel rear_strip object (REQUIRED)
  rear_strip.show();            // Turn OFF all pixels ASAP
  rear_strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)
  setColor(rear_strip.Color(255,0,0)); // Red

}

void loop() {

}

void setColor(uint32_t color) {
  for(int i=0; i<rear_strip.numPixels(); i++) { // For each pixel in rear_strip...
    rear_strip.setPixelColor(i, RED);         //  Set pixel's color (in RAM)
    rear_strip.show();                          //  Update rear_strip to match
  }
}

This is one of the many reasons why using macros for these kinds of things is a terrible idea.

Just use a constant instead:

const auto red = Adafruit_NeoPixel::Color(255,   0,   0);

// ... 

setColor(red);

The reason why your previous code didn't work is because it used the C++ comma operator. The first two values are discarded, and the last one is passed to the setPixelColor function.

setPixelColor(i, (255, 0, 0)); // Wrong! equivalent to setPixelColor(i, 0x000000) i.e. black
setPixelColor(i, (0, 255, 255)); // Wrong! equivalent to setPixelColor(i, 0x0000FF) i.e. blue

Pieter

PieterP:
This is one of the many reasons why using macros for these kinds of things is a terrible idea.

Just use a constant instead:

const auto red = Adafruit_NeoPixel::Color(255,   0,   0);

// ...

setColor(red);




The reason why your previous code didn't work is because it used the C++ comma operator. The first two values are discarded, and the last one is passed to the setPixelColor function.



setPixelColor(i, (255, 0, 0)); // Wrong! equivalent to setPixelColor(i, 0x000000) i.e. black
setPixelColor(i, (0, 255, 255)); // Wrong! equivalent to setPixelColor(i, 0x0000FF) i.e. blue




Pieter

Thank you. This is exactly it. Working perfectly now.

Thank you!

If you'd like a bunch (140, actually) of colors that are already defined, you could look here:
https://forum.arduino.cc/index.php?topic=657728.0