Back to Parent

// This #include statement was automatically added by the Particle IDE.
#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 );

uint32_t w = strip.Color(255, 255, 255);
uint32_t r = strip.Color(255, 0, 0);
//container for strip.Color

int Brightness = 0;             // container for map the elapsed time
int interval = 60000;           // fade duration
unsigned long lastFade = 0;
unsigned long start = 0;
unsigned long elapsedTime = 0;
//variable to store brightness
//lu data for fade and refade

bool fadeFlag = false;
//flag to trigger fading

void setup() {
    
    Serial.begin(9600);
    
    strip.begin();
    strip.show();
    
    Particle.function( "ambientOrb" , ambientOrb );
    Particle.function( "turnRed", turnRed );
    //testing with red
    setColor(w);
    //default color white
    Serial.println("Setup");

}

void loop() {
    
    if (fadeFlag) {
    start = millis();
    Serial.println("Call fade function");
    Serial.printlnf("Start Time   : %5.2lu ms", start);
    fade(w, r, false);

    start = millis();
    Serial.println("Call refade function");
    Serial.printlnf("Start Time   : %5.2lu ms", start);
    fade(r, w, true);

    fadeFlag = false; // Reset the flag after fading
  }
}

int ambientOrb( String cmd ){
    if (cmd == "fade") {
        fadeFlag = true; // Set the flag to trigger fade in the loop
        Serial.println("Flag to fade");
        return 1;
    } else {
        setColor(w);
        return 0;
    }
}

int turnRed( String command ){
    if ( command == "red" ){
        setColor(r);
        Serial.println("Set to Red");
        return 1;
    }else{
        setColor(w);
        Serial.println("Reset to Default Color");
        return -1;
    }
}

void fade(uint32_t oColor, uint32_t dColor, bool refade){
    unsigned long now = millis();
    int n = 0;                      
    //counter for serial port
    while (now - start <= interval) {
        now = millis();
        elapsedTime = now - start;
        n++;
        //activate counter
        Serial.println(n);
        Serial.println(refade ? "Re-Fading" : "Fading");
        int mapBrightness;
        if (refade) {
            mapBrightness = map(elapsedTime, 0, interval, 0, 255);
        } else {
            mapBrightness = map(elapsedTime, 0, interval, 255, 0);
        }
        //define fade direction based on bool input
        uint32_t f = strip.Color(255, mapBrightness, mapBrightness);
        // Calculate the brightness based on the elapsed time
        // Container for strip.Color
        for (int i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, f);
        }
        strip.show();
        // Update color to neopixel
        Serial.printlnf("Elapsed Time : %5.2lu ms", elapsedTime);
        Serial.printlnf("Brightness   : %5.2lu", mapBrightness);
        Serial.println();
        delay(1000);
    }
    Serial.println(refade ? "Re-Fade function ended" : "Fade function ended");
}

void setColor(uint32_t color){
    for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, color);
    }
    strip.show();
}
//setColor function to change color immediatly
Click to Expand

Content Rating

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

0