Problems with initial position and speed of a servo motor

Hi Everyone, I'm doing this project that needs to servos to move in opposite directions, in slow speed. Please pardon my English, I'm from Puerto Rico, and English is not my strong.

First of all this is my first project and first time programming in this language. I wrote this code, and is working in some ways, but definitely needs a lot of love. I do not know how to set them so they do not move before a command when I power them. Also with the last edit is telling me I'm missing something that I can't find.

here is the code:

#include <Servo.h>

Servo servoLeft;
Servo servoRight;
#define downbutton 7
#define upbutton 9

void setup()
{
servoLeft.attach(3);
servoRight.attach(5);
pinMode(downbutton, INPUT);
pinMode(upbutton, INPUT);
}

void loop() {
int buttonstate1 = digitalRead(downbutton); // Two States for buttons
int buttonstate2 = digitalRead(upbutton);
int pos = 20;
int neg = 70;
int pos2 = 20;
int neg2 = 70;
for(pos < 70; pos += 5; neg > 20; neg += -5){ // goes from 0 degrees to 110 degrees and viceversa in steps of 5 degree
if(buttonstate1 == true){ //button is high
servoLeft.write(pos); // tell servo to go to position in variable 'pos'
servoRight.write(neg); // tell servo to go to position in variable 'neg'
delay(1000); // wait 1 second
}
}
for(pos2 < 70; pos2 += 5; neg2 > 20; neg2 += -5){ // goes from 0 degrees to 110 degrees and viceversa in steps of 5 degree
if(buttonstate2 == true){ //button is high
servoLeft.write(neg); // tell servo to go to position in variable 'neg'
servoRight.write(pos); // tell servo to go to position in variable 'pos'
delay(1000); // wait 1 second
}
}
}

There's no feedback from a plain servo. You don't know where it is until you command it to go somewhere different. Then you don't really know if it arrived at that position except you expect that you gave it enough power to drive itself to that position.

If you don't want the servo suddenly moving when you turn it on then either park it in a known position when you turn it off or remember where you last sent it to, then start from that position.

If it's possible for someone or something to move the servos while they are powered off then you cannot avoid that sudden jerk.

If that really is a problem then perhaps another technology like a stepper motor might be better?

Thanks for the feedback MorganS. Moving the servos while they are turned off it is not a problem. After installed, the servos will be in a closed box attached to a shaft that is physically restricted.

About park it, is there a specific command? Something like a serial begin?

If you know the position it was parked in you can start your servo code like this

myServo.write(parkAngle);
myServo.attach(parkPin);

and if you have an orderly shut-down process before you switch off your Arduino the last instruction to the servo can be

myServo.write(parkAngle);

However if the Arduino gets switched off for any reason without moving the servo to the parkAngle then there is no means to know where it is at startup.

Another issue is that some servos move a little in the short interval between applying power to the Arduino and the servo.attach() command being implemented.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Thanks Robin2. Knowing this I'm wondering what exactly do you mean by "an orderly shut-down process before you switch off your Arduino"?

OscarUPRM:
Thanks Robin2. Knowing this I'm wondering what exactly do you mean by "an orderly shut-down process before you switch off your Arduino"?

It could be as simple as a button that you press that tells the Arduino to move the servo to the park position. By "orderly" I just meant that you adopt a formal procedure for shutting down - e.g. always press the "park button" before disconnecting the power.

You could build an auto-shutdown circuit with an auxiliary battery that would keep the Arduino and servo powered for maybe 20 seconds after the Arduino detects that the main power has been disconnected - if it is worth that much trouble.

...R

Could write the last position to sdcard maybe.... before formal powerdown. And recall it at startup.

for(pos < 70; pos += 5; neg > 20; neg += -5)Don't try to invent syntax. It will never end well.