Back to Parent

//#include <Servo.h>
int servoPin = D1;
Servo s1;   //declaration
int servoPos = 0;
// This value will store the last time we published an event
long lastPublishedAt = 0;
// this is the time delay before we should publish a new event
// from this device
int publishAfter = 10000;
//declare pins for the flex sensor
int flexPin = A0;
void setup()
{
  pinMode(D1, OUTPUT);
  pinMode(A0, INPUT);
  s1.attach(D1);
  Serial.begin(9600);
  // attaches the servo on the D0 pin to the servo object
  //enter input output for the flex senspr
  Particle.subscribe("smile/diot/paired/", servoHandler, "3f003d000b47343438323536");
}
void loop()
{
  s1.write(50);
  int x = analogRead(A0);
  Serial.println(x);
  if (x>= 2700)
  {
    publishMyEvent();
  }
  delay(100);
}
 void publishMyEvent()
 {
  //  s1.write(20);
  //  delay(200);
  //  s1.write(50);
  //  delay(500);
   if( lastPublishedAt + publishAfter < millis() )
   {
     String eventName = "smile/diot/paired/" + System.deviceID();
     Particle.publish( eventName, "smile", PUBLIC );
     lastPublishedAt = millis();
   }
 }
void servoHandler (const char *event, const char *data)
{
  //convert to string
  String eventName = String(event);
  //check if event name contains device id
  String deviceID = System.deviceID();
  //if we get any value other than -1 the event
  //came from this device
  if( eventName.indexOf( deviceID ) != -1 )
  {
   return;
  }

    s1.write(20);
    delay(200);
    s1.write(50);
  
}
Click to Expand

Content Rating

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

0