Back to Parent

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

#include <math.h>

int button = D3; //button is read from D3
int ledpin = D4; //LED output is D4

unsigned long now = 0; //variable to keep track of time
unsigned long pressed = 0; //variable to keep track of time for buttonState
unsigned long eventTimer = 0; //variable to keep track of time for events

int buttonState = 0; //variable to keep track of button presses
long lastPublishedAt = 0; // This value will store the last time we published an event
int publishAfter = 1000; // this is the time delay before we should publish a new event
int eventState = 0; //variable to keep track of incoming events
int red = 0;//initializes red to 0
int green = 0;//initializes green to 0
int blue = 0;//initializes blue to 0


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

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


void setup()
{
    Particle.subscribe(  "diot/2019/paired/LOVE" , getLove ); //subscription to shared events
    Particle.subscribe(  "diot/2019/paired/SOS" , getHelp );
    Particle.subscribe(  "diot/2019/paired/BREATHE" , justBreathe );
    pinMode(button, INPUT_PULLUP);
    pinMode(ledpin, OUTPUT);
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
}

void loop()
{
    int buttonRead = digitalRead (button); 
    delay(10);
    if (buttonRead == LOW) //looking for button press
    {
        pressed = millis(); //remembers when the button was pressed    
        light(); //LED confirms button pressed
//        lightColor();
//        confirm();
        buttonState++; //Advances the button state for double or tripple press
        delay(10);
    }
    
    now = millis(); //to compare current time to last time button pressed
    
    if (eventState==1) { breatheYellow(); }
    if (eventState==2) { breatheRed(); }
    if (eventState==3) { breatheBlue(); }
    if (buttonState==2) { light2(); }
    if (buttonState==3) { light3(); }
    if (buttonState>=3) { buttonState=3; }
    if (now-pressed>=2000) { buttonState=0; digitalWrite (ledpin, LOW);} //resets the button state if >2 seconds have elapsed
    if (eventState==1 || eventState==2 || eventState==3){eventTimer=millis(); }
//    if (now-eventTimer>=60000) {eventState=0; }//this line prevents everything from working
}   


// void lightColor()
// {
//     if (buttonState <= 2000){red=255; green=255; blue=0;}
//     if (buttonState <= 2000 && buttonState <= 4000){red=255; green=255; blue=0;}
//     else {red=255; green=255; blue=0;}
//}



// float confirm()
// {
//     uint32_t c = strip.Color(red, green, blue);
//     int i=0;
//     for(i=0; i< strip.numPixels(); i++) 
//     {
//         strip.setPixelColor(i, c );
//     }
//     strip.show();
    
// }

void light()//causes LED to blink once to confirm loveSent
{
    sendLove();
    digitalWrite (ledpin, HIGH);
    delay(100);
    digitalWrite (ledpin, LOW);
    delay(100);

}

void light2()//causes LED to blink rapidly to confirm SOSsent
{
    sendSOS();
    digitalWrite (ledpin, HIGH);
    delay(100);
    digitalWrite (ledpin, LOW);
    delay(100);

}

void light3()//causes the led to blink more rapidly to confirm justBreathe has been sent
{
    sendBreathe();
    digitalWrite (ledpin, HIGH);
    delay(50);
    digitalWrite (ledpin, LOW);
    delay(50);
}

void sendLove(){ //sends shared event 1
  if( lastPublishedAt + publishAfter < millis() )
  {  // check that it's been 10 secondds since our last publish
      String eventName = "diot/2019/paired/LOVE" + System.deviceID();
      Particle.publish( eventName, "love sent" );
      lastPublishedAt = millis();
  }
}

void sendSOS(){ //sends shared event 2
  if( lastPublishedAt + publishAfter < millis() )
  {  // check that it's been 10 secondds since our last publish
      String eventName = "diot/2019/paired/SOS" + System.deviceID();
      Particle.publish( eventName, "SOS sent" );
      lastPublishedAt = millis();
  }
}

void sendBreathe(){ //sends shared event 2
  if( lastPublishedAt + publishAfter < millis() )
  {  // check that it's been 10 secondds since our last publish
      String eventName = "diot/2019/paired/BREATHE" + System.deviceID();
      Particle.publish( eventName, "Just Breathe" );
      lastPublishedAt = millis();
  }
}

void getLove(const char *event, const char *data) //receives shared event 1
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();    // device id = 0123456789abcdef
    event = "diot/2019/paired/LOVE";

    if( eventName.indexOf( deviceID ) != -1 )
    {
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
    return;
    }
    eventState=1;
}  

void getHelp(const char *event, const char *data) //receives shared event 2
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();    // device id = 0123456789abcdef
    event = "diot/2019/paired/SOS";//

    if( eventName.indexOf( deviceID ) != -1 )
    {
       //if we get anything other than -1
       //the event came from this device.
       //so stop doing stuff
    return;
    }
    eventState=2;
} 

void justBreathe(const char *event, const char *data) //receives shared event 3
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();    // device id = 0123456789abcdef
    event = "diot/2019/paired/SOS";//

    if( eventName.indexOf( deviceID ) != -1 )
    {
       //if we get anything other than -1
       //the event came from this device.
       //so stop doing stuff
    return;
    }
    eventState=3;
}  

float breatheRed()
{
    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, 0, 0);
    for(i=0; i< strip.numPixels(); i++)
        {
        strip.setPixelColor(i, c );
        }
    strip.show();
}

float breatheYellow()
{
    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, 0);
    for(i=0; i< strip.numPixels(); i++) 
    {
        strip.setPixelColor(i, c );
    }
    strip.show();
}

float breatheBlue()
{
    float val = (exp(sin(millis()/2000.0*M_PI)) - 0.36787944)*108.0;
    Serial.println( val );
    uint16_t i;
    uint32_t c = strip.Color(0, 0, val );
    for(i=0; i< strip.numPixels(); i++) 
    {
        strip.setPixelColor(i, c );
    }
    strip.show();
}
Click to Expand

Content Rating

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

0