Listing RGB Values

Hello all!

I am making a Ionizing Radiation Detector project. It utilizes a I2C 16x2 LCD with RGB backlight to display the info. I have made a list of radiation values and their equivalent devices that emit it.

int values[] =             {     1,        5,          10,         40,      100,       250,          400,          1000,      10000   };
const char* equivalent[] = { "Airport", "Dental", "Norm(1day)", "Flight", "X-Ray", "NPP(1year)", "Mammogram",  "Gov Limit", "CT Scan" };

I have used this function to find the equivalent:

int nearestEqual(int x, bool sorted = true) {
  int idx = 0; // by default near first element
  int distance = abs(bearings[idx] - x);
  for (int i = 1; i < arrSize(bearings); i++) {
    int d = abs(bearings[i] - x);
    if (d < distance) {
      idx = i;
      distance = d;
    }
    else if (sorted) return idx;
  }
  return idx;
}

I am trying to add RGB values for each equivalent[]. Basically, a color that the display turns when one of those equivalents[] is reached. I hope this makes sense?

I was thinking maybe something like this, but I could be totally butchering it:

const char* equalcolor[r,g,b] = {[255,0,0],[255,127,20],[255,255,0],etc. 

Thanks in advance!

It sounds to me as though you need an array of structs. Each struct would have 3 values in it, the value, the equivalent and the RGB values which could be separate if you want to do it that way

The advantage of a struct is that it keeps all the values together even if they are of different types and an array of structs allows you to iterate through it testing one variable and then to access the corresponding value of an associated variable

1 Like

Hello @UKHeliBob !

Ummm, sorry. How would you do that?

const uint8_t equalcolor[][3] = {
{255,0,0},
{255,127,20},
{255,255,0},
etc.
}
1 Like

An example

struct data_layout
{
  byte value;
  char * equivalent;
  byte R;
  byte G;
  byte B;
} data[] =
{
  {
    1, "Airport", 255, 0, 0
  },
  {
    2, "Dental", 255, 127, 20
  },
  {
    3, "Norm(1 day)", 255, 255, 0
  }
};

void setup()
{
  Serial.begin(115200);
  showMatchingData(1);
  showMatchingData(2);
}

void loop()
{
}

void showMatchingData(byte target)
{
  for (int x = 0; x < 3; x++)
  {
    if (data[x].value == target)
    {
      Serial.println(data[x].equivalent);
      Serial.println(data[x].R);
      Serial.println(data[x].G);
      Serial.println(data[x].B);
    }
  }
}

One advantage of using structs is that it allows you to use meaningful names for the variables

1 Like

Thanks so much @UKHeliBob for your thorough explanation! That makes sense now; I used a similar setup in a radio project I did a bit back, but I forgot :man_facepalming:.

Thanks @johnwasser !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.