Back to Parent

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

#define PIXEL_PIN D3
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int fadeDuration = 1000 * 60 * 15;
long timeStart = -1;
bool timerStarted = false;
bool fadingToRed = true;  // Added flag to track the direction of the fade

void setup() {
    strip.begin();

    // Set the initial state to white
    for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, 255, 255, 255);
    }

    strip.show();

    Particle.function("calEvent", calEvent);
}

void loop() {
    if (timerStarted == true) {
        long timeNow = millis();
        long timeElapsed = timeNow - timeStart;

        if (timeElapsed < fadeDuration) {
            int colorValue = map(timeElapsed, 0, fadeDuration, 0, 255);

            int r, g, b;

            if (fadingToRed) {
                // Fade from white to red
                r = 255;
                g = 255 - colorValue;
                b = 255 - colorValue;
            } else {
                // Fade from red to white
                r = 255;
                g = 0 + colorValue;
                b = 0 + colorValue;
            }

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

        } else {
            if (fadingToRed) {
                // Toggle the direction of the fade
                fadingToRed = !fadingToRed;

                // Reset the timer and flag
                timeStart = millis();
            } else {
                // Set the light bar to white and stop the timer
                for (int i = 0; i < strip.numPixels(); i++) {
                    strip.setPixelColor(i, 255, 255, 255);
                }
                strip.show();
                timerStarted = false;
                fadingToRed = !fadingToRed;     //allows function to be called multiple times and still have the same fade each calendar event
            }
        }
    }

}

int calEvent(String cmd) {
    timeStart = millis();
    timerStarted = true;
    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