[ Pobierz całość w formacie PDF ]
}
34 | appendix
servo output
Hobby servos are a type of self-contained motor that can move in a 180º arc. All that
is needed is a pulse sent every 20ms. This example uses a servoPulse function to
move the servo from 10º -170º and back again.
int servoPin = 2; // servo connected to digital pin 2
int myAngle; // angle of the servo roughly 0-180
int pulseWidth; // servoPulse function variable
void setup()
{
pinMode(servoPin, OUTPUT); // sets pin 2 as output
}
void servoPulse(int servoPin, int myAngle)
{
pulseWidth = (myAngle * 10) + 600; // determines delay
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // microsecond pause
digitalWrite(servoPin, LOW); // set servo low
}
void loop()
{
// servo starts at 10 deg and rotates to 170 deg
for (myAngle=10; myAngle
{
servoPulse(servoPin, myAngle); // send pin and angle
delay(20); // refresh cycle
}
// servo starts at 170 deg and rotates to 10 deg
for (myAngle=170; myAngle>=10; myAngle--)
{
servoPulse(servoPin, myAngle); // send pin and angle
delay(20); // refresh cycle
}
}
appendix | 35
[ Pobierz całość w formacie PDF ]