Back to Parent

Directing Servo Code
Servo MyServo;
int current =0;

void setup()
{
    
MyServo.attach(D2);

Particle.subscribe(  "langley/2019/iot/yellowButtonPressed/", handleSharedEventYellow );
Particle.subscribe(  "langley/2019/iot/blueButtonPressed/", handleSharedEventBlue );
Particle.subscribe(  "langley/2019/iot/redButtonPressed/", handleSharedEventRed );


}

void loop()
{


}


//for YELLOW button

void handleSharedEventYellow(const char *event, const char *data)
{
    
    String eventName = String( event );
    String deviceID = System.deviceID();
    
    //servo start

  current=MyServo.read();
//for loop with delays to slow down movement of servo

   for(int i=current; i>45;i--)
   {
       MyServo.write(i);
       delay(50);
   }
   


    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }

}

//for BLUE button

void handleSharedEventBlue(const char *event, const char *data)
{
    
    String eventName = String( event ); 
    String deviceID = System.deviceID();
    
    //serov start

    
   
   current=MyServo.read();
//for loop with delays to slow down movement of servo

  if(current<60)

   for(int i=current; i<60;i++)
   {
       MyServo.write(i);
       delay(50);
   }
  else  if(current>60)
   for(int i=current; i>60;i--)
   {
       MyServo.write(i);
       delay(50);
   }
    
    //



    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
}

//for RED button

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

    

    String deviceID = System.deviceID();
    
  // MyServo.write(80);

  //  delay(8000);

  //  MyServo.write(4);

    current=MyServo.read();
  //if(current<80)

   for(int i=current; i<80;i++)
   {
       MyServo.write(i);
       delay(50);
   }
// else if(current>80)

//   for(int i=current; i>80;i--)

//   {

//       MyServo.write(i);

//       delay(50);

//   }

 


    if( eventName.indexOf( deviceID ) != -1 ){
      // if we get anything other than -1

      // the event came from this device.

      // so stop doing stuff

      return;
    }

}
Click to Expand

Content Rating

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

0