Hey Arduino community,
I’m working on a project with RGB leds and the adafruit neopixel library.
strip.setPixelColor(0, 255, 0, 0); //GREEN
strip.show(); //.
I'm searching for a way to store the colour values in a variable.
something like this:
*variable* COLOUR = 0, 255, 0, 0
strip.setPixelColor(COLOUR);
strip.show(); //.
Thanks,
Martijn
system
March 5, 2018, 7:56pm
2
Look at the setPixelColor() method. It takes the three color values, as bytes, and creates an unsigned long from them. There is an overloaded method that takes an unsigned long.
The variable type in your "I want to do this" is unsigned long.
Two options
as PaulS stated take an
unsigned long colour = R << 16 + G << 8 + B;
create a struct
struct COLOUR {
int R;
int G;
int B;
};
COLOUR colour1, colour2;
some color swapping can be done like this
colour1.R = colour2.G;
colour1.G = colour2.B;
colour1.B = colour2.R;
That functionality is built into the library. Create a color value with one of these methods:
// Convert separate R,G,B into packed 32-bit RGB color.
// Packed format is always RGB, regardless of LED strand color order.
uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
// Convert separate R,G,B,W into packed 32-bit WRGB color.
// Packed format is always WRGB, regardless of LED strand color order.
uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
Then use it with this one:
// Set pixel color from 'packed' 32-bit RGB color:
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {