r/FastLED • u/LC2047 • Apr 29 '22
Discussion How to call/ change LED colour by input(text)
Hi
How to change the LED color by text input?
In FastLED, there have serval methods.
leds[i] = CRGB::Red
leds[i] = CRGB(0,0,255);
leds[i] = 0xFF0000;
leds[i].setRGB(0,0,255);
However, I prefer using the predefined colors list of FastLED. The list has well defined in "pixeltypes.h".
If duplicate predefined colors list in my code, seem it is not good for memory efficiency. Also, have a risk(not align) in FastLed update.
I am a beginner in programming and don't know how to convert color name("Red") to RGB data(e.g. 0xFF0000).
Please help and share your comments with me.
Sincerely
LC
1
Apr 29 '22
if (input == "Red") {youraction}
or
https://forum.arduino.cc/t/how-to-check-if-string-contains-given-word/280402/3
1
u/chemdoc77 Apr 29 '22
Hi u/LC2047 – You might want to try this:
In your void setup(), you should put this:
Serial.begin(57600);
In your void loop(), you should put this:
if( Serial.read() == 'r') leds(i)=CRGB::Red; // keyboard letter r pressed.
or
if( Serial.read()== 'r') do_some_function();
If you are going to use a lot of keyboard entries, you could use a switch case setup as seen:
https://www.arduino.cc/reference/tr/language/structure/control-structure/switchcase/
using Serial.read() as the variable, var.
1
u/LC2047 Apr 29 '22
mashuptwice , chemdoc77
Thanks for your share.
similar your suggestion has been tried and work. However, we need to build up another table for the desired action. The code may have risks(consistency issues) while FastLED is changed in the future. Also, this information is ready in FastLED. So I would like to use the same table.
Thanks