Hi, i have been having a problem with using the attach() function from the servo library in Arduino Uno. Last time i used my servos they where working fine but now each time i use the attach() on them they just start rotating even if i don't write to the servo. I have already tried with various continuous rotation servos and they all present the same problem.
Code:
When the servo is attached, the pulses for the servo motor is started. I think the servo motor will go to a central position. It is not possible to activate a servo motor and keep the current position.
The default pulse width sent after an attach is 1500us and is only nominally the 0 speed value for a continuous rotation servo.
I've never used a continuous one, but I gather that they can vary in the value that actually keeps them still. I believe some have an adjusting screw that you can turn until it stops. Alternatively, experiment with servo.write()'s degrees or servo.writeMicrosecond()'s microseconds until you find a value at which motion stops.
edit... these two lines in Servo.h are important:
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
Servo test code you can use to determine the current stopped us command position for the 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
}