RGB LCD Code Questions

I tried to search for answers to this initially but could not find any help.

I recently purchased a 20x4 RGB LCD negative from adafruit. I have it all hooked up and everything works fine. My question is, I am very new to coding, and I cannot decifer from the rgb swirly example provided by adafruit how to control the display colors. I am working on a project that will display results from a sensor in a good/green, not so good/yellow, bad/red fashion. Any help would be greatly appreciated. I can see in my code that i have a "#define REDLITE" but I am not sure how to call upon that in my code to turn the text red.

Arduino Uno R3 RGB 20x4 LCD negative from adafruit

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"
#include "LiquidCrystal.h"
#include "Wire.h"

#define DHTPIN 2 // DHT on Pin 2
#define DHTTYPE DHT22
#define REDLITE 3 // Red LED on pin 3
#define GREENLITE 5 // Green LED on pin 5
#define BLUELITE 6 // Blue LED on pin 6

byte degree[8] = {
  B00100,
  B01010,
  B01010,
  B00100,
  B00000,
  B00000,
  B00000,
  B00000
};

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// you can change the overall brightness by range 0 -> 255
int brightness = 255;

void setup() {
  dht.begin();
  
    // set up the LCD's number of rows and columns: 
  lcd.createChar(0, degree);
  lcd.begin(20, 4);
  // Print a message to the LCD.
  lcd.print("Initializing...");

   
  pinMode(REDLITE, OUTPUT);
  pinMode(GREENLITE, OUTPUT);
  pinMode(BLUELITE, OUTPUT);

  brightness = 100;
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float temp_f = (t * 9.0 +2.0)/5.0 + 32.0; // Conversion from Celsius to Farenheit by Rob Tillaart

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    lcd.print("Failed to read from DHT");
  } else {
    
    // Line 1 - Humidity
    lcd.setCursor(0,0);
    lcd.print("Humidity   : ");
    lcd.setCursor(13,0);
    lcd.print(h);
    lcd.setCursor(19,0);
    lcd.print("%");
    
    // Line 2 - Humidity Avg. and Max
    // Avg.
    lcd.setCursor(0,1);
    lcd.print("Avg:");
    // Add readings later
    lcd.setCursor(4,1);
    lcd.print("00.00");
    lcd.setCursor(9,1);
    lcd.print("%");
    // Max
    lcd.setCursor(10,1);
    lcd.print("Max:");
    lcd.setCursor(14,1);
    // Add readings Later
    lcd.print("00.00");
    lcd.setCursor(19,1);
    lcd.print("%");
    
    // Line 3 - Temperature
    lcd.setCursor(0,2);
    lcd.print("Temperature: ");
    lcd.setCursor(13,2);
    lcd.print(temp_f);
    lcd.setCursor(18,2);
    lcd.write(byte(0));
    lcd.setCursor(19,2);
    lcd.print("F");
    
    // Line 4 - Temperature Avg. and Max
    // Avg.
    lcd.setCursor(0,3);
    lcd.print("Avg:");
    // Add readings later
    lcd.setCursor(4,3);
    lcd.print("00.00");
    lcd.setCursor(9,3);
    lcd.print("%");
    // Max
    lcd.setCursor(10,3);
    lcd.print("Max:");
    lcd.setCursor(14,3);
    // Add readings Later
    lcd.print("00.00");
    lcd.setCursor(19,3);
    lcd.print("%");
    delay(3000);
  }
}

If you only want the three primary colors, I think you can just set the proper “Lite” output to 0.
e.g., for a red background:
analogWrite(REDLITE, 0);
It’s 0 since the LEDs are common anode.

It might be simpler (in terms of your understanding) to just use the LEDs in digital mode:
e.g.,
digitalWrite(REDLITE,LOW);

If the parameter dictating the background color is temperature, and you wanted blue, green or red backgrounds the logic would have the shape:

if (temp_f<=threshold1) //assuming colder is better
{ digitalWrite(BLUELITE, LOW);
digitalWrite(GREENLITE, HIGH);
digitalWrite(REDLITE, HIGH);
}
else if (temp_f<=threshold2)
{ digitalWrite(BLUELITE, HIGH);
digitalWrite(GREENLITE, LOW);
digitalWrite(REDLITE, HIGH);
}
else
{ digitalWrite(BLUELITE, HIGH); //RED HOT
digitalWrite(GREENLITE, HIGH);
digitalWrite(REDLITE, LOW);
}

for a yellow background, turn on red and green at the same time.

According to the adaFruit example, the red background may be brighter than the other backgrounds. This could make your yellow look orange. If that is a problem, you may have to use the analogWrite method and experiment with values >0 to attenuate the RED.

Thank you for your help. I will try what you suggested.

I was finally able to get it working for the most part last night. I added this to the bottom of my code after the loop, then added setBacklight(255,0,0); just above my first lcd.print which gave me a red background. So, I guess now that I got that figured out finally, I'll have to learn about making thresholds and if statements.

void setBacklight(uint8_t r, uint8_t g, uint8_t b) {
  // normalize the red LED - its brighter than the rest!
  r = map(r, 0, 255, 0, 100);
  g = map(g, 0, 255, 0, 150);

  r = map(r, 0, 255, 0, brightness);
  g = map(g, 0, 255, 0, brightness);
  b = map(b, 0, 255, 0, brightness);

  // common anode so invert!
  r = map(r, 0, 255, 255, 0);
  g = map(g, 0, 255, 255, 0);
  b = map(b, 0, 255, 255, 0);
  analogWrite(REDLITE, r);
  analogWrite(GREENLITE, g);
  analogWrite(BLUELITE, b);
}

Your results are consistent with how I think your device works. If you had used setBacklight(0,255,0); I think your background would be Green and if you used setBacklight(0, 0, 255, 0); you'd be Blue. But not the sad kind of blue because it works.