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 D3
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

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

uint32_t c;

int fadeDur = 1000 * 60 * 20;
long timeStartedAt = -1;
bool eventStart = false;

void setup() {
    Serial.begin(9600);
    Particle.function("lightUp", lightUp);
    Particle.function("denotice", denotice);
    strip.begin();
    strip.show();
}

void loop() {
    // original state
    if (eventStart == false) {
        for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, 0, 0, 0);
        }
        strip.show();
        delay(1000);
    }
    
    // when fadeOutTimerStarted is true
    if (eventStart == true) {
        long timeNow = millis();
        long timeLap = timeNow - timeStartedAt;
        int colorVal;
        if (timeLap <= fadeDur / 4) {
            colorVal =  map(timeLap, 0, fadeDur / 4, 0, 100);
            display(0, colorVal, 0);
        } else if (timeLap > fadeDur / 4 && timeLap < 3 * fadeDur / 4) {
            colorVal =  map(timeLap, fadeDur / 4, 3 * fadeDur / 4, 1, 100);
            display(colorVal, 0, 0);
        } else if (timeLap >= 3 * fadeDur / 4 && timeLap <= fadeDur) {
            colorVal =  map(timeLap, 3 * fadeDur / 4, fadeDur, 100, 0);
            display(colorVal, 0, 0);
        } else {
            eventStart = false;
        }
    }
} 

void display(int r, int g, int b) {
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, r, g, b);
    }
    strip.show();
}

int lightUp(String cmd) {
    timeStartedAt = millis();
    eventStart = true;
    return 1;
}

int denotice(String cmd) {
    eventStart = 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