I need help with my project using Stepper Motor

Hello everyone!
I'm using a Stepper Motor size 57 with 1 TB6600 Driver, 1 limit switch, and 1 proximity sensor.
- When the sensor receives the signal, the motor moves forward and reaches the limit switch. When the limit switch is reached, delays 5s and then the motor reverses.

Here is my project's code:

int x;
#define BAUD (9600)
const int PIN_PUL_1 =  7;// the number of the LED pin//1          
const int PIN_DIR_1 =  6;// the number of the LED pin

long PUL_1 =  0;// the number of the LED pin   //2          

void setup() 
{
  Serial.begin(BAUD);
  pinMode(PIN_PUL_1,OUTPUT); //  //3          
  pinMode(PIN_DIR_1,OUTPUT); // Step pin
  pinMode (2, INPUT); //limit switch
  pinMode (3, INPUT); // sensor
  digitalWrite(5,LOW); //enable bat
}

 void motor1(int pul1) 
{
  for(x = 0; x < 5000; x++) 
  {
//start Coppy  //4          
   if(pul1>0)
   {
      digitalWrite(PIN_DIR_1,LOW);
      digitalWrite(PIN_PUL_1,LOW);
   }
   if(pul1<0)
   {
      digitalWrite(PIN_DIR_1,HIGH);
      digitalWrite(PIN_PUL_1,LOW);
   }  
   delay(2); 
       
   if(pul1>0)
   { 
   digitalWrite(PIN_PUL_1,HIGH);
   pul1--;
   }
   if(pul1<0)
   {
   digitalWrite(PIN_PUL_1,HIGH);
   pul1++;
   }

   
   delay(2);  
 }
}

void loop() 
{
  if(digitalRead(3) == LOW){
        motor1(5000);
      }
  if (digitalRead(2) == LOW){
    delay(5000);
    motor1(-5000);
  }
}

The problem is that when the engine hits the limit switch, the motor does not stop but continues to go all the way.
Thanks for reading!

Please see Read this before posting a programming question and post your code properly

Please edit your post and use the code button </> for your code rather than italics.

your code
should look like this

If you want a motor to stop at a limit switch you need to check the switch between every step.

...R
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

Thanks for the information. I've already modified it. Sorry and thank you!