Learn how to use the basics of the Servo Library and interact with a Servo.
If you are using a basic servo that does not require external power, this should be your configuration:
// Main.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "arduino.h"
#include <Servo.h>
int _tmain(int argc, _TCHAR* argv[])
{
return RunArduinoSketch();
}
Servo myservo; // create servo object to control a servo
int delayAmount = 2000; // used for spacing out calls
int pin = 3; // the pin that the Servo is on
void setup()
{
myservo.attach(pin); // attaches the servo on pin to the servo object
}
void loop()
{
if (!myservo.attached())
{
Log("Servo is not attached\n");
Log("Servo is attaching\n");
myservo.attach(pin);
if (myservo.attached())
{
Log("Servo is attached\n");
}
}
else
{
Log("Servo is attached\n");
}
myservo.write(0); // tells the servo to go to angle 0
Log("ServoIndex: %d\n", myservo.read());
Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds());
delay(delayAmount);
myservo.write(180); // tells the servo to go to angle 180
Log("ServoIndex: %d\n", myservo.read());
Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds());
delay(delayAmount);
if (myservo.attached())
{
Log("Servo is attached\n");
Log("Servo is detaching\n");
myservo.detach();
if (!myservo.attached())
{
Log("Servo is detached\n");
}
}
else
{
Log("Servo is not attached\n");
}
}