Arduino - Servo - On startup servo motor moves to default position too fast!

Hi,

I have an arduino pro mini and a servo motor - servo motor powered separately.

I know how to control the rotation speed of the servo motor using for loops and delays - no problem there.

I also know how to send strings over a serial connection to rotate the servo to different angles and have the servo rotate back and forth slowly.

I need to rotate the servo slowly because of the load it is connected to.

Currently I'm sending commands over Serial Monitor.

Here is my problem:

I have set the default angle for the servo in the setup loop so that every time the arduino is powered on or Serial Monitor is closed the servo returns to it's starting position.

My problem is that when the servo is at an angle other than the default position and the arduino is powered on or Serial Monitor is closed the servo motor rotates too quickly back to the default position.

Is there a way to control the rotation speed of a servo to the default position at power on (or closing Serial Monitor)?

Thanks.

My problem is that when the servo is at an angle other than the default position and the arduino is powered on or Serial Monitor is closed the servo motor rotates too quickly back to the default position.

Is there a way to control the rotation speed of a servo to the default position at power on (or closing Serial Monitor)?

I assume we are talking about a standard servo, not a 'modified for continous rotation' servo? What you are dealing with is pretty fundementally difficult to solve. A servo when powered off will tend to remain in it's last commanded position (assuming the mechanical load on the gear train doesn't move it when unpowered), and there is no way for a sketch first starting up to know what position the servo is presently at, because there is no position feedback signal from the servo back to the arduino avalible.

The speed of a servo is basicly controlled by the servo itself, not the arduino software commands, as all you can do with software is to command smaller angle steps and add delays between issuing new commands to simulate slower movement then if you just issued one command to the final desired positon.

Maybe somebody else has an idea for you as all I can suggest is that if it's super important then you must mechanically couple some kind of pot to the servo linkage and read back it's true position via a analog input pin.

Lefty

Lefty,

How does the arduino detect that the Serial Monitor is closed?

When I close the Serial Monitor is it closing the serial port?

When I close the Serial Monitor the arduino moves the servo back to its default position (at a fixed speed that's too fast).

I could possibly wire something up so that the servo runs on less voltage when the Serial Monitor or serial port is closed and on power on - that should slow it down (maybe).

Any thoughts on the code I could use to detect when the Serial Monitor or serial port is closed?

How does the arduino detect that the Serial Monitor is closed?

Your probably seeing the effects of the arduino auto-reset feature. Opening and closing the serial monitor cause the board to do a reset which starts your sketch from the beginning.

Lefty

Lefty,

I forgot about disabling the autoreset with a resistor - I took my jumper off while I was tweaking the program.

That fixes the problem with opening and closing the Serial Monitor and serial port.

As for power on and the servo moving quickly I think I’ll wire up a photo relay to a pin on the arduino so that I can control the voltage going to the servo in the software.

On power up the servo will run at a lower voltage and thus move slower.

When I send an command to the arduino the servo can run at normal power.

I’ll try this out and see how it goes.

Thanks.

Controlling the voltage to a servo’s power lead is not a simple task, do you have a specific circuit in mind?

Lefty

Is there be a way to attach a potentiometer to the servo? Would that work?

Is there be a way to attach a potentiometer to the servo?

Doesn't the servo already have one?

If you add a pushbutton, and some code to remember where the servo is, you could store the servo position in EEPROM when the button was pressed.

On startup, read where the servo was last positioned (from EEPROM), and set that as the current position, before attaching the servo. Then, the servo won't jump to its "proper" position, and you can move it at whatever speed you like to the start position.

I have the same problem with the servo positioning, at Arduino wake up, the micro servo moves to 96 degrees, after that I can control the servo position perfectly. What can I do to remove that initial positioning?

My sketch (so improvable I know) is the next:

#include <Servo.h>
Servo myservo;

void setup() {

  • myservo.attach(9); // servo uses digital 9*
  • servoGo(0); // to go to 0 deg*
  • servoGo(180); } // go to 180 degrees*
    void loop() {}
    void servoGo(byte finalAngle){
  • byte currentAngle = myservo.read(); //servo position*
  • if (finalAngle > currentAngle) {*
  • for (int i = currentAngle; i < finalAngle ; i++){*
  • myservo.write(i);*
  • delay(50); } }*
  • else {*
  • for (int i = currentAngle; i > finalAngle ; i–){*
  • myservo.write(i);*
  • delay(50); } }*
    }

I have seen other forum thread for this..

http://forum.arduino.cc/index.php?topic=48602.0

You should first be aware that servo.read() just returns the last value you used with servo.write(). It does NOT interrogate the servo. I have no idea what it returns if you have not issued a servo.write() before hand.

Much better just to store the current servo position in a variable.

The initial position of a servo is not easy to deal with. There are 2 potential problems.

First, when the Arduino starts it takes a few seconds before it gets around to running the code in setup(). If the servo has power during that period it will have no position signal and some servos behave erratically in that situation. I have found that connecting a 4700 ohm resistor between the signal wire and ground can stop the erratic behaviour.

Second, the Arduino has no means of knowing where the servo actually is when the Arduino starts. For example the servo might have been rotated manually while the Arduino was off. This means that whatever angle you choose for the initial position may not be the angle the servo is actually at, and it will move to the indicated position at its normal full speed.

You can go some way to minimize the second problem by using a shut-down routine to position the servo ready for start-up and making it a rule not to move it manually.

If you look at the demo several things at a time you will see code to move the servo without holding up other activities.

...R