My servo won't go where it is supposed to go!

I have a single servo connected to my Arduino Duemilanove. The code I’m using for the servo is part of a much larger program. I included only the relevant servo sections.

I added detach statements because I was trying to stop the servo from moving - but it isn’t working. It moves to the position, then flips out and moves back and forth randomly for a few seconds. How can I get it to just to go a position and stay there permanently?

#include <Servo.h>

Servo myservo;               
int open = 60;              
int close = 135;            

void setup()
{
[CODE REMOVED HERE FOR SIMPLICITY]
}

void checkstatus()
{
  if (status == 1)
    { 
      servo_open();
    }
  else
    {
    servo_close();
    }
}

void loop()
{
[CODE REMOVED HERE FOR SIMPLICITY]
}

void servo_open()
{
  myservo.attach(13);                          
  myservo.write(open);                       
  delay(1000);                                  
  myservo.detach();
}

void servo_close()
{
  myservo.attach(13);                            
  myservo.write(close);                           
  delay(1000);                                  
  myservo.detach();
}

Have you tried uploading and running the sample Servo programs? Does the servo behave correctly with the sample sketches running?

What kind of servo is it?

Could be a power issue - have you got the servo on a separate supply? You really should.

The servo does behave correctly with the sample servo script. I have the power supply coming from a battery pack, the same battery pack that is powering the Arduino board.

I have the power supply coming from a battery pack, the same battery pack that is powering the Arduino board.

That could be a problem; What voltage/current is the pack rated at, and is it fully charged?

The servo does behave correctly with the sample servo script.

Although that makes me think what I said a second ago was wrong…

Have you connected the battery 0v (ground) to the Arduino's ground?

Can we see the parts you removed for simplicity? The problem may lie in there.

I added detach statements because I was trying to stop the servo from moving - but it isn't working. It moves to the position, then flips out and moves back and forth randomly for a few seconds. How can I get it to just to go a position and stay there permanently?

Did the "move to position then flip out" issue occur before you added the detach statements?

In the sample sketches, the servo.attach calls occur in setup. Once the servo.write call is made, the servo function (is supposed to) keep pulsing the servo, so that it stays at the position that it was directed to.

Detaching the servo stops this from happening. The servo needs power supplied to it to keep it from drifting.

If the sample sketches work, I think you need to go back to the way that they control the servo. Attach in setup, and don't detach.

I'm using a GPS with this, and the original code is a really long program. Thus, I really don't want to post everything here.

The servo flips out (goes back and forth from 180 to the correct position) over and over again when I have the attach statement in the setup. Unless I call detach right after the servo moves, it goes continues to hum and jerks a couple of degrees every once in a while. It is not continuous, so it reminds me of a floating pin on the board being neither high or low.

Also, if I don't add a 1 second delay before calling detach, the servo doesn't have time to move.

Does this help?

The servo needs a signal approximately once every 20mS to control it's position. Is it possible that other interrupts, etc. are preventing this from happening?

I have no idea why the servo is behaving the way you describe, when the attach is in setup. Admittedly, I don't have much experience with servos. But, when the sample sketches work, and yours doesn't, I suspect that something else you are doing is interfering with the servo control software.

Some PWM and timer stuff is disabled on some pins, when using servos. Besides the GPS, what else do you have connected? What pins are the servo attached to? The GPS?

I have the servo on pin 13, which is a digital pin. Is it a problem if the pin is not a PWM pin?

Also, if I attach, write the position, then detach, why would I need to send a signal every 20 ms to control the position?

Yes, it's a problem if the servo is attached to a non-PWM pin.

If you detach the servo, you don't need to signal it any more. But, what makes the servo hold still, then?

Yes, it's a problem if the servo is attached to a non-PWM pin.

It may be a problem, but it depends on the version of library used.

OK. I thought servos required PWM. I stand corrected.

I have spent the past few hours troubleshooting this, and I've now determined that since I'm using a GPS, and the NewSoftSerial library to talk to it, the problem is in one line of my code:

nss.begin(4800);

I understand this is required for communication to the GPS. When I remove it, the servo responds correctly. When I add it back in to use the GPS, the servo goes nuts.

I have nothing on pin 1, can I use this as a typical digital pin instead? Maybe it's just a problem within pin 13?

So why didn't you tell us that earlier? You were asked to post [u]all[/u] of your code.

I doubt that its a problem with pin13, but you can connect the servo to any free digital pin and try it. My guess is that the interrupt handling of nss is interfering with the interupt handling in the servo library. You could try connecting your GPS using hardware serial on pins 0 and 1, it that works then that would indicate an interaction with nss.

I believe you are correct. If I revert back to the servo library from Arduino 16, it should work. But this also means moving something from pins 9 and 10 and moving the servo to one of these pins.

Yes, try it with the Arduino 0016 servo library on pin 9 or 10.