Back to Parent

//Skills Dev III - Exercise 3

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

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812


// Define time calcs for later
int timeIn = 1;
int timeOut = 1;

// Define fade variables for later
int fade_Before = timeIn * 60 * 1000;
int fade_After = timeOut * 60 * 1000;

//Define variable to trigger fade loop
bool fadeTrigger = false;
bool fadeTriggerOut = false;

//Define calculation variables for calculating fade duration
unsigned long lastFade = 0;
int fadeIn = 255;
int fadeOut = 255;

//Define Pushbutton Pin
int buttonPin = D3;
// current state of the button (pressed or not)
int buttonState = 0;     
// previous state of the button
int lastButtonState = 0; 
// Press start time
int startPressed = 0; 
// Press release time
int endPressed = 0;  
// Button Hold Time
int holdTime = 0;        

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

void setup() {
    
    strip.begin();
    strip.show();
    
    pinMode( buttonPin , INPUT_PULLUP);

    
    Particle.function("CalendarAppointment", colorFadeIn);
    Particle.function("Time to fade in (min)", minIn);
    Particle.function("Time to fade out (min)", minOut);
    
    //Start program with lights at white
    uint16_t i;
    setwhite();
}


//Set LED to white
void setwhite( ){
    for (int i = 0; i < strip.numPixels(); i++) {
                strip.setPixelColor(i, 255, 255, 255);
                strip.show(); 
        }
}

//User input fade in time
int minIn(String command) {
    int value = command.toInt();
    //If input is > 0, then set minIn value
        if(value > 0) {
            timeIn = value;
            fade_Before = timeIn * 60 * 1000;
        }
        else{
            return -1;
        }
    return 1;    
}


//User input fade out time
int minOut(String command) {
    int value = command.toInt();
    //If input is > 0, then set minIn value
        if(value > 0) {
            timeOut = value;
            fade_After = timeOut * 60 * 1000;
        }
        else{
            return -1;
        }
    return 1;    
}

int colorFadeIn(String command) {
    //Set fadeTrigger to true starting loop
    fadeTrigger = true;
    //resert values of lastFade, fadeIn and fadeOut to defaults after running
    
    lastFade = 0;
    fadeIn = 255;
    fadeOut = 255;
    return 1;
}


void buttonPress() {
    
    // the button has been pressed
    if (buttonState == LOW) {
        
        startPressed = millis();
        
    } 
    else {
        
      endPressed = millis();
      holdTime = endPressed - startPressed;
      
    }

}



void updateCounter() {
    
    // the button is still pressed
    if (buttonState == LOW) {
        
        holdTime = millis() - startPressed;

    if (holdTime >= 3000) {
        setwhite();
        fadeTrigger = false;
        fadeTriggerOut = false;
      }
  }
}
 
 
 
void loop() {
    
    //Read button state
    int buttonState = digitalRead( buttonPin );

    if (buttonState != lastButtonState) { 
        
     buttonPress(); // button state changed
     
    } else {
        
     updateCounter(); // button state not changed
     
    }
    
    
    
    // When CoorFadeIn activated, activate the fade
    if( fadeTrigger == true ) {
        
        unsigned long now = millis();
        uint16_t i;
        
        // Fade in length determined by user input
        if (fadeIn > 0) {
            if ((now - lastFade) >= fade_Before/256) {
                fadeIn --;
                lastFade = now;
            }
            // Change color of LED's to match fade
            for (int i = 0; i < strip.numPixels(); i++) {
                strip.setPixelColor(i, 255, fadeIn, fadeIn);
                strip.show();
            }
        }
        // When fade completed, begin fade out, length per user input
        if (fadeIn == 0) {
            fadeOut = 255;
            fadeTrigger = false;
            fadeTriggerOut = true;
        }
    }
    //When fadeTriggerOut is activated, start transitioning from red back to white
    if(fadeTriggerOut == true) {
        
        unsigned long now = millis();
        uint16_t i;
        
        // Fade in length determined by user input
        if (fadeOut > 0) {
            if ((now - lastFade) >= fade_After/256) {
                fadeOut --;
                lastFade = now;
            }
            // Change color of LED's to match fade
            for (int i = 0; i < strip.numPixels(); i++) {
                strip.setPixelColor(i, 255, 255-fadeOut, 255-fadeOut);
                strip.show();
            }
        }
        if (fadeOut == 0) {
            fadeTriggerOut = false;
        }
    }
    
}
Click to Expand

Content Rating

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

0