Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D4
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
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() {
    strip.begin();
    strip.setBrightness(50);
    strip.show(); // Initialize all pixels to 'off'
    Particle.function("set_color", colorSingle);

    Particle.variable("Red", redValue);
    Particle.variable("Green", greenValue);
    Particle.variable("Blue", blueValue);
}

void loop() {

}

int colorSingle(String command) {
    String colors[3];
    char charBuf[20];
    char *token;
    uint32_t c;
    uint8_t wait = 200;
    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();
    Particle.publish("status","changing color");
    c = strip.Color(redValue, greenValue, blueValue);
    colorAll(c, wait);
    return 1;
}

// Set all pixels in the strip to a solid color, then wait (ms)
void colorAll(uint32_t c, uint8_t wait) {
    uint16_t i;

    for(i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
    }
    strip.show();
    delay(wait);
}
Click to Expand

Content Rating

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

0