Back to Parent

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

// This #include statement was automatically added by the Particle IDE.


// Include Particle Device OS APIs
#include <Particle.h>

#define PIXEL_PIN SPI
#define PIXEL_COUNT 7
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

bool fadeToRedActive = false;      // Whether the fade to red is active
unsigned long fadeStartTime = 0;   // Start time for the fade effect

// Color definitions
const uint32_t OFF = strip.Color(0, 0, 0);
const uint32_t FULL_RED = strip.Color(255, 0, 0);

// setup() runs once, when the device is first turned on
void setup() {
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
  
    // Register Particle Cloud function
    Particle.function("startFadeToRed", startFadeToRed);
}

// Particle function to initiate fade to red
int startFadeToRed(String command) {
    fadeToRedActive = true;
    fadeStartTime = millis();  // Record the start time
    return 1;  // Return success
}

// Function to handle fading to red over 1 minute
void fadeToRed() {
    unsigned long currentTime = millis();
    unsigned long elapsedTime = currentTime - fadeStartTime;

    if (elapsedTime >= 60000) {  // 1 minute (60000 ms) has passed
        // Set all pixels to full red and stop fading
        for (uint16_t i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, FULL_RED);
        }
        strip.show();
        fadeToRedActive = false;  // Stop the fade effect
    } else {
        // Calculate the fade level from 0 to 255 based on elapsed time
        uint8_t fadeLevel = (elapsedTime * 255) / 60000;

        // Set each pixel to the progressively faded red color
        for (uint16_t i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, strip.Color(fadeLevel, 0, 0));  // Red with increasing intensity
        }
        strip.show();
    }
}

// Main loop function
void loop() {
    if (fadeToRedActive) {
        fadeToRed();  // Call fade function if active
    }
}
Click to Expand

Content Rating

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

0