How to stop servo motor

Good day everyone,

I try this code for controlling my servo motor, what i want is when i push a button that is connected from pin 2 to ground the servo will move from position 0 to 90 in step of 1 degree then it will stop. and vice versa.
But what happen is that when the button is pressed the servo will move from position 0 to 90 slowly then it will immediately go back to position 0 then it move from 0 to 90 slowly again. It just wont stop.
I Hope you can enlighten me.

/*
 Input Pullup Serial
 
 This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a 
 digital input on pin 2 and prints the results to the serial monitor.
 
 The circuit: 
 * Momentary switch attached from pin 2 to ground 
 * Built-in LED on pin 13
 
 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal 
 20K-ohm resistor is pulled to 5V. This configuration causes the input to 
 read HIGH when the switch is open, and LOW when it is closed. 
 
 created 14 March 2012
 by Scott Fitzgerald
 
 http://www.arduino.cc/en/Tutorial/InputPullupSerial
 
 This example code is in the public domain
 
 */
#include <Servo.h> 
Servo myservo;
int pos = 0;
void setup(){
  //start serial connection
  //Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT); 
myservo.attach(9); 
Serial.begin(9600);
}

void loop(){
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  //Serial.println(sensorVal);
  
  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the 
  // button's pressed, and off when it's not:
  myservo.attach(9);
  
  if (sensorVal == HIGH) 
  {
    
    for(pos = 0; pos <= 90; pos += 1)  
        {                               
          
          if(pos == 90)
          {myservo.detach();}
          else{
          myservo.write(pos);
          delay(15);
          }
          
          
        } 
   
  }
  else 
  {
     
     for(pos = 90; pos>=0; pos -=1)     
    
          if(pos == 0)
          {myservo.detach();}
          else{
          myservo.write(pos); 
          delay(15);
          }
        }
   
       
   
    
  }
  
}

That’s exactly what you’ve coded!

You shouldn’t be calling myservo.attach twice - do it once in setup() only.

The better approach to this problem is not to use a for loop at all, but maintain
state variables between calls to loop().

int pos = 0 ;    // current position of servo
int direction = 0 ;  // direction state, +1, 0 or -1

void loop ()
{
  if (digitalRead (2))  // trigger from button
    direction = 1 ;

  // now run the state machine
  if (direction == 1)   // moving forward
  {
    if (position < 90)
    {
      position ++ ;
      myservo.write (position) ;
      delay (10) ;    // limit speed
    }
    else 
      direction = -1 ;   // change direction
  }
  else if (direction == -1)
  {
    if (position > 0)
    {
      position -- ;
      myservo.write (position) ;
      delay (10) ;
    }
    else
      direction = 0 ; // stopped
  }
}