Servo moves out of position when attaching

Hi there
i'm trying to control a 300deg servo with an arduino and it is working perfectly, except for one wee glitch. the servo is a 300 deg, and i need it to turn a full 180, so i have the home position at 1750 and the 180 position is at 500 (min extent counter clockwise) when i boot the arduino it seems that in the setup when the servo is attached, it goes to centre (1500) when it gets to line 52 (the attached code doesn't show the line numbers, but it's myservo.writeMicroseconds(DAMPER_CLOSE_DEGREES); after myservo.attach) it sets it to 1750, the desired home position. i don't want the servo to move when i boot if it can be helped. i won't boot it often, only when we have power cuts, but still, it would be nice.

#include <Servo.h>
#include <Arduino.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

#define SERVO_POSITION  A0
#define DRYER_INPUT     2
#define SERVO_POWER     3
#define GREEN_LED       4
#define RED_LED         5
#define SERVO_PIN       9

#define DAMPER_IS_OPENED         0
#define DAMPER_IS_CLOSED         1

#define DAMPER_CLOSE_DEGREES     1750
#define DAMPER_CLOSE_POSITION    397
#define DAMPER_OPEN_DEGREES      500
#define DAMPER_OPEN_POSITION     982

#define SERVO_POSITION_PERMITTED_ERROR  10

#define ONE_SECOND              1000

#define TEST_FOR_POSITIONING_ERROR(pos)  if( abs(pos - val) > SERVO_POSITION_PERMITTED_ERROR ) digitalWrite(RED_LED, HIGH)

int angle = 0;    // variable to store the servo position
int val;
int DAMPERPosition = DAMPER_IS_CLOSED;

void setup()
{
  pinMode(SERVO_POSITION, INPUT); // position of the servo feedback
  pinMode(DRYER_INPUT, INPUT); // input from sensor about dryer on

  pinMode(SERVO_POWER, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);

  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);

  Serial.begin(9600);
  
  
  myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
  digitalWrite(SERVO_POWER, HIGH); // turn servo power supply on
  delay(ONE_SECOND);


  myservo.writeMicroseconds(DAMPER_CLOSE_DEGREES); // make sure DAMPER is closed when we power up

  val = analogRead(SERVO_POSITION);
  Serial.print("Power");  //doesn't print
  Serial.println(val);

  //TEST_FOR_POSITIONING_ERROR( DAMPER_CLOSE_POSITION );

  delay(ONE_SECOND);
  digitalWrite(SERVO_POWER, LOW); // turn servo power supply off
}

void loop()
{
  int dryerSensor = analogRead(DRYER_INPUT);
  Serial.print("Dryer ");
  Serial.println(dryerSensor);


  if(dryerSensor >= 100 )
  {
      if( DAMPERPosition == DAMPER_IS_CLOSED )
      {
          digitalWrite(SERVO_POWER, HIGH); // turn servo power supply on

          myservo.writeMicroseconds(DAMPER_OPEN_DEGREES);
          delay(ONE_SECOND);

          digitalWrite(RED_LED, LOW);
          val =  analogRead(SERVO_POSITION);
          Serial.print("OPEN ");
          Serial.println(val);

          TEST_FOR_POSITIONING_ERROR( DAMPER_OPEN_POSITION );

          DAMPERPosition = DAMPER_IS_OPENED;
          digitalWrite(GREEN_LED, HIGH);
      }
  }
  else
  {
      if( DAMPERPosition == DAMPER_IS_OPENED )
      {
          myservo.writeMicroseconds(DAMPER_CLOSE_DEGREES);
          delay(ONE_SECOND);

          digitalWrite(RED_LED, LOW);

          val =  analogRead(SERVO_POSITION);
          Serial.print("CLOSE ");
          Serial.println(val);

          TEST_FOR_POSITIONING_ERROR( DAMPER_CLOSE_POSITION );

          DAMPERPosition = DAMPER_IS_CLOSED;
          digitalWrite(GREEN_LED, LOW);
          digitalWrite(SERVO_POWER, LOW); // turn servo power supply off
      }
  }

  delay(300);

}

When you attach() the servo it will move to its default centre position unless you have written a position to it before doing the attach()

Of course, even if you write a position to it before the attach(), if the servo is not at that position it will move

awesome, that worked. thanks. i didn't know there was a point to writing the position before i attached. if it's not in that position to begin with, it will because the power went out while it was at 500, in which case i'll want it to set to 1750 when the power is restored. perfect, thanks again