WOW .....what's going on with my servo?

I've been trying to clean up my sketch and have tried to have my servo initialize itself in the void setup() before I go into the void loop(), but it is acting strangely and I cannot figure it out. I want to make sure the servo is at the top (ie 0 degrees) and then detach it so it doesn't make a noise since it may be 12 hours before it is used. So I figure this would do it:

void setup()
....
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
delay(1000); //thought this would give it time to react
myservo.write(0); // tell servo to go to position 0 degrees
delay(1000); //thought this would give it time to react
myservo.detach(); //disconnect servo so it doesn't make noise
....

What I get is that it goes to 90 degrees first, then 0 degrees, then 10 degrees all within these statements (I had tried 90 degrees once when I was troubleshooting it and I set it to 10 degrees later in the sketch but it does that all here). When I make this modification:

void setup()
....
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
delay(1000); //thought this would give it time to react
myservo.write(0); // tell servo to go to position 0 degrees
//delay(1000); //thought this would give it time to react
myservo.detach(); //disconnect servo so it doesn't make noise
....

It goes to 90 and stops. When I make this mod:

void setup()
....
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
//delay(1000); //thought this would give it time to react
myservo.write(0); // tell servo to go to position 0 degrees
delay(1000); //thought this would give it time to react
myservo.detach(); //disconnect servo so it doesn't make noise
....
It goes to 0 then 10. When I make this mod:

void setup()
....
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
//delay(1000); //thought this would give it time to react
myservo.write(0); // tell servo to go to position 0 degrees
//delay(1000); //thought this would give it time to react
myservo.detach(); //disconnect servo so it doesn't make noise
....

It doesn't move at all regardless of where it starts.

YIKES....Does this make sense to anyone? Any help would help my clear my mind!

Thanks,

Doug

Any time that you write to the servo and immediately detach it, you stop the servo before it has a chance to move.

That explains a lot of the funniness you are seeing.

As for going to 90 degrees first, that is the default powerup position. As I understand, you can change this by writing to the servo before you attach.

I do not know what brand and model of servo you are using. My servos do not quite go to 0 and 180 degrees. Usually something like 10-170, or even less range.

vinceherman:

Thanks for the reply..... I tried to write to the servo without attaching it but it didn't move. I would have thought you have to attach it first. It sounds like you're on the right track - changing the default powerup position. I'll research that some more.

Thanks

Doug

If your servo is making noise, it may be straining against its mechanical stop, which is not good for it. You can use the below test code to determine at what position it starts to make noise.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

I tried to write to the servo without attaching it but it didn't move. I would have thought you have to attach it first.

No, you don't have to attach it before writing to - it simply sets the initial default position of the object.
However, if you do write to an unattached servo, you should not expect it to move.

Thanks to all that replied. I don't understand how you can write to the servo without attaching it but that seems to work.

zoomkat:

Thanks for the code. My servo makes noises inconsistently. In general it doesn't from 10 to 155 although it can at, say, 60. Maybe I damaged it by going less or more than that?

Thanks again,

Doug

DBB:
I don't understand how you can write to the servo without attaching it but that seems to work.

MyServo is a variable, or better, object in your Arduino code. It allows you to control a real servo, but you can call methods on it whether or not the attach has been invoked. Before the attach though, nothing you do will cause any action from the physical servo.

In this case, servo.write allows you to set the position you want the actual servo to go to once you subsequently run the attach.

edit: typo

Can anybody tell me where I would have been able to learn this about the servo object (ie, writing to it without attaching sets the default position) without asking you guys? I'm new to the Arduino and have read all (I think) Arduino has to offer but I cannot find one reference source that has it all. Is there one?

Can anybody tell me where I would have been able to learn this about the servo object

You can look at the library sources.
In your Arduino directory, libraries/Servo/Servo.cpp and Servo.h.

It may looka little daunting, but is nicely structured.
I recommend working back from the public methods.