Back to Parent

Winding Servo Code
// int servoPin = A1;
int servoPin2 = A1;
// Servo myServo;
Servo myServo2;
// int servoPos = 0;
int servoPos2 = 0;

bool run = false;

// uint32_t w = strip.Color(0, 0, 0);
//     //setting colour based on the handler event (blue/green/yellow)
// uint32_t c = strip.Color(255, 255, 255);


// 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;


void setup() {

    // myServo.attach( servoPin );
    myServo2.attach( servoPin2 );
    
    //myServo2.write ( 93.5 );
    
    //my retrieve (Flavor 1)
    Particle.subscribe("langley/2019/iot/yellowButtonPressed/", handleSharedEvent );
    
    // my retrieve (Flavor 2)
    Particle.subscribe("langley/2019/iot/blueButtonPressed/", handleSharedEvent );
    
    //my retrieve (Flavor 3)
    Particle.subscribe("langley/2019/iot/redButtonPressed/", handleSharedEvent );
    
    Particle.variable("run", run);

    // turnonLight();

}

void loop() {
    
    if (run) {
        
        myServo2.attach( servoPin2 );
        
        delay(200);
        
        //up
        myServo2.write( 115 );
        //for counterclockwise
        delay(8400);
        //delay(8000); //clockwise
        
        //down
        myServo2.write( 72 );
        delay(8350);
        //myServo2.write( 93.5 );
        
        myServo2.detach();
            
        run = false;
        
        finishedEvent();
    }
}

void handleSharedEvent(const char *event, const char *data)
{

    String eventName = String( event ); 
    
    String deviceID = System.deviceID();
    
    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
    
    run = true;

}

void finishedEvent()
{
  // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
   
      String eventName = "langley/2019/iot/eventFinished/" + System.deviceID();

      // now we have something like "diot/2019/paired/0123456789abcdef"
      // and that corresponds to this devices info

      // then we share it out
      Particle.publish( eventName, "finished" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      lastPublishedAt = millis();
  }

}
Click to Expand

Content Rating

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

0