Back to Parent

int servoPin = A3;
Servo myServo;
int servoPos = 0;

int buttonPin = D3;

void setup() {
  myServo.attach( servoPin );
  Particle.function("servo", servoControl);
  Particle.variable(  "servoPos" , &servoPos , INT );
  pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
  myServo.write( 0 );
}

void loop() {
    int buttonState = digitalRead( buttonPin );
    if( buttonState == LOW )
  {
    // turn the LED On
   Particle.publish( "buttonpushed");
   String degree = "90";
   int pos = degree.toInt();
   servoPos = constrain( pos, 0 , 180);
   myServo.write(pos);
   delay( 5000 );
   }

}


int servoControl(String command)
{
    // Convert
  int newPos = command.toInt();
  // Make sure it is in the right range
  // And set the position
  servoPos = constrain( newPos, 0 , 180);

  // Set the servo
  myServo.write( servoPos );
  
  // done
  return 1;
}
Click to Expand

Content Rating

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

0