Back to Parent

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

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

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

int fsrPin = A0;
// Create a variable to hold the FSR reading
int fsrReading = 0;
// Define a pin we'll place an LED on
int ledPin = D0;
// Create a variable to store the LED brightness.
int ledBrightness = 0;
int buttonPin = D2;
int thresholdValue = 1000;
int shouldRun = 0; // It serves like Bool, to turn on and off the neopixel light


    void setup()
{
  // Set up the LED for output
  pinMode(ledPin, OUTPUT);
  // Create a cloud variable of type integer
  Particle.variable("force", &fsrReading, INT);
  
  
  strip.setBrightness( 30 );
  strip.begin();
  strip.show();
  
  Particle.subscribe("sleepstar/2p", ChangeLight); 
  
  Particle.variable("Bool", &shouldRun, INT);
  
}

    void loop()
{
    doorknob();
  

    if( shouldRun == 1 ){
    breathe2(0);
    delay(5000);
    }
    else  {
    doorknob(); 
    }
}


    void doorknob(
{
    
  fsrReading = analogRead(fsrPin);
  //Particle.publish("test",;fsrReading");
  // Map this value into the PWM range (0-255)
  // and store as the led brightness
  if((fsrReading > thresholdValue) && (shouldRun == 0) )
  {
  Particle.publish("sleepstar/2p", "run");
   
  colorWipe(50,50);
   
  }
  else if ((fsrReading <= thresholdValue) && (shouldRun == 0)){ 
    
      breathe(50);
 }

}

    void ChangeLight(const char *event, const char *data)
{
  String dataStr = String(data);
  if( dataStr.equals( "sleep_t" )) {
    shouldRun = 1;
  }
    else if (dataStr.equals( "sleep_ts" )){
        
        shouldRun = 0;
    }
}

  /* Calc the sin wave for the breathing white led */
  float breathe( uint8_t wait ){

  float val = (exp(sin(millis()/2000.0*M_PI)) - 0.36787944)*108.0;
  Serial.println( val );

  uint16_t i;
  uint32_t c = strip.Color(val, val, val );

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, c );
  }
  strip.show();
  delay(wait);

  }
  
    /* Calc the sin wave for the breathing white led */
  float breathe2( uint8_t wait ){

  float val = (exp(sin(millis()/2000.0*M_PI)) - 0.36787944)*108.0;
  Serial.println( val );

  uint16_t i;
  uint32_t c = strip.Color(val, 225, val );

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, c );
  }
  strip.show();
  delay(wait);

  }
  

  void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    uint32_t c = strip.Color(0, 255, 255);
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
Click to Expand

Content Rating

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

0