I recently purchased a parallax continuous rotation servo. I hooked it up to my arduino and followed the instructions (on this page) to calibrate it (basically getting it to stop when supplied with 1500 μs). That went fine without much issue. The problem is when I run the demo code:
#include <Servo.h>
Servo myServo; // Create Servo object to control the servo
void setup() {
myServo.attach(9); // Servo is connected to digital pin 9
}
void loop() {
myServo.writeMicroseconds(1700); // Counter clockwise
delay(2000); // Wait 2 seconds
myServo.writeMicroseconds(1300); // Clockwise
delay(2000);
myServo.writeMicroseconds(1500); // Stop
delay(2000);
}
When I run the program above, the servo will rotate counter clockwise for 4 seconds and then it'll stop for 2 seconds. It never changes direction and rotates clockwise. The weird thing is if I change the program to write 1300 and then 1700, the servo will rotate clockwise for 4 seconds then stop. So basically I can get the servo to rotate in both directions and I can get it to stop but for some reason, in the same program I can never get it to change directions. There doesn't seem to be anything mechanically wrong with the servo so I feel this must be a programing problem? I've searched and havent found anyone that has had this same problem. Hopefully I'm missing something simple. Any help is greatly appreciated.
jcleezer:
When I run the program above, the servo will rotate counter clockwise for 4 seconds and then it'll stop for 2 seconds. It never changes direction and rotates clockwise.
I don't immediately see anything wrong with your code.
What happens if you make it stop between the forward and reverse movements ?
You might try calibrating the servo again. The below code might be if use testing your servo.
// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a
// to decrease) and enter to change servo position
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;
void setup()
{
myservo.attach(7, 400, 2600); //servo control pin, and range if desired
Serial.begin(9600);
Serial.println("serial servo incremental test code");
Serial.println("type a character (s to increase or a to decrease)");
Serial.println("and enter to change servo position");
Serial.println("use strings like 90x or 1500x for new servo position");
Serial.println();
}
void loop()
{
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
if(readString.indexOf('x') >0) {
pos = readString.toInt();
}
if(readString =="a"){
(pos=pos-1); //use larger numbers for larger increments
if(pos<0) (pos=0); //prevent negative number
}
if (readString =="s"){
(pos=pos+1);
}
if(pos >= 400) //determine servo write method
{
Serial.println(pos);
myservo.writeMicroseconds(pos);
}
else
{
Serial.println(pos);
myservo.write(pos);
}
}
readString=""; //empty for next input
}
Robin2, thanks for the reply. If I make it stop before the forward and reverse movements it will stop successful however when it comes time to move in the new direction it will move again, but in the original direction. I've even gone as far as to try detaching the servo from the pin then reattaching before changing directions but that didnt seem to have any affect. The thought was that if I'm able to get it to change direction when uploading a new program that attaching and reattaching would be the next closest thing. I was powering the servo from the arduino and I've read about various problems that can be caused by insufficient power so I've ordered a power source for the servo.
Thanks for the calibration code zoomkat. I'll give that a go once the power source comes in the mail.