Definining colors on RGB LCD

I'm trying to understand how I can define certain colors rather than changing the numbers each time.

From the example code below, I get the color red, but how would I make it so that I can use along the lines of lcd.setRGB(red) or lcd.setRGB(blue)

/*!
 * @file HelloWorld.ino
 * @brief Show helloworld.
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @maintainer [yangfeng](feng.yang@dfrobot.com)
 * @version  V1.0
 * @date  2021-09-24
 * @url https://github.com/DFRobot/DFRobot_RGBLCD1602
 */
#include "DFRobot_RGBLCD1602.h"

const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

DFRobot_RGBLCD1602 lcd(/*lcdCols*/16,/*lcdRows*/2);  //16 characters and 2 lines of show

void setup() {
    /**
     *  @brief initialize the LCD and master IIC
     */ 
    lcd.init();
    
    lcd.setRGB(colorR, colorG, colorB);
    
    // Print a message to the LCD.
    lcd.print("hello, world!");

    delay(1000);
}

void loop() {
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    /**
     *  @brief set cursor position
     *  @param col columns optional range 0-15
     *  @param row rows optional range 0-1,0 is the first row, 1 is the second row
     */
    lcd.setCursor(0, 1);
    // print the number of seconds since reset:
    lcd.print(millis()/1000);

    delay(100);
}

You could write your own function that took a colour name as a parameter. Define the colours in an ENUM to give them names.

Your function would then call the setRGB() function with the appropriate RGB values perhaps using switch/case for clarity or an array of R, G, and B values indexed on the value of the colour name passed to the function to derive the RGB values

Websites like this one help finding the RGB-values

best regards Stefan

I will have a look into what ENUM's are.. I suspect there's no easy option for this and I might as well just set using the numerical references. Cheers

Thanks Stefan, the colour codes are not the problem, I was just wanting to define the colours as opposed to using the numerical values each time.
Nice site though.

Cheers

24bit-values

// Farbwerte für Adafruit Neo-Pixel
const uint32_t BLACK = 0x000000;
const uint32_t BLUE = 0x0000FF;
const uint32_t GREEN = 0x008000;
const uint32_t LIME = 0x00FF00;
const uint32_t CYAN = 0x00FFFF;
const uint32_t ROYALBLUE = 0x4169E1;
const uint32_t STEELBLUE = 0x4682B4;
const uint32_t MEDIUMTURQUOISE = 0x48D1CC;
const uint32_t INDIGO = 0x4B0082;
const uint32_t AQUAMARINE = 0x7FFFD4;
const uint32_t MAROON = 0x800000;
const uint32_t PURPLE = 0x800080;
const uint32_t BLUEVIOLET = 0x8A2BE2;
const uint32_t YELLOWGREEN = 0x9ACD32;
const uint32_t GREENYELLOW = 0xADFF2F;
const uint32_t VIOLET = 0xEE82EE;
const uint32_t RED = 0xFF0000;
const uint32_t MAGENTA = 0xFF00FF;
const uint32_t DEEPPINK = 0xFF1493;
const uint32_t ORANGERED = 0xFF4500;
const uint32_t TOMATO = 0xFF6347;
const uint32_t HOTPINK = 0xFF69B4;
const uint32_t ORANGE = 0xFFA500;
const uint32_t PINK = 0xFFC0CB;
const uint32_t GOLD = 0xFFD700;
const uint32_t YELLOW = 0xFFFF00;
const uint32_t LIGHTYELLOW = 0xFFFFE0;
const uint32_t WHITE = 0xFFFFFF;
color24bit = GOLD;
byte blue=  color24bit; // as a byte can hold only 8 bits only the lowest 8 bits get assigned
color24bit >> 8; // shift 8 bits to the right kicks out the lowest 8 bits we have just assigned to blue
// ==> the 8 to16ths bit become bits 0 to 7 

byte green =  color24bit; // as a byte can hold only 8 bit only the lowest 8 bits get assigned
color24bit >> 8; // shift 8 bits to the right kicks out the lowest 8 bits we have just assigned to green
// ==> the 8 to16ths bit become bits 0 to 7

byte red =  color24bit;

best regards Stefan

1 Like

You can already access some basic colours with that library:

setColor(RED);

but only for the basic colours

#define WHITE           0
#define RED             1
#define GREEN           2
#define BLUE            3

You can edit the library code and add more colours. Those constants are offsets to this colour table:

const uint8_t color_define[4][3] = 
{
    {255, 255, 255},            // white
    {255, 0, 0},                // red
    {0, 255, 0},                // green
    {0, 0, 255},                // blue
};
1 Like

This is what I'm looking for, I'm lost how to implement it though, I have tried different ways, but I get "setColor" was not declared in this scope.

Aha got it Thanks lcd.setColor(RED);

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