Single function to receive multiple values

Hi there,

I’ve not gotten buch luck with getting ArtNet working, so am falling back onto the original plan, the Cloud API.

I’ve found the Simple-Spark-Core-Controller (which I believe @BDub put together) to be extremely helpful, and can get it successfully talking to the :spark: :smiley:

I was hoping I could go one step further – rather than having to have duplicate functions reside on the Spark Firmware, I was hoping to be have code on the Firmware which receives three values, separated by commas, and then writes these values to an output.

ie: 098,183,204 would get parsed and send value 098 to A5, 183 to A6, and 204 to A7.

Here’s the code I’ve got so far, but it’s not doing what I want. I’ve tested it and it just seems to grab the integer in its physical location instead of its CSV grouping.

int rgbstrip(String command)
    // Convert received string to RGB values
    String r = command.substring(1);
    int rval = r.toInt();
    String g = command.substring(2);
    int gval = g.toInt();
    String b = command.substring(3);
    int bval = b.toInt();

    // Write values to outputs
    analogWrite(red, rval);
    analogWrite(green, gval);
    analogWrite(blue, bval);

    // Return response
    return 1;

Any advice would be appreciated :smile:

(Once I’ve got this working, I just need to figure out how to get the value to “fade”. For example, if the value on a pin started at 200 and needs to get to 10, I want it to gradually dim. If anyone has a lightweight way of doing this, I’d also be interested to know.)

@Jeff, I suppose you are trying to set color of RGB Led, below is the code I used to parse and set the color for my BlinkM project.

int r, g, b;
char szArgs[13];

command.toCharArray(szArgs, 12);

sscanf(szArgs, "%d,%d,%d", &r, &g, &b);

analogWrite(red, r);
analogWrite(green, g);
analogWrite(blue, b);

That worked perfectly @krvarma! Thanks so much!

Now I just need to find out how to make the colour fade from the old to the new and the code will be complete :smile:

@Jeff, a poor man’s version is like this, you can introduce more complicated formula to get realistic fades

float oldr;
float oldg;
float oldb;

float newr;
float newg;
float newb;

float cr = oldr;
float cg = oldg;
float cb = oldb;

int steps

float rstep = (newr - oldr) / steps;
float gstep = (newg - oldg) / steps;
float bstep = (newb - oldb) / steps;

for(int i=0; i<steps; ++i){
    cr += rstep;
    cg += gstep;
    cb += bstep;
1 Like