On and Off switch for a 28-BYJ48 stepper motor via HC-05

Hey guys!

im fairly new to arduino and im trying to get a stepper motor to go clockwise for 2000 steps and when it reaches the 2000 it proceeds counter clockwise. i have it so far, but im having trouble stopping it during this process. please help!!

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1


AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
int incomingByte = 0;
void setup() {
  Serial.begin(9600);

 stepper1.setAcceleration(100.0);
  stepper1.setSpeed(100);
  stepper1.moveTo(2000);      
  stepper1.setMaxSpeed(200.0);
 Serial.println("bluetooth connected"); 
  
}

void loop() 
{int flag=0; 
if(Serial.available()>0)
  {
    int input = Serial.read(); 
    switch(input)
    {
     case 'T' :
     Serial.println("stepper be steppin");
     flag=1; 
     stepper1.run();
     while(flag==1)
  {if ((stepper1.distanceToGo() == 0))
   {
    stepper1.moveTo(-stepper1.currentPosition());
   }
  stepper1.run(); 
  }
     break;
     case 'X' : Serial.println("Kill Switch Activated");
     stepper1.stop();
     break;
     default : break;
    }
  } 
  
  }
     while(flag==1)
  {if ((stepper1.distanceToGo() == 0))
   {
    stepper1.moveTo(-stepper1.currentPosition());
   }
  stepper1.run();
  }

If flag IS 1, this is an infinite loop. Not a good idea.

Your code looks like crap. Nothing follows a { on the same line. Ever.

Tools + Auto Format would sober up the drunken sailor look that your code current presents.