// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
//Set pixel COUNT, PIN, and TYPE
#define PIXEL_COUNT 8
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812
// Setup Pixel
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// Define switch
int switchPin = D3;
int switchState = LOW;
// Define duration of fade variable to 15 minutes (1000 = 1 second, 60 seconds = 1 min, 15 min)
int durationOfFade = 1000 * 60 * .25;
// Define variable to hold the start time
long timeStartedAt = -1;
long resetTimeStartedAt = -1;
// Define variable to hold value for whether timer has started or not (IFTTT Function)
bool timerStarted = FALSE;
bool resetTimerStarted = FALSE;
void setup() {
// Sets pin as input
pinMode( switchPin , INPUT_PULLUP);
// Set pixel to off state
strip.begin();
strip.show();
//Set up IFTTT Calendar Function
Particle.function("eventAlert", handleCalendarAlert);
Serial.println("Setup Complete");
}
void loop() {
int switchState = digitalRead( switchPin );
//Define whether Pixel should be on or off
if (switchState == LOW) {
if (timerStarted == TRUE) { //Determine whether timer has started or not
long timeNow = millis(); //Get the time now
long timeElapsed = timeNow - timeStartedAt; //Define how much time has elapsed
Serial.print(timeElapsed);
if (timeElapsed < durationOfFade) {
int colorValue = map(timeElapsed, 0, durationOfFade, 0, 255); //Controls color fade. Starting point is 0 to max durationOfFade value, which is then mapped to 0-255
Serial.print("Color Value: ");
Serial.print(colorValue);
int r = colorValue;
int g = 0;
int b = 0;
for( int i = 0; i <strip.numPixels(); i++) {
strip.setPixelColor( i, r, g, b);
}
strip.show();
}
else {
timerStarted = FALSE; //reset timer
resetHandler(1);
Particle.publish("timerFinished");
Serial.println("timerFinished ");
}
}
else if (resetTimerStarted == TRUE) {
Serial.print("\t");
Serial.println("Reset Timer Running");
long timeNow = millis();
long timeElapsed = timeNow - resetTimeStartedAt;
if (timeElapsed < durationOfFade) {
int colorValue = map(-timeElapsed, 0, durationOfFade, 0, 255); //Controls color fade. Starting point is 0 to max durationOfFade value, which is then mapped to 0-255
Serial.print("Color Value: ");
Serial.print(colorValue);
int r = colorValue;
int g = 0;
int b = 0;
for( int i = 0; i <strip.numPixels(); i++) {
strip.setPixelColor( i, 255, 255-colorValue, 255-colorValue);
}
strip.show();
}
else {
resetTimerStarted = FALSE; //reset timer
Particle.publish("timerFinished");
Serial.println("Reset Timer Finished ");
}
}
else {
delay(1000);
powerOn();
}
}
//TURN LIGHTS OFF BECAUSE SWITCH IS OFF
else {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor (i, 0, 0, 0);
}
strip.show();
}
}
// Define IFTTT Calendar Function Value
int handleCalendarAlert ( String cmd) {
if(cmd == "TRUE") {
timeStartedAt = millis();
timerStarted = TRUE;
return 1;
}
return -1;
}
int resetHandler (int value) {
if (value == 1) {
resetTimeStartedAt = millis();
resetTimerStarted = TRUE;
Particle.publish("resetTimerTriggered");
Serial.println("resetTimerTriggered");
return 1;
}
return -1;
}
void powerOn () {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 255, 255);
strip.show();
}
}
Click to Expand