Servo controlled by joystick: why is my code wrong?

Hi! I'm not very sure this is the right section, sorry if it's the wrong one.

I'm learning about various Arduino modules and, while reading the section that explains the joystick, it accurred to me that I could control a little servo with said joystick. I wanted to do a simple program to control the servo: when pointing to the right with the thumbstick, the servo would go to the right. Same thing for the left. Also, when pressing the thimbstick button, the servo would detach and the program would terminate.

I wrote the code but, no matter what, I couldn't get the servo to behave properly. I checked that both servo and joystick weren't broken and that the various connections were right (I don't know anything about electronics yet, sorry for the improper language), so I guess the problem is in my code. Could you help me find it?

Here's the code:

#include <Servo.h>

//Digital port where the servo is connected
#define SERVO 11
//Defining positions for the motor
#define START 90
#define RIGHT 0
#define LEFT 180
//Defining delay
#define DELAY 15

//Analog ports where the X and Y data lines of the joystick are connected
#define XAXIS 0
#define YAXIS 1
//Digital port where the stick button of the joystick is connected
#define BUTTON 3
//Since the joystick reads tend to vary, if X axis is 400 < XAXIS value < 600 nothing happens
#define RIGHTPOS 100
#define LEFTPOS 900


//Servo variable
Servo myServo;
//Assign to motorPosition the starting point, 90 degrees
int motorPosition = START;
//Variable that will contain the XAXIS readings
int stickPosition;

void setup(){
  //Declaring the joystick button as INPUT device
  pinMode(BUTTON, INPUT);
  myServo.attach(SERVO);
  myServo.write(START);
}

void loop(){
  myServo.attach(SERVO);
  while(1){
    //XAXIS reading goes into stickPosition
    stickPosition = analogRead(XAXIS);
    //If the stick goes to the left, the motor goes to motorPosition to motorPosition+1
    if(stickPosition > LEFTPOS && motorPosition <= LEFT){
      myServo.write(motorPosition--);
      delay(DELAY);
    //If the stick goes to the right, the motor goes to motorPosition to motorPosition-1
    }else if(stickPosition < RIGHTPOS && motorPosition >= RIGHT){
      myServo.write(motorPosition++);
      delay(DELAY);
    //If the stick stays between the range of 100/900, the program does nothing
    }else if(stickPosition < LEFTPOS && stickPosition > RIGHTPOS){
      ;
    //If the program detects that BUTTON has been pressed, it exits the loop and detaches the servo
    }else if(digitalRead(BUTTON) == LOW){
      break;
    }
  }
  myServo.detach();
}

When running, the program seems to function properly except for little things. Here's what I noticed:

1 - First of all, I don't understand why if I add myServo.detach(); in the setup function the servo doesn't start at all and doesnt' move to the START position. Is something related to the Servo.h library or I didn't understand the setup function in Arduino programming?

2 - Similar issue to the previous: if I don't put the myServo.attach(SERVO); instruction on the top of the loop function, the servo doesn't work. Again, Servo.h library design or my ignorance?

3 - Another issue is with the BUTTON: it doesn't turn off the servo when pressed. It seems to work only when the servo is in RIGHT or LEFT positions (more on that later).

4 - Also, I feel that the servo attaches and detaches kinda randomly during the execution of the program, and I cannot figure out why, since that when the program is in the while loop, the servo sould be always attached thanks to the myServo.attach(SERVO); instruction, the first of the loop function.

5 - Another thing: when the servo reaches the RIGHT of the LEFT position, there's nothing I can do for it to move again: I feel it vibrate but it doesn't move. Why?

Please help me, I want to understand!

You don't need to keep attaching and detaching the servo. When it is attached and you write to it it will stay where it is until it receives another write. If you detach it the control signal stops and it no longer knows where it is supposed to be positioned. So your first problem is almost certainly because you detach the servo before it has had chance to move and it's possible some of the others are similar.

I'd rewrite the code to use the servo properly and since loop() already does a constant loop you really don't need another constant while() loop inside it.

Steve