Sunday, June 20, 2010

Arduino servo control code

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<=170; 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
}
}
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following