Can you store RGB values in variable for neopixels?

I have been trying to figure out how to go about storing RGB values in a variable such that when you use something like:

noseJewel.setPixelColor(4, noseJewel.Color(255, 255, 255));

you can use a variable for the RGB values that will change the color with say a button push. Looking for suggestions on where to start on this?

You could make a strict that holds the three values but you'd have to rewrite that function to accept it as an argument. I'd do some digging around the documentation for whatever library you're using. It appears to already have some sort of Color object defined that you could use. You didn't tell what library and only gave one line of code so I can't tell which library you're using. So I can't really help you look through it.

Something like this would make a good starting point.

// Color definitions
static const uint16_t BLACK = 0x0000;
static const uint16_t WHITE   = 0xFFFF;
static const uint16_t NAVY   = 0x000F;
static const uint16_t DARKGREEN   = 0x03E0;
static const uint16_t DARKCYAN   = 0x03EF;
static const uint16_t MAROON   = 0x7800;
static const uint16_t PURPLE   = 0x780F;
static const uint16_t ORANGE   = 0xFD20;
static const uint16_t PINK   = 0xF81F;
static const uint16_t OLIVE   = 0x7BE0;
static const uint16_t BLUE     = 0x001F;
static const uint16_t RED     = 0xF800;
static const uint16_t GREEN   = 0x07E0;
static const uint16_t CYAN     = 0x07FF;
static const uint16_t MAGENTA = 0xF81F;
static const uint16_t YELLOW   = 0xFFE0;
static const uint16_t BRIGHT_RED = 0xF810;
static const uint16_t LIGHT_GREY = 0xC618;//0x8410;
static const uint16_t DARK_GREY = 0x7BEF;//0x4208;
static const uint16_t GREENYELLOW = 0xAFE5;


class colorObj {

public:
  colorObj(byte inRed, byte inGreen, byte inBlue);
  colorObj(word color16);
  colorObj(void);

  void setColor(byte inRed, byte inGreen, byte inBlue);
  void setColor(word color16);
  void setColor(colorObj* inColor);
  word getColor16(void);
    
  byte getRed(void);
  byte getGreen(void);
  byte getBlue(void);

private :
  byte red;
  byte green;
  byte blue;
};



// ****** colorObj ******

colorObj::colorObj(byte inRed, byte inGreen, byte inBlue) { setColor(inRed,inGreen,inBlue); }

//colorObj::colorObj(colorObj* inColor) { setColor(inColor); }

colorObj::colorObj(word inColor16) { setColor(inColor16); }

colorObj::colorObj(void) { setColor(0,0,0); }


void colorObj::setColor(byte inRed, byte inGreen, byte inBlue) {

  red = inRed;
  green = inGreen;
  blue = inBlue;
}


void colorObj::setColor(word color16) {

  switch(color16) {
  case BLACK :           //0x0000
    red = 0;
    green = 0;
    blue = 0;
    break;
  case BLUE :            //0x001F
    red = 0;
    green = 0;
    blue = 255;
    break;
  case RED :             //0xF800
    red = 255;
    green = 0;
    blue = 0;
    break;
  case GREEN :           //0x07E0
    red = 0;
    green = 255;
    blue = 0;
    break;
  case CYAN :            //0x07FF
    red = 0;
    green = 255;
    blue = 255;
    break;
  case MAGENTA :         //0xF81F
    red = 255;
    green = 0;
    blue = 255;
    break;
  case YELLOW :          //0xFFE0 
    red = 255;
    green = 255;
    blue = 0;
    break;
  case WHITE :           //0xFFFF
    red = 255;
    green = 255;
    blue = 255;
    break;
  default :
    red = highByte(color16);
    green = lowByte(color16>>5);
    blue = lowByte(color16)<<3;
  }
}


void colorObj::setColor(colorObj* inColor) {
    
    red =inColor->getRed();
    green =inColor->getGreen();
    blue = inColor->getBlue();
}


// Copied from Adafruit'
word colorObj::getColor16(void) { 
  return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
}


