Calculating an RGB-Value array possible?

Hi!

I've built a little controller for a PC-Casemod which controls an RGB-LED-Strip that change their color depending on the inner case temperature.

To get it to work I've used some P16NF06L N-Channel Power mosfets and an DS18S20 temperature sensor with the OneWire library.

So far, everything works perfect.

The only "issue" I have with the code is that for the color fade (from blue to red) I'm using a hard-coded array for the single values, which makes the code fairly big and somewhat hard to maintain.

What I'd like to do is to calculate an array so the resolution could be determined in a variable.
I just don't understand how I could implement this in the code since sometimes the values are constant and sometimes they change.

R     G      B
0     0     255    Blue
0    255    255    Teal
0    255     0      Green
255  255     0      Yellow
255   0      0      Red

This would be the min/max values to change the color specific to my needs.

And this are the arrays I've come up with that I calculated manually:

int C_RED[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
int C_GREEN[] = {0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 238, 221, 204, 187, 170, 153, 136, 119, 102, 85, 68, 51, 34, 17, 0};
int C_BLUE[] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 238, 221, 204, 187, 170, 153, 136, 119, 102, 85, 68, 51, 34, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

As you can see, they are pretty big, I'd like to calculate them in the setup routine so I can determine the resolution.

I'd appreciate any help :slight_smile:
Full code in the attachment.

Thanks in advance.
Andy

inframe_led_temp.ino (2.7 KB)

As you can see, they are pretty big, I

Twice as big as they need to be, at least.
They should be of type "byte".
They should also be in PROGMEM.

Ugly solution:
byte getRed(i) {return (i<33?0:(i>47?255:((i-32)*17)));}

(etc)

Assuming that val is in the 0 .. 1023 range:

  if (val < 256)
  { // red to yellow
    r = 255;
    g = val;
    b = 0;
  }
  else if (val < 512)
  { // yellow to green
    r = 511- val;
    g = 255;
    b = 0;
  }
  else if (val < 768)
  { // green to cyan
    r = 0;
    g = 255;
    b = val - 512;
  }
  else
  { // cyan to blue
    r = 0;
    g = 1023 - val;
    b = 255;
  }

+1 for Jobi-Wan