Help needed with a button press for limited actions.

So, I got 1 button and a servo.
Now when I press and hold the button I want the servo to move a number of steps and then stop as long as the button is pressed.
Once the button is released it needs to go back the same numbers of steps to get to the original location.

I tried a bunch of stuff.
Make it move in void setup() instead of in void loop(), for ()....
But not giving the effect I want.

#include <Servo.h>

Servo servoUD;  // create servo object to control a servo

int angleUD = 90;
int angleStepUD = 1;
int i;

#define Backup 8

void setup() {

  Serial.begin(9600);
  servoUD.attach(10);
  pinMode(Backup, INPUT_PULLUP);
  servoUD.write(angleUD);
  Serial.println("************************************************");

}

void loop() {

  while (digitalRead(Backup) == LOW) {


    if (angleUD > 20 && angleUD <= 160) {
      angleUD = angleUD - angleStepUD;
      if (angleUD < 20) {
        angleUD = 20;
      } else {
        for (i = 0; i < 10; i++) {
          servoUD.write(angleUD); // move the servo to desired angle
          Serial.print("Moved DS north to: ");
          Serial.print(angleUD);   // print the angle
          Serial.println("°");
          delay(100);
        }
      }

      delay(10000); // waits for the servo to get there
    }
  }

  while (digitalRead(Backup) == HIGH) {

    if (angleUD >= 20 && angleUD <= 160) {
      angleUD = angleUD + angleStepUD;
      if (angleUD > 160) {
        angleUD = 160;
      } else {
        for (i = 0; i < 10; i++) {
          servoUD.write(angleUD); // move the servo to desired angle
          Serial.print("Moved DS south to: ");
          Serial.print(angleUD);   // print the angle
          Serial.println("°");
          delay(100);
        }
      }

      delay(10000); // waits for the servo to get there
    }
  }

}

What does it do that it shouldn't ?
Why the 10 second delay()s ?

I am very new to Arduino so I am just copy pasting parts of code I find and adjusting them.
The 10 second delay was just an effort to make it stop looping.

It needs to loop to read the button press/release, but as long as the button is pressed it only needs to do 10 steps.
I cant get it to stop looping those 10 steps.

I cant get it to stop looping those 10 steps.

You need to detect when the button becomes pressed then run the for loop. Note the need to detect when the button becomes pressed rather than when it is pressed

See the StateChangeDetection example in the IDE

Thanks, thats what I needed.

The code now looks like this:

#include <Servo.h>

Servo servoUD;  // create servo object to control a servo

int angleUD = 90;
int angleStepUD = 1;
int i;
int buttonState = 0;
int lastButtonState = HIGH;

#define backUp 8
#define ledPin 13

void setup() {

  Serial.begin(9600);
  servoUD.attach(10);
  pinMode(backUp, INPUT_PULLUP);
  servoUD.write(angleUD);
  Serial.println("************************************************");

}

void loop() {

  // read the pushbutton input pin:
  buttonState = digitalRead(backUp);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      digitalWrite(ledPin, HIGH);
      Serial.println("on");


        for (i=0;i<10;i++){
          servoUD.write(angleUD = angleUD - angleStepUD); // move the servo to desired angle
          Serial.print("Moved DS South to: ");
          Serial.print(angleUD);   // print the angle
          Serial.println("°");
          delay(100); // waits for the servo to get there
        }
    } else {
      // if the current state is LOW then the button went from on to off:
      digitalWrite (ledPin, LOW);
      Serial.println("off");

        for (i=0;i<10;i++){
          servoUD.write(angleUD = angleUD + angleStepUD); // move the servo to desired angle
          Serial.print("Moved DS north to: ");
          Serial.print(angleUD);   // print the angle
          Serial.println("°");
          delay(100); // waits for the servo to get there
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  }

}

I'm glad it works and I am sure that you will use the technique again in future

It does not matter to the compiler but some of the comments do not match the code. This may be confusing if you need to refer to this code when using the technique in future

  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == LOW)
    {
      // if the current state is HIGH then the button went from off to on: