Back to Parent

#include <neopixel.h>

#define PIXEL_PIN D3
#define PIXEL_COUNT 8 //for stick, 16 for ring
#define PIXEL_TYPE WS2812B //for 4pin neopixels, WS2812/WS2813 for 6pin

Adafruit_NeoPixel strip = Adafruit_NeoPixel( PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int buttonPin = D2;
bool calAlert = false;
unsigned long meetingstartTime= 0;
unsigned long totalFadeInTime = 120000;   //1200000
unsigned long initialFadeInTime = 30000;  //300000
unsigned long middleFadeInTime = 60000;    //600000
unsigned long FadeOutTime = 30000;    //300000

unsigned long timeSinceCalAlert= 0;
int initialFadeIn = 0;
int middleFadeIn = 0;
int finalFadeOut = 0;

// Function to take fadein time as input from user 
int userfadeInTime(String command)
{ 
    int input = 0;
    input = command.toInt();
        if(input > 0) 
        {
            //calAlert = true;
            
            // Divide the user fadeintime into initial, middle and final fade out time by 25%, 50% and 25% respectively.
            totalFadeInTime = input * 60 * 1000;
            initialFadeInTime= 0.25 * totalFadeInTime;
            middleFadeInTime= 0.5 * totalFadeInTime;
            FadeOutTime = 0.25 * totalFadeInTime;
            return 1;
        }
        else
        {
            return 0;
        }
}
 
 

// this function automatically gets called upon getting a POST request
int googleCalAlert(String command)
{
    Serial.println("Calendar Alert Status in cloud function:");
        Serial.println(calAlert);

    meetingstartTime = millis(); 
    if(command == "1"){
        calAlert = !calAlert;
        Serial.println(calAlert);
        meetingstartTime = millis(); 
        Serial.println(meetingstartTime);
    }
    if(calAlert == true){
        return 1;
    }
    return 0;
}


void setup() {
    
    pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
    strip.begin();
    strip.show();
    Serial.begin( 9600 );         // Setups Serial Communications
    //Cloud Functions
    Particle.function("Google_Calendar_Alert", googleCalAlert);
    Particle.function("Please input the fade-in time in mins", userfadeInTime);
    
    Particle.variable("Time since the meeting alert received in mins", timeSinceCalAlert);
}

int buttonpressStartTime =0;
int buttonpressTotalTime =0;
bool buttonPressed =false;


void loop() {
    
    int buttonState = digitalRead( buttonPin );
    if( buttonState == LOW )
    {
        Serial.println("buttonpress status");
        Serial.println(buttonState);
        buttonPressed =true;
        Serial.println("buttonpressed  or not ");
        Serial.println(buttonPressed);
        buttonpressStartTime = millis();
  }
  
    if(buttonState == HIGH & buttonPressed == true){
        buttonpressTotalTime = millis()- buttonpressStartTime;
        Serial.println(buttonState);
        Serial.println(buttonpressTotalTime);
        if(buttonpressTotalTime>= 3000)
        {
        calAlert = false;
            for(int i=0; i<strip.numPixels(); i++)
            {
                strip.setPixelColor(i, 0, 0 , 0);
            }
            strip.show();
    buttonPressed = false;        
    }
    }

    timeSinceCalAlert= millis()- meetingstartTime;
    
    if(calAlert == true & initialFadeIn <= 255)
    { // rgb 0 0 255 for blue
        int initialDifference=0;
        initialDifference = millis()-meetingstartTime;
        initialFadeIn = map(initialDifference, 0, initialFadeInTime, 0, 255);
        Serial.println(" In initial fade in Status");
        Serial.println("Initial FadeInValue");
        Serial.println(initialFadeIn);
        for(int i=0; i<strip.numPixels(); i++)
            {
                strip.setPixelColor(i, 255- initialFadeIn, 255- initialFadeIn, 255);
            }
        strip.show();
    }
    
    else if(calAlert == true & middleFadeIn <= 255 & initialFadeIn >= 255)
    { // rgb 255 0 0 for red
        int middleDifference=0;
        middleDifference = millis()- (meetingstartTime+ initialFadeInTime);
        middleFadeIn = map(middleDifference, 0, middleFadeInTime, 0, 255);
        Serial.println(" In middle millis value is");
        int a = millis();
        //Serial.println(meetingTrigger);
        Serial.println(a);
        Serial.println(" In middle fade in Status");
        //Serial.println(meetingTrigger);
        Serial.println("middle FadeInValue");
        Serial.println(middleFadeIn);
        for(int i=0; i<strip.numPixels(); i++)
            {
                strip.setPixelColor(i, middleFadeIn , 0 , 255- middleFadeIn);
            }
        strip.show();
    } 
    else if(calAlert == true & finalFadeOut <= 255  & middleFadeIn >= 255 & initialFadeIn >= 255)
    { // rgb 255 255 255 for white
        int finalDifference=0;
        finalDifference = millis()- (meetingstartTime + middleFadeInTime + initialFadeInTime) ;
        finalFadeOut = map(finalDifference, 0, FadeOutTime, 0, 255);
        Serial.println(" In Final fade in Status");
        Serial.println("Final FadeInValue");
        Serial.println(finalFadeOut);
        for(int i=0; i<strip.numPixels(); i++)
            {
                strip.setPixelColor(i, 255, finalFadeOut, finalFadeOut);
            }
        strip.show();
    } 
    
    if(finalFadeOut >= 255){
        calAlert = false;
        initialFadeIn = 0;
        middleFadeIn = 0;
        finalFadeOut = 0;
        for(int i=0; i<strip.numPixels(); i++)
            {
                strip.setPixelColor(i, 0, 0 , 0);
            }
            strip.show();
    }
    strip.show();
  //  delay(400);
    }
Click to Expand

Content Rating

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

0