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
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.
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;