Back to Parent

// Skill Dev 3
// Ambient Orb Google Cal Notification
#include "neopixel.h"

// Neopixel Config
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

bool alertActive = false;
unsigned long alertStartTime = 0;
const unsigned long alertDuration = 900000; // 15 minutes

void setup() {
  strip.begin();
  strip.show(); 
  Particle.function("CalEvent", calendarAlert); // IFTTT CalEvent function
  Particle.function("turnOff", turnOff); // For testing purposes
}

void loop() {
  unsigned long currentTime = millis();
  
  if (alertActive) {
    if (currentTime - alertStartTime < alertDuration) {
      // Fade to 20% red over 15 minutes
      fadeToColor(strip.Color(51, 0, 0), currentTime - alertStartTime, alertDuration); // 20% brightness red
    } else if (currentTime - alertStartTime < 2 * alertDuration) {
      // Fade back to 20% white over the next 15 minutes
      fadeToColor(strip.Color(51, 51, 51), currentTime - alertStartTime - alertDuration, alertDuration); // 20% brightness white
    } else {
      alertActive = false;
      setStripColor(strip.Color(51, 51, 51)); // Set to 20% white
    }
  }
  delay(500); // Placing a small delay here
}

int calendarAlert(String param) {
  alertActive = true;
  alertStartTime = millis();
  return 1;
}

int turnOff(String param) {
  alertActive = false; 
  setStripColor(strip.Color(0, 0, 0)); // Turning LED off
  return 1;
}

void fadeToColor(uint32_t color, unsigned long elapsedTime, unsigned long totalDuration) {
  float ratio = (float)elapsedTime / totalDuration;
  uint8_t red = (uint8_t)(ratio * ((color >> 16) & 0xFF));
  uint8_t green = (uint8_t)(ratio * ((color >> 8) & 0xFF));
  uint8_t blue = (uint8_t)(ratio * (color & 0xFF));
  setStripColor(strip.Color(red, green, blue));
}

void setStripColor(uint32_t color) {
  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, color);
  }
  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