Skills Dev III

Made by Ben Oppenheim ·

Ambient orb that has customizable time/duration of fading and pushbutton override if held for 3 seconds.

Created: November 17th, 2022

0

Outcome

0
//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
0

Process

For this project I created an ambient orb that responds to a calendar appointment through IFTT and is customizable in a number of ways. When a calendar appointment is 15 minutes away, the LED's slowly respond to the change, fading from white to red and then back to white. A user can input how long they would like the period of fade in and fade out to be. Additionally, I added a pushbutton to the circuit so that a user can override the color fade. If this button is held for 3 or more seconds the fade is dismissed and returns to the default white.

This was a very challenging project to get the code to work as intended and required a lot of trial and error.

0

Reflection

I will continue to try to improve my coding skills as this is the piece that is holding me back the most. I usually feel like I understand what I need to do for each of the steps, but am struggling to implement the steps and have the program act as intended.

x
Share this Project

This project is only accessible by signed in users. Be considerate and think twice before sharing.


Courses

About

Ambient orb that has customizable time/duration of fading and pushbutton override if held for 3 seconds.