arduino servo

i can't figure this out...for some reason, whenever the arduino resets, my servo moves to the center position (90 degrees). why is this happening and how can i fix it?

thanks

How to use this forum

Read this before posting a programming question

Using what code?

Unless you specifically change it, the servo library default position is 1500us (~90 deg).

zoomkat: i can change the default startup position by editing the "Servo.h" file, right. and I can change the value back once I upload the program to the arduino?

thanks

p.s. Nick: i don't think its in my code. I'm pretty sure zoomkat's suggestion will work.

Some servo test code with provisions for changing the default at the top of the code.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 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(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-test-22-dual-input"); // so I can keep track of what is loaded
}

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 
    int n = readString.toInt();  //convert readString into a number

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

    readString=""; //empty for next input
  } 
}

But you wrote to the servo before attaching it...how does that work?

leeamsi97:
But you wrote to the servo before attaching it...how does that work?

You can specify the desired initial position desired for the servo, and then the servo will be sent that position control signal when the servo is attached. As a test you could comment out the myservo.attach() part and see if the servo still when the arduino resets. If the servo still moves, then there could be arduino bootup activity on the pin to which the servo is attached.

Ok thanks zoomkat. I'll try that and see if it works.

leeamsi97:
But you wrote to the servo before attaching it...how does that work?

The interrupt routine in Servo library consults a table of values to decide how wide to make
each pulse to each servo. write and writeMicroseconds just update the table irrespective of
whether the interrupt routine does anything with it. See the code for more details (its open source!),
specifically the line

    servos[channel].ticks = value;

at the end of writeMicroseconds() sets the value in the table and

    *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks;

in handle_interrupts() sets the timer delay from this.