Back to Parent

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




#define PIXEL_PIN D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
uint32_t n = strip.Color(0, 0, 0);
uint32_t R = strip.Color(200, 0, 0);
uint32_t G = strip.Color(0, 200, 0);
//Set up pins for the items used
int x = 0;
int redPin = D3;
int greenPin = D4;
int fanPin = D7;
int heaterPin = D6;
int scentPin = D5;
//button state at begining is false
int pressed = 0;


//============publish
long lastPublishedAt = 0;
int publishAfter = 1000;

// was the scent button pressed
bool scentPressedDisplay = false;
// the time at which the button was pressed
long scentPressedAt = 0;

// how long it should be in the mode for 
int scentPressedTimeout = 10 * 1000;
bool redlevel = false;


void setup() 
{
    strip.begin();
    for( int i = 0; i < strip.numPixels(); i++ )
        {
        strip.setPixelColor(i, n); 
        }
    strip.show();
    pinMode(redPin, INPUT_PULLUP);
    pinMode(greenPin, INPUT_PULLUP);
    pinMode(fanPin, OUTPUT);
    pinMode(heaterPin, OUTPUT);
    pinMode(scentPin, INPUT_PULLUP);
    Particle.subscribe(  "diot2019/team9/redled/" , handleSharedEventRed );
    Particle.subscribe(  "diot2019/team9/greenled/" , handleSharedEventGreen );
    Particle.subscribe(  "diot2019/team9/scent/" , handleSharedEventBlink );
}

void loop() 
{

  if( scentPressedDisplay )
  {
      
      Blink();
      unsigned long now = millis();
      if( scentPressedAt + scentPressedTimeout < now  ){
          scentPressedDisplay = false;
          if(redlevel == false)
          {
              for( int i = 0; i < strip.numPixels(); i++ )
                {
                    strip.setPixelColor( i, G);
                    delay(50);
                }
              strip.show();
          }
          if(redlevel == true)
          {
              for( int i = 0; i < strip.numPixels(); i++ )
                {
                    strip.setPixelColor( i, R);
                    delay(50);
                }
              strip.show();
          }
      }
        
  }else{
      
      int redState = digitalRead( redPin );
      int greenState = digitalRead ( greenPin );
      int scentState = digitalRead ( scentPin);
      if(redState == LOW)
      {
         
         publishMyEventRed();
         delay(100);
      }
      if(greenState == LOW)
      {
         
         publishMyEventGreen();
         delay(100);
      }
      if(scentState == LOW)
      {
         publishMyEventScent();
         delay(100);
      }
      
  }
  delay(100);
  
   if(pressed == 1){
       digitalWrite(heaterPin, HIGH);
       delay(40000);
       digitalWrite(heaterPin, LOW);
   }
   
   if ((pressed == 1) && (scentPressedAt + 30000 < millis())){
      digitalWrite(fanPin, HIGH);
      delay(10000);
      digitalWrite(fanPin, LOW);
      pressed = 0;
   }
   else{
       
   }
  
}




//=============myevent
void publishMyEventRed()
{
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "diot2019/team9/redled/" + System.deviceID();
      Particle.publish( eventName, "data goes here" );
      lastPublishedAt = millis();
  }
}
void publishMyEventGreen()
{
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "diot2019/team9/greenled/" + System.deviceID();
      Particle.publish( eventName, "data goes here" );
      lastPublishedAt = millis();
  }
}
void publishMyEventScent()
{
  if( lastPublishedAt + publishAfter < millis() )
  {
      String eventName = "diot2019/team9/scent/" + System.deviceID();
      Particle.publish( eventName, "data goes here" );
      lastPublishedAt = millis();
  }
}



void handleSharedEventRed(const char *event, const char *data)
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();
    if( eventName.indexOf( deviceID ) != -1 )
    {
      return;
    }
    for( int i = 0; i < strip.numPixels(); i++ )
    {
        strip.setPixelColor( i, R);
        delay(100);
    }
    strip.show();
    redlevel = true;
}

void handleSharedEventGreen(const char *event, const char *data)
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();
    if( eventName.indexOf( deviceID ) != -1 )
    {
      return;
    }    
    for( int i = 0; i < strip.numPixels(); i++ )
    {
        strip.setPixelColor( i, G);
        delay(100);
    }
    strip.show();
    redlevel = false;

}


void handleSharedEventBlink(const char *event, const char *data)
{
    String eventName = String( event ); // convert to a string object
    String deviceID = System.deviceID();
    if( eventName.indexOf( deviceID ) != -1 )
    {
      return;
    }    
    scentPressedDisplay = true;
    scentPressedAt = millis();
    pressed = 1;
}


void Blink( )
{
    x = n;
    for( int i = 0; i <1000; i++ )
    {
        
        x = x + 500;
        strip.setPixelColor( i % 8, x);
        strip.show();
        delay(40);
      
    }
}
Click to Expand

Content Rating

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

0