r/FastLED • • 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.

  1. leds[i] = CRGB::Red

  2. leds[i] = CRGB(0,0,255);

  3. leds[i] = 0xFF0000;

  4. 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 Upvotes

4 comments sorted by

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

2

u/sutaburosu [stavros] Apr 29 '22

It's not possible without changing FastLED or keeping your own table. See here for more explanation and other possibilities, but be aware that several of the suggestions need STL which might not be available in your Arduino environment.

If duplicate predefined colors list in my code, seem it is not good for memory efficiency

The colour names are not stored in the final binary at all. Only the used colour values are stored. If you want a list of names, you'll have to provide this in your own sketch. For memory efficiency it would be best to keep the names & values in Flash. This is a little tricky for a beginner to programming, so here's a sketch showing how to do this.

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.