Hey Guys!
New here and wondering if someone can help.
Got a project using Blynk/FastLED and was wondering if someone can help.
Below is the code. Essentially what I want to do is take in the string value recieved the blynk URL and assign the LEDs to that colour.
void setColour(CRGB colour, int led_start, int led_end) {
for (int i = led_start; i < led_end; i++) {
leds1[i] = colour;
FastLED.show();
delay(50);
}
}
BLYNK_WRITE(V0)
{
String colour = param.asStr();
setColour (colour, 0, 36);
fadeFlash(0, 36); //separate function that doesn't matter right now
}
As you can see i have the BLYNK_WRITE(V0) {...} function and what this does is assign whatever string value it receives to the variable colour. I then pass this into the function setColour() as well as what which LED I want to be controlled. However because the CRGB can't tell it's a variable, it comes back with an error saying this cannot work. If I set something like: leds1[i] = Red; that would work as expected because Red is in the library however I want to be able to assign a colour over the web with blynk, it to search the FastLED library and show that colour.
Hope that all makes sense! Any help would be extremely appreciated. Thanks in advance!
hey, thanks for the reply.
I did have a look at that, but not sure if I am missing something, theses are all pre set value, so... leds[i] = CRGB::HotPink; but what I want to do is something like leds[i] = CRGB::*variable*;
Unless I am missing something?
Thanks for all the replies! Read all what you have wrote and it makes sense.
Not sure if this is a stupid question but I will ask it anyways. Is there a way to search something like a header file with all of those colours as a string as assign my_colour to that type?
If you mean in a running program for your convenience, I don't think you'll find what you are wanting.
But you could (easily?) do some work on that header file and construct your own searchable table of names stored as Strings, or better yet real C strings (character arrays), where the search for a name would allow retrieval of the 0xFF00FF or whatever color value.
Either this makes total sense and would be easy, or it will launch you on a journey of discovery, discovery of fairly basic programming and data wrangling techniques.
Give it a shot and ask for help if you need. Creating the whole table could be done by hand, or challenge yourself to write a program (on a real computer) to do it for you.
I left searching the table to find the value for a color you have the name of to you.
struct {
char *name ;
unsigned long colorValue;
} myColorTable[] = {
{"RED", 0xFF0000},
{"GREEB", 0x00FF00},
{"YELLOW", 0xFFFF00},
{"BLUE", 0x0000FF},
};
void setup() {
Serial.begin(115200);
Serial.println("Hello Table Wrold!\n");
Serial.print("color ");
Serial.print(myColorTable[3].name);
Serial.print(" is ");
Serial.println(myColorTable[3].colorValue, HEX);
Serial.println();
Serial.print("color ");
Serial.print(myColorTable[2].name);
Serial.print(" is ");
Serial.println(myColorTable[2].colorValue, HEX);
}
void loop() {
// put your main code here, to run repeatedly:
}
Thanks!
All this is very new to me, so it seems it is going to send me down a journey of discovery! I will give it a go and come back for any help I need, thank you again!
ok yep, already getting confused lol with the main part of this. I understand the basics of what you have written, so if I am not mistaken you have created a struct with the name and hex value, and i understand how you print those values, but how would you actually search for the name and get the hex value (I guess that the main part lol, but I am not understanding how I will get my variable to search in the table and pull the value?
Searching and sorting are things you could make a career of.
For all I know, C++ has some build in ability to make this kinda thing easy.
But I would recommend trying to do it the hard way, or the old way, whatever.
You just need to run a for loop through the table, checking if the name in hand matches the name at the current index. In the loop
if (!strcmp(colorISeek, myColorTable[index].name))
break; // found color at index, outta here
Then the same index is used to get the color hex 0x00FFFF when you looked for CYAN, e.g.
unsigned long colorIwant = myColorTable[index].colorValue;
You might not find the color name, so plan for failure and default behaviour in that case.
You might be short on time, in which case more sophisticated search methods can be used, if the table was sorted alphabetically.
Which just points up the intimate connection between searching and sorting, and whys it is important and so many algo books take their sweet time exposing them.
Give it a go. Consult your favorite learning source.