// set up neopixel: library, pixel COUNT, PIN and TYPE
#include "neopixel.h"
#define PIXEL_PIN A2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
long lastPublishedAt = 0;
// time delay before we should publish a new event
int publishAfter = 5000;
// add switch
int switchPin = D3;
int buttonState;
// 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 );
}
void loop(){
publishMyEvent();
// delay for a bit
delay(100);
}
void publishMyEvent(){
// check switch status
buttonState = digitalRead(switchPin);
// check that it's been 10 secondds since our last publish
if(lastPublishedAt + publishAfter < millis()){
//******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";
Particle.publish(eventName, "available");
}else{
availability = "unavailable";
Particle.publish(eventName, "unavailable");
}
// we just pubished so capture this.
lastPublishedAt = millis();
}
}
// Our event handle 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"
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"){
myServo.write(100);
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!
You must login before you can post a comment. .