Back to Parent

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

#include "neopixel.h"

#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

long timerStartAt = -1;
int status = 0; 
std::array<int, 3> red = {255, 0, 0};
std::array<int, 3> white = {255, 255, 255};
long fadeDuration = 1000 * 15 * 60;

void setup() {
    Serial.begin(9600); 
    strip.begin();
    strip.show(); 
    Particle.function("changeStatus", handleStatue);
    Particle.variable("status", status);
}

uint32_t getCurrentRGB(int fadeDuration, int timeElapsed, std::array<int, 3> prevColor, std::array<int, 3> targetColor) {
    int prev_r = prevColor[0];
    int prev_g = prevColor[1];
    int prev_b = prevColor[2];

    int target_r = targetColor[0];
    int target_g = targetColor[1];
    int target_b = targetColor[2];

    int r = 0, g = 0, b = 0; 

    if (fadeDuration != 0) {
        r = map(timeElapsed, 0, fadeDuration, prev_r, target_r);
        g = map(timeElapsed, 0, fadeDuration, prev_g, target_g);
        b = map(timeElapsed, 0, fadeDuration, prev_b, target_b);
    }

    return strip.Color(r, g, b);
}

int handleStatue(String new_status) {
    if (new_status.toInt() >= 0 && new_status.toInt() <= 2) {
        status = new_status.toInt();
    } else {
        Particle.publish("error status");
    }
    return 1;
}

void loop() {
    long timeNow = millis();
    long timeElapsed = timeNow - timerStartAt;
    uint32_t c;

    switch (status) {
        case 0:
            c = strip.Color(255, 255, 255);
            timerStartAt = timeNow;
            break;
        case 1:
            c = (timeElapsed < fadeDuration) ? getCurrentRGB(fadeDuration, timeElapsed, white, red) : (status = 2, timerStartAt = timeNow, strip.Color(255, 255, 255));
            break;
        case 2:
            c = (timeElapsed < fadeDuration) ? getCurrentRGB(fadeDuration, timeElapsed, red, white) : (status = 0, timerStartAt = timeNow, strip.Color(255, 255, 255));
            break;
        default:
            Particle.publish("error status");
            return;
    }

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

Content Rating

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

0