Hello: I'm having no luck trying to control a digital servo, HiTech HSR-5995 that I purchased six years ago. I just need basic code to test with at this point. When using the code below (that I wrote 6 years ago) the servo moves about 30 degrees back and forth while making a very high pitched and disturbing buzz. I would like to move it 180 degrees back and forth. I suspect the pulse width I'm using is flawed.
The servo specs:
Width of Neutral Pulse: 1500uSec
Pulse Variation: +/- 400uSec
Pulse Cycle: 12 - 26mSec
Pulse Wave Voltage: 3.3 - 7.4V
The Code
int servoPin = 9; // R/C Servo connected to digital pin
int myAngle; // angle of the servo (roughly in degrees) 0-180
int pulseWidth; // function variable
void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 2.2) + 1500; // converts angle to microseconds
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount
digitalWrite(servoPin, LOW); // set servo low
delay(2.5); // refresh cycle of typical servos (20 ms)
}
void setup() {
pinMode(servoPin, OUTPUT); // set servoPin pin as output
}
void loop() {
// cycle through every angle (rotate the servo 180 slowly)
for (myAngle=0; myAngle<=180; myAngle++) {
servoPulse(servoPin, myAngle);
}
delay(0);
}
I've also tried the Arduino sample servo sweep code. In this case the servo moves to 0, then rotates about 120 degrees, then stops and makes the high pitched buzz with no further movement.
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
}
Any recommendations are greatly appreciated.