Back to Parent

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

// set up neopixel: library, pixel COUNT, PIN and TYPE
#include "neopixel.h"
#define PIXEL_PIN A2
#define PIXEL_PIN2 A5
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN2, PIXEL_TYPE);


long lastPublishedAt = 0;
// time delay before we should publish a new event
int publishAfter = 5000;

// add switch
int switchPin = D3;
int buttonState = -1;

int blinked = 0;

// add servo
Servo myServo;
int servoPin = A3;
int servoPos = 0;

// variable to store availability
String availability = "unavailable";


void setup()
{
    pinMode(switchPin , INPUT_PULLUP);
    myServo.attach(A3);
    
    //******UPDATE EVENTNAME*******//
    Particle.subscribe(  "missU" , handleSharedEvent );
    
    strip2.begin();
    strip2.show(); 
    

}

void loop(){
    
    publishMyEvent();
    
    // turn on roof light
    for(int i=0; i < strip2.numPixels(); i++){
        strip2.setPixelColor(i, 255, 153, 51);
        strip2.setBrightness(40);
        strip2.show();
    }
    delay(100);

    // delay for a bit
    delay(100);
    
    int buttonReading = digitalRead(switchPin);
    if(buttonReading == HIGH){
        if(blinked == 0){
            delay(500);
            for(int i2=0; i2 < strip2.numPixels(); i2++){
                strip2.setPixelColor(i2, 255, 153, 51);
                strip2.setBrightness(10);
                strip2.show();
            }
            delay(400);
            for(int i2=0; i2 < strip2.numPixels(); i2++){
                strip2.setPixelColor(i2, 255, 153, 51);
                strip2.setBrightness(40);
                strip2.show();
            }
            delay(400);
            for(int i2=0; i2 < strip2.numPixels(); i2++){
                strip2.setPixelColor(i2, 255, 153, 51);
                strip2.setBrightness(10);
                strip2.show();
            }
            delay(400);
            for(int i2=0; i2 < strip2.numPixels(); i2++){
                strip2.setPixelColor(i2, 255, 153, 51);
                strip2.setBrightness(40);
                strip2.show();
            }
            delay(400);
            for(int i2=0; i2 < strip2.numPixels(); i2++){
                strip2.setPixelColor(i2, 255, 153, 51);
                strip2.setBrightness(10);
                strip2.show();
            }
            delay(400);
            for(int i2=0; i2 < strip2.numPixels(); i2++){
                strip2.setPixelColor(i2, 255, 153, 51);
                strip2.setBrightness(40);
                strip2.show();
            }
            delay(400);
        }
    blinked = 1;
}

}


void publishMyEvent(){
    
    // check switch status
    int buttonReading = digitalRead(switchPin);
    
    // check that it's been 10 secondds since our last publish
    if( buttonReading != buttonState ){
             //******UPDATE EVENTNAME*******//
        String eventName = "missU" + String(System.deviceID());
    
        // then we share it out
        // if switch is on, publish an event
        if(buttonState == LOW){
            availability = "available";
            blinked = 0;
            Particle.publish(eventName, "available");
        }else{
            availability = "unavailable";
            Particle.publish(eventName, "unavailable");
        }   
    }


    buttonState = buttonReading;

    //if(lastPublishedAt + publishAfter < millis()){
        // we just pubished so capture this.
    //    lastPublishedAt = millis();
    
    //}

}

// Our event handdle requires two bits of information
// A character array that consists of the event name
// A character array that contains the data published in the event we're responding to.
void handleSharedEvent(const char *event, const char *data){
    
    String eventName = String(event); 
    availability = data;

    String deviceID = System.deviceID();
    
    // device id = e00fce688578dddab57c1e9d
    // event = "missUe00fce688578dddab57c1e9d"
    // eventV2 = "missUe00fce68d1958060a1d74bac"
    
    if( eventName.indexOf( deviceID ) != -1 ){

      return;
    }
    
    turnOnAvailabilityLight(availability);
    servoControl(availability);

}

// function to turn on the window light based on availability(event data)
int turnOnAvailabilityLight(String command){

    if(command == "available"){
        for(int i=0; i< strip.numPixels(); i++){
            strip.setPixelColor(i, 255, 153, 51);
            strip.setBrightness(10);
            strip.show();
            delay(100);
        }
        return 1;
        
    }else{
        for(int i=0; i< strip.numPixels(); i++){
            strip.setPixelColor(i, 0, 0, 0);
            strip.show();
            delay(100);
        }
        return -1;
    }
}

// function to turn on servo based on availability(event data)
int servoControl(String command){
    
    if(command == "available"){
        for(int i=0; i<70; i+=2){
          myServo.write(i);
          delay(30);
        }
        return 1;
        }else{
        myServo.write(0);
        return -1;
    }

}
Click to Expand

Content Rating

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

0