byte colorObj::getRed(void) { return red; }


byte colorObj::getGreen(void) { return green; }


byte colorObj::getBlue(void) { return blue; }

This is a simplified version of what I use. Extremely handy when you extend Adafruit's set & get pixleColor() to use colorObjects.

Actually mine includes this..

//Changes yourself by blending in some new color.
void colorObj::blend(colorObj* mixinColor,byte mixPercent) {
    
    if (mixPercent>=100) {          // If >= 100 means totally mixin color.
        setColor(mixinColor);
    } else if (mixPercent>0) {      // So its NOT >= 100 but it is > 0.. Blend it.
        mixMapper.setColors(this,mixinColor);
        colorObj temp = mixMapper.Map(mixPercent);
        setColor(&temp);
    }                               // Otherwise, there is no change.
}

But that's more for the more advanced version.

Hope this helps.

-jim lee

Color myNoseJewelColor = Color(255,255,255);
...
noseJewel.setPixelColor(4, myNoseJewelColor);

Regards,
Ray L.

byte red = 255;
byte green = 255;
byte blue = 255;
noseJewel.setPixelColor(4, noseJewel.Color(red, green, blue));

Are you stuck on using variables or changing their values when a button is pressed ?

UKHeliBob:

byte red = 255;

byte green = 255;
byte blue = 255;
noseJewel.setPixelColor(4, noseJewel.Color(red, green, blue));



Are you stuck on using variables or changing their values when a button is pressed ?

Trying to figure out how to change the values on button presses. I have one button, would like each press to switch between four different colors.

if(digitalRead(buttonPin) == LOW) {  // if the button is pressed
    red = someNewValue;
    green = someOtherValue;
    blue = whateverBlueNeedsToBe;
    noseJewel.setPixelColor(4, noseJewel.Color(red, green, blue));
}

Delta_G:

if(digitalRead(buttonPin) == LOW) {  // if the button is pressed

red = someNewValue;
    green = someOtherValue;
    blue = whateverBlueNeedsToBe;
    noseJewel.setPixelColor(4, noseJewel.Color(red, green, blue));
}

I'll try it thanks!

I'll try it thanks!

When you have got that working have a look at the StateChangeDetection example in the IDE. It will show you how to detect when the button becomes pressed rather than when it is pressed which will allow you to cycle through several colour combinations.

Use the source, luke!

Let's look at the adafruit library... when you call setPixelColor() with two arguments, that can only be calling this:

void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c) {
  if(n < numLEDs) {
    uint8_t *p,
      r = (uint8_t)(c >> 16),
      g = (uint8_t)(c >>  8),
      b = (uint8_t)c;
    if(brightness) { // See notes in setBrightness()
      r = (r * brightness) >> 8;
      g = (g * brightness) >> 8;
      b = (b * brightness) >> 8;
    }
    if(wOffset == rOffset) {
      p = &pixels[n * 3];
    } else {
      p = &pixels[n * 4];
      uint8_t w = (uint8_t)(c >> 24);
      p[wOffset] = brightness ? ((w * brightness) >> 8) : w;
    }
    p[rOffset] = r;
    p[gOffset] = g;
    p[bOffset] = b;
  }
}

And you see, the "colors" that you're passing it are just an unsigned long (uint32). The color function returns a uint32:

uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b) {
  return ((uint32_t)r << 16) | ((uint32_t)g <<  8) | b;
}

So there you go - you can store colors in uint32's (unsigned long) or generate packed colors on the fly.

OR, you can just use the version of setPixelColor that takes the three colors separately instead of one packed 32-bit value, eg, this one:

void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b)

I'm not even going to ask what a "nose jewel" is...

DrAzzy:
I'm not even going to ask what a "nose jewel" is...

Its actually a neopixel jewel in the nose of a ghostbusters proton gun prop. Out of context it does sound wierd lol.

grogking:
Its actually a neopixel jewel in the nose of a ghostbusters proton gun prop. Out of context it does sound wierd lol.

I was wondering where the wires to it went ...

DrAzzy:
Use the source, luke!

That's a keeper