Back to Parent

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

int now = 0;
unsigned long tStartedAt =0;
bool tRunning = FALSE;
int duration = 10*1000;
int z=255;

uint32_t c;


uint32_t k;
int buttonPin = D3;
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 from this device

// ------------------- NEOPIXEL
#define PIXEL_COUNT 1
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
  Particle.subscribe(  "diot/2019/paired/sthfoodout/" , handleSharedEvent );
  pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() { //publish my event you'll want some more complex stuff here

    int buttonState = digitalRead( buttonPin );
    
    c = strip.Color(0, 0, 255); // Blue
    
    if( buttonState == LOW ){  // turn the LED ON
    tStartedAt = millis();
    tRunning = TRUE;
    publishMyEvent();
    rainbow(c);
    delay(100); // delay for a bit
    }
    
    int now = millis();
    int j=now-tStartedAt;
    if(tRunning && j%1000<100) {
        z=z-255/10;
        k = strip.Color(0, 0, z);
        rainbow(k);
    }
    
    if (j>duration) {
        k = strip.Color(0,0,0);
        rainbow(k);
        tRunning=FALSE;
    }
}

void publishMyEvent(){
     if( lastPublishedAt + publishAfter < millis() ) {
         String eventName = "diot/2019/paired/sthfoodout/" + System.deviceID();
         Particle.publish( eventName, "food is available" );
         lastPublishedAt = millis();
         }
    }

void handleSharedEvent(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;
    } else {
        tStartedAt = millis();
        tRunning = TRUE;
        rainbow(c);
    }
    
    
    // c = strip.Color(255, 0, 0);
    // rainbow(c);
    // c = strip.Color(0, 255, 0);
    // rainbow(c);
    // c = strip.Color(255, 255, 255);
    // rainbow(c);
}



void rainbow(uint32_t c){
    for( int i = 0; i < strip.numPixels(); i++ ){
     strip.setPixelColor(i, c); // i= which led to light up, c= set a color 
     strip.show();
     delay( 100 ); // update each pixel one by one, and wait 100 ms in between each update
     }
  delay( 100 );
   
//   for( int i = strip.numPixels()-1; i >= 0 ; i-- ){
//      c = strip.Color(0, 0, 0); // White
//      strip.setPixelColor(i, c); // set a color 
//      strip.show();
//      delay( 100 ); // update each pixel one by one, and wait 100 ms in between each update
//   }
//   delay( 100 );
}
Click to Expand

Content Rating

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

0