Back to Parent

#include "neopixel.h"

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

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int fadeInDuration = 1000 * 60 * 10; // 10 minutes
int fadeToRedDuration = 1000 * 60 * 5; // 5 minutes
int fadeOutDuration = 1000 * 60 * 5; // 5 minutes
long timeStart = -1;
int stage = 0; // 0: idle, 1: fading to white, 2: fading to red, 3: solid red, 4: fading out

void setup() {
    strip.begin();
    strip.show();
    setCoolWhite(); 

    Particle.function("Reminder", LEDReminder);
}

void loop() {
    if (stage > 0) {
        long timeNow = millis();
        long timeElapsed = timeNow - timeStart;

        if (stage == 1 && timeElapsed < fadeInDuration) {
            fadeToWhite(timeElapsed);
        } else if (stage == 2 && timeElapsed < fadeInDuration + fadeToRedDuration) {
            fadeToRed(timeElapsed - fadeInDuration);
        } else if (stage == 3 && timeElapsed < fadeInDuration + fadeToRedDuration) {
            setSolidRed();
        } else if (stage == 4 && timeElapsed < fadeInDuration + fadeToRedDuration + fadeOutDuration) {
            fadeOut(timeElapsed - fadeInDuration - fadeToRedDuration);
        } else {
            setCoolWhite();
            stage = 0; // Reset stage
        }
    }
}

int LEDReminder(String cmd) {
    timeStart = millis();
    stage = 1; // Start fading to white
    return 1;
}

void setCoolWhite() {
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(0, 255, 0)); // Cool white color
    }
    strip.show();
}

void fadeToWhite(long timeElapsed) {
    int brightness = map(timeElapsed, 0, fadeInDuration, 0, 255);
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(brightness, brightness, brightness));
    }
    strip.show();
    if (timeElapsed >= fadeInDuration) {
        stage = 2; // Next stage: fade to red
    }
}

void fadeToRed(long timeElapsed) {
    int redValue = map(timeElapsed, 0, fadeToRedDuration, 255, 255);
    int greenBlueValue = 255 - map(timeElapsed, 0, fadeToRedDuration, 0, 255);
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(redValue, greenBlueValue, greenBlueValue));
    }
    strip.show();
    if (timeElapsed >= fadeToRedDuration) {
        stage = 3; // Next stage: solid red
    }
}

void setSolidRed() {
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(255, 0, 0)); // Solid red color
    }
    strip.show();
    stage = 4; // Next stage: fade out
}

void fadeOut(long timeElapsed) {
    int brightness = 255 - map(timeElapsed, 0, fadeOutDuration, 0, 255);
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(brightness, 0, 0));
    }
    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