Servo stop motion loop if button is released

I can get this servo program to go through the motion profile I want, but what I can't get it to do is the moment you let off the button I want the servo to instantly return to the 0 position no matter where it is in the program. The servo always goes through the loop once. Can anyone help and fix this program to do so? I have tried and tried. Thanks for your help.

Here is the code I have.

#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
const int buttonPin = 12; //sets pin 2 as button
int buttonState = 0; //sets button 1 as off
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (buttonPin, INPUT); // sets button as input
if (buttonState == HIGH)
{myservo.write(0,255,true);
delay (10);
}
else {
delay(50);
myservo.write(0,10,true);
delay(50);
}

}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{

myservo.write(90,10,true);
delay(50);

for(pos = 90; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=90; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position

}
}
else {
delay(50);
myservo.write(0,10,true);
delay(50);
}
}

I would think an interrupt would do exactly what you're asking.

You need to switch to using millis() to control the servo movement timing. That way you can read the switch pin between each servo step.

Basically you move a little, read the input, move a little, read the input and so on.

See the BlinkWithoutDelay example in the IDE.

Look at how the servo is controlled in the demo several things at a time

...R

#include <VarSpeedServo.h>
VarSpeedServo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
const int buttonPin = 12; //sets pin 2 as button
int buttonState = 0; //sets button 1 as off
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (buttonPin, INPUT); // sets button as input
if (buttonState == HIGH)
{myservo.write(0,255,true);
delay (10);
}
else {
delay(50);
myservo.write(0,10,true);
delay(50);
}

}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
 
myservo.write(90,10,true);
delay(50);

  for(pos = 90; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
  {  // in steps of 1 degree
    if (digitalRead(buttonPin) == LOW)
        break;
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=90; pos-=1)     // goes from 180 degrees to 0 degrees
  {
    if (digitalRead(buttonPin) == LOW)
        break;
                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

delay(50);
myservo.write(0,10,true);
delay(50);
}