Servo motor controlled via Bluetooth

Dear Reader,
i am building a tabletennis maschine and i am coded a programm which should when i press a button on my phone make a servo motor move in 3 different positions all 1,5 seconds. This works perfectly. but then when i press another button then the code is still running from the first button! :frowning:

#include <Servo.h>
Servo motor;
int signal= 0;
void setup() {
motor.attach(9);
motor.write(45);
Serial.begin(9600); //CONNECTION RATE FÜR BT MODUL
}

void loop() {


 if(Serial.available() > 0){
  signal = Serial.read();
    if(signal == '1'){
      while(signal=='1'){
      
       for(int position=45;position<=135; position+=45){
        motor.write(position);
        delay(1500);
       }
       
        for(int position=135;position>=45; position-=45){
        motor.write(position);
        delay(1500);
       }

      }

   if(signal == '2')motor.write(45);
   if(signal == '3')motor.write(90);
   if(signal == '4')motor.write(135);
   
   
   
   
   
   }
}
}

i suggest you have a sub-function that moves the servo to a target. you can use millis() to determine if it's time to move to the next step. that function returns in between steps and is called each iteration of loop(). of course the sub-function doesn't go beyond the target; it doesn't make any further changes at that point

loop() also checks for commands thru the serial interface. a command may be a new target. setting a new target may require resetting the timeout between step changes and may cause the sub-function to change direction. loop() calls the sub-function to move the motor regardless of getting receiving a cmd

while(signal=='1')What in that loop stops signal being '1'?