Back to Parent

//This code is duplicated across two particles

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

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

int fsrPin = A0;
int fsrReading = 0;
int toggle = 0;


#define PIXEL_PIN D2
#define PIXEL_COUNT 7
#define PIXEL_TYPE WS2812

#define PIXEL_PIN2 D4
#define PIXEL_COUNT2 7
#define PIXEL_TYPE2 WS2812

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Adafruit_NeoPixel strip2(PIXEL_COUNT2, PIXEL_PIN2, PIXEL_TYPE2);

//flag to keep track of fade direction up or down
bool breathe_up = true;
//has the blanked been warmed up yet?
bool blanketWarmed = false;
//is my spouse in their bed?
bool spouseInBed = false;
int i = 0;
int publish_time = millis();
int start_time = 0;
void publish_event();
int buttonPin = D7;
int heatingPin = D5;
void turn_heat_on();
void turn_heat_off();
void setSpouseInBed(const char *event, const char *data);
void unloadingSequence();
bool programON = true;

void setup()
{
    Particle.subscribe( "inbed3000",setSpouseInBed );  // From all devices!
    pinMode( heatingPin, OUTPUT);
    
    pinMode(fsrPin, INPUT_PULLUP);
    pinMode(buttonPin, INPUT_PULLUP);
    //pinMode(PIXEL_PIN, OUTPUT);
    Particle.variable("fsr", fsrReading);
    Particle.variable("spouseInBed",spouseInBed);
    Particle.variable("blanketWarmed",blanketWarmed);
    
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
    strip2.begin();
    strip2.show();
   
    //loadingSequence();
    //delay(1000);
    strip2.clear();
    strip2.show();
    strip.clear();
    strip.show();
}



void loop() {
fsrReading = analogRead(fsrPin);

    //if you step on the rug, warm the blanket 
    if (fsrReading>2000 and blanketWarmed == false) 
    { 
         loadingSequence();
         start_time = millis();
         blanketWarmed = true;
         turn_heat_on();
         programON = true;
    }
    //if you step on the rug after you've been in bed x hours, cool down the bed
    //turn off procedure if program has ran startup sequence and 10 seconds have passed
    else if (fsrReading >2000 and blanketWarmed and millis() - start_time > 10000)
    {
        blanketWarmed = false;
        unloadingSequence();
        programON = false;
    }
    
    //if I'm in bed, tell my spouse's bed
    if (digitalRead(buttonPin) == LOW and blanketWarmed == true and programON) 
    {
        publish_event();
    }
    else if (programON)
    {
        //if its been 5 seconds since the last published event
        if(millis() - publish_time > 15000)
        {
            spouseInBed = false;
        }
    }
    

    if (blanketWarmed == true and spouseInBed == false and programON )
    {
        //blue is the "ready" state
        for(int j=1; j<strip.numPixels(); j++) {
          strip.setPixelColor(j, strip.Color(0,0,255)); 
          strip.show();
          strip2.setPixelColor(j, strip.Color(0,0,255));
          strip2.show();
        }
        
        //reset the breathing sequence back to starting state
        //starting state should be at 255-blue and fading down
        i=255;
        breathe_up=false;
    }
    else if (blanketWarmed == true and spouseInBed == true and programON)
    {
        //breathe to indicate your spouse is in their bed
        breathAll();
    }
    
    delay(10);
}

//turn the heating pad on
void turn_heat_on()
{
     digitalWrite( heatingPin, HIGH ); 
}
void turn_heat_off()
{
     digitalWrite( heatingPin, LOW ); 
}

//have spam limits on the events
void publish_event()
{
    if(millis() - publish_time > 15000)
    {
        Particle.publish("inbed3000");
        Particle.publish("inbed1000");
        publish_time = millis();
    }
}

//set the spouseinbed flag
void setSpouseInBed(const char *event, const char *data)
{
    spouseInBed = true;
}

//simulate the warming up sequence
void loadingSequence()
{
        strip2.clear();
        strip2.show();
        strip.clear();
        strip.show();
        for(int j=1; j<strip.numPixels(); j++) {
          strip.setPixelColor(j, strip.Color(255,0,0)); 
          strip.show();
          strip2.setPixelColor(j, strip.Color(255,0,0)); 
          strip2.show();
          delay(500);
        }
}

//simulate the warming down sequence
void unloadingSequence()
{
        for(int j=1; j<strip.numPixels(); j++) {
          strip.setPixelColor(j, strip.Color(0,0,0)); 
          strip.show();
          strip2.setPixelColor(j, strip.Color(0,0,0)); 
          strip2.show();
          delay(500);
        }
}

//breathe the LEDs
void breathAll() {
    if (breathe_up)
    {
        i = i + 1;
         for(int j=1; j<strip.numPixels(); j++) {
          strip.setPixelColor(j, strip.Color(0,0,i)); 
          strip2.setPixelColor(j, strip.Color(0,0,i)); 
        }
        strip.show();
        strip2.show();
        if(i>=255)
        {
            breathe_up = false;
        }
        
    }
    else
    {
        i = i - 1;
         for(int j=1; j<strip.numPixels(); j++) {
          strip.setPixelColor(j, strip.Color(0,0,i)); 
          strip2.setPixelColor(j, strip.Color(0,0,i)); 
        }
        strip2.show();
        strip.show();      

        if(i<=0)
        {
            breathe_up = true;
        }
        
    }
    
}
Click to Expand

Content Rating

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

0