Back to Parent

int servoPin = A2; // Define the pin for the servo
Servo myServo; // Create a Servo object
int servoPos = 0; // Variable to store the servo position

void setup() {
  // Attach the servo to the specified pin
  myServo.attach(servoPin);

  // Register Particle function to control the servo position
  Particle.function("servo", servoControl);

  // Set up a cloud variable to monitor the current position
  Particle.variable("servoPos", servoPos);
}

void loop() {
  // Empty loop; all control happens through Particle function
}

// Function to control the servo position
int servoControl(String command) {
  // Convert command to an integer position value
  int newPos = command.toInt();

  // Constrain position to 0 - 180 degrees
  servoPos = constrain(newPos, 0, 180);

  // Move servo to the new position
  myServo.write(servoPos);

  // Return success
  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