Back to Parent

int redValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int greenValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int blueValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255

void setup()
{
    //Register our Particle function here
    Particle.function("led", ledControl);
    
    Particle.variable("Red", redValue);
    Particle.variable("Green", greenValue);
    Particle.variable("Blue", blueValue);
}

void loop()
{
    // Nothing to do here
}

// This function gets called whenever there is a matching API request
// the command string format is <red>,<green>,<blue>
// for example: 255,0,128 or 128,0,0

int ledControl(String command)
{

    String colors[3];
    char charBuf[20];
    char *token;
    const char *s = ",";
    int i = 1;
    command.toCharArray(charBuf, 20);
    /* get the first token */
    token = strtok(charBuf, s);
    colors[0] = token;
    /* walk through other tokens */
    while( token != NULL ) {
        token = strtok(NULL, s);
        colors[i] = token;
        i++;
   }
    
    redValue = colors[0].toInt();
    greenValue = colors[1].toInt();
    blueValue = colors[2].toInt();


    // We need to say we'll be controlling the RGB led
    RGB.control(true);

    // set it to white
    RGB.color(redValue,greenValue,blueValue);

    // wait 3 seconds
    delay(3000);

    // return control of the RGB led to Particle
    RGB.control(false);


   return 1;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0