Need help stopping servos spinning during startup sequence

Hi everyone! I'm building a "Sample Holder" which consists of a gantry, a large box with a depression and 2 servos. The whole setup is running on an uno with 2 encoders.

The way the system works is that one encoder controls the movement in the linear axis while the other encoder controls the rotation of the box. My problem now is that when we give it power/turn it on, both servos go to a certain position and have to be homed again before we can use it properly. If we leave it at that position and restart the power, it retains its position and which leads me to believe we need to add some kind of servo.write line in the setup just before the loop but im not too sure how to go about doing that. Could someone help point me in the right direction? Any help is greatly appreciated!!!

#include <Servo.h>
// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4
#define CLKs 5
#define DTs 6
#define SWs 7
Servo myservo;
Servo myservo2;
int counter = 0;
int counter2 = 0;
int currentStateCLK;
int currentStateCLK2;
int lastStateCLK;
int lastStateCLK2;
String currentDir ="";
String currentDir2 ="";
unsigned long lastButtonPress = 0;
unsigned long lastButtonPress2 = 0;

void setup() {
  pinMode(CLK,INPUT);
  pinMode(DT,INPUT);
  pinMode(SW, INPUT_PULLUP);
  pinMode(CLKs,INPUT);
  pinMode(DTs,INPUT);
  pinMode(SWs, INPUT_PULLUP);
  // Setup Serial Monitor
  Serial.begin(9600);
  // Read the initial state of CLK
  lastStateCLK2 = digitalRead(CLKs);
  lastStateCLK = digitalRead(CLK);
  myservo.attach(9); //left control knob linear
  myservo2.attach(10); //right knob spin

    myservo.write(10); 
    myservo2.write(10); 
  

}

void loop() {


  currentStateCLK = digitalRead(CLK);
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){
    if (digitalRead(DT) != currentStateCLK) {
      counter --;
      currentDir ="CCW";
      if(counter<0){
        counter = 0; 
      }
    } else {
      counter ++
      ;
      currentDir ="CW";
      if(counter>180){
        counter = 180;
      }
    }
 //   Serial.print("Direction1: ");
   // Serial.print(currentDir);
   // Serial.print(" | Degrees: ");
    //Serial.println(counter);
  }
  lastStateCLK = currentStateCLK;
  int btnState = digitalRead(SW);
  if (btnState == LOW) {
    if (millis() - lastButtonPress > 50) {
     // Serial.println("Button pressed!");
      counter = 92;
    }
    lastButtonPress = millis();
  }
  delay(1);

 currentStateCLK2 = digitalRead(CLKs);
  if (currentStateCLK2 != lastStateCLK2  && currentStateCLK2 == 1){
    if (digitalRead(DTs) != currentStateCLK2) {
      counter2 --;
      currentDir2 ="CCW";
      if(counter2<0){
        counter2 = 0;
      }
    } else {
      counter2 ++;
      currentDir2 ="CW";
      if(counter2 >180){
        counter2 = 180;
      }
    }
//    Serial.print("Direction2: ");
//    Serial.print(currentDir2);
//    Serial.print(" | Degrees: ");
 //   Serial.println(counter2);
  }
  lastStateCLK2 = currentStateCLK2;
  int btnState2 = digitalRead(SWs);
  if (btnState2 == LOW) {
    if (millis() - lastButtonPress2 > 50) {
   //   Serial.println("Button pressed!");
      counter2 = 90; 
    }
    lastButtonPress2 = millis();
  }
  //delay(10);
  myservo2.write(counter);
  myservo.write(counter2);
}

112.gif

112.gif

My problem now is that when we give it power/turn it on, both servos go to a certain position and have to be homed again before we can use it properly. I

Use servo.write() before servo.connect() to give the servo an initial position otherwise it will default to 90 degrees as you have found

Yes just do a .write() for each servo BEFORE the .attach().

Steve