Servo startposition problem

Hello everyone,

I have a problem. In a project I am working on, Its quite important that the servo is not doing that "getting in start position" thing.
The only thing that the servo has to do, is turn 180 degrees, and back in the end of the program. But when the arduino is powered, the servo is turning weird, and gets in the position where it approximately was. '

Is anyone familiar with this problem?

Thanks a lot!

Your Q is convoluted and confusing.
Please post your code, and added // comments that describe what you think each section/line -should- do.
Please use code tags, see '#' in the formatting options.

The default servo start position is 90 deg (1500us). You can specify the start position in the setup section of your code like below. Note that if the servo is in a different position than the specified position, the servo will move when attached. Best to park the servo in the startup position prior to shutting the arduino down.

// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h> 
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo 
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  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) {
    Serial.println(readString);  //so you can see the captured string 

      // attach or detach servo if desired
    if (readString == "d") { 
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(7); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }    

    n = readString.toInt();  //convert readString into a number

    // auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n); 
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}

The only thing that the program has to do, is sweep 180 degrees and back when a button is pushed. The servo should not be moving when the arduino is powered on.

GJVN:
The only thing that the program has to do, is sweep 180 degrees and back when a button is pushed. The servo should not be moving when the arduino is powered on.

I don't see the code you were asked to post. You have all the info, and we have none unless you give it to us.

Without seeing your code it is impossible to guess what signals the Arduino may or may not be sending to your servo.

How are you powering the servo? Many servos draw more power than the Arduino can provide and need their own power supply with a common ground with the Arduino. If they draw power from the Arduino they can cause the Arduino to reset with strange consequences - including possible damage to the Arduino.

Some servos behave wildly if they have power but no signal. This might be tamed by connecting a 4k7 resistor between the servo signal and ground wires.

...R

#include <Servo.h> 
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int Potpin = A0;    // Pin of the potentiometer
int Value = 0;      // 
int i;
int n;
Servo myservo; 
int pos = 0;   


int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  
  myservo.attach(3);
  pinMode(ledPin, OUTPUT);  
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);         

}

void loop(){
  
  buttonState = digitalRead(buttonPin);

 
  if (buttonState == HIGH) {  
    
  Value = analogRead(Potpin); // Read Potentiometer
  delay (Value);   // wait as long as the value is
  Serial.println(Value);  
  
  digitalWrite (ledPin, HIGH);  // Turn LED on


 for(pos = 180; pos>=0; pos-=180)     // goes from 180 degrees to 0 degrees 
  {                                   // in one step of 180 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
 
 
digitalWrite (ledPin, LOW);   // turn LED low
 }
  else {
   
    digitalWrite(ledPin, LOW);  // turn LED low
  }



}

I'm sorry for not posting the code.
The servo is powered by the arduino itself. It is indeed acting weird when the signal wire is not attached. I'll try the resistor thing you said.

The resistor thing worked! Thanks a lot everyone for helping me. I really appreciate it!