Back to Parent

long lastPublishedAt = 0;
int publishAfter = 100;

bool motorOn = false;

int button = D3;
int servoPin = A3;

Servo myServo;
int servoPos = 0;

void setup()
{
  
    myServo.attach( A3 );
    pinMode(button, INPUT_PULLUP);
    pinMode(servoPin, OUTPUT);

  Particle.subscribe( "diot2021" , handleSharedEvent );

}

void loop()
{

    publishMyEvent();
    
    if (motorOn == true) {
        for (servoPos = 0; servoPos <= 180; servoPos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
            myServo.write(servoPos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
        }
        for (servoPos = 180; servoPos >= 0; servoPos -= 1) { // goes from 180 degrees to 0 degrees
            myServo.write(servoPos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
        }
        
        motorOn = false;
    }
    
    delay(100);
}


void publishMyEvent()
{

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
    
      String eventName = "diot2021";

      if (digitalRead(button) == LOW){
          
            Particle.publish( eventName );
      }
      
      
      lastPublishedAt = millis();
  }

} 

void handleSharedEvent(const char *event, const char *data)
{
    
    String eventName = String( event ); // convert to a string object
    

    String deviceID = System.deviceID();

    // device id = 0123456789abcdef
    // event = "diot/2019/paired/0123456789abcdef"

    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
    
    else {
        motorOn = true;
    }
    
}
Click to Expand

Content Rating

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

0