Arduino, accelstepper with driver DM556 and motor nema 34

Hi everyone, I'm a beginner, you will notice from the type of code I wrote.
I'll explain what I have to do with this project.
I use an Arduino Mega 2560V3
I have to drive a driver for a stepper motor (DM556) that operates a nema 34 stepper with which I move the X axis of a hobby milling machine that I have at home.
For the operation I have provided a joystick with NO contacts to move to the right or left, if I wanted I could also use that of an analog arduino, but to start and make it easier as programming I chose this.
I have two limit switches that I will put on the table along the X axis to stop the operation when it reaches one of the two limits, these are NC microswitches so that if you have to cut the cable or have some problem I stop the motion and cause no damage .
I have a button for fast forward (with its own trimmer to adjust the speed) and a trimmer for the basic speed subject to the two switches on the joystick.
As outputs I have: 3 LEDs, one for status as power on and 2 to indicate the direction, the 3 pins for the stepper driver ENA, DIR, PUL.
I have set the inputs as PULLUP so I always use the reference to ground.
however I managed after many tests to find the various problems that did not make me go to the sketch.
Now I have a new problem but I think it can be solved with a Accelstepper library if I'm not mistaken, I have to read up.
Basically everything I want it to do works but if I start with a high number of impulses to the driver, the motor gets stuck, I think it should use the accel_stepper library or something like that.
if anyone has some better idea or even suggests me how to optimize the sketch I would be grateful.

#include <AccelStepper.h>





#define puls_sx 3      //pulsante controller SX
#define puls_dx 4      //pulsante controller DX
#define ena 5           //ENA stepper Driver
#define dir 6           //DIR stepper Driver
#define pul 7           //PUL stepper Driver
#define rapid 8         //Pulsante rapido
#define potS A0         //Potenziometro velocità normale
#define potR A1         //Potenziometro velocità Rapida
#define fcsx 10         //Fine corsa SX NC
#define fcdx 11         //Fine corsa DX NC
#define ledSx 22        //Led direzione SX
#define ledDx 23        //Led direzione DX
#define buzzer 9        // Buzzer segnalazioni acustiche
#define power_on 2       //led stato acceso

int val_p_sx =0;
int val_p_dx =0;
int val_fcsx =0;
int val_fcdx =0;
int val_potS =0;
int val_potR =0;
int val_rapid =0;
long delay_MicrosS = 0;
long delay_MicrosR = 0;

void setup() {

  Serial.begin(9600);
  pinMode(puls_sx, INPUT_PULLUP);
  pinMode(puls_dx, INPUT_PULLUP);
  pinMode(ena, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(pul, OUTPUT);
  pinMode(potS, INPUT);
  pinMode(potR, INPUT);
  pinMode(fcsx, INPUT_PULLUP);
  pinMode(fcdx, INPUT_PULLUP);
  pinMode(ledSx, OUTPUT);
  pinMode(ledDx, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(power_on, OUTPUT);
  pinMode(rapid, INPUT_PULLUP);
  
  
  
 
  digitalWrite(power_on, LOW);
  digitalWrite(ledSx,LOW);
  digitalWrite(ledDx,LOW);
  delay(200);
  digitalWrite(ledSx, HIGH);
  digitalWrite(ledDx, HIGH);
  Serial.println ("Ready");
  delay(1000);
  digitalWrite(ledSx, LOW);
  digitalWrite(ledDx, LOW);
  tone(1, 1000, 600);
  delay(1000);



}

void tone_err() {
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  tone(2, 1200, 100);
  delay(100);
  Serial.println("Tono Errore");
}



void start_normal() {
  
   // if (digitalRead(val_rapid)==1){                                                      //se puls rapid non premuto
    delay_MicrosS = map(analogRead(potS), 0, 1023, 1000, 20000);  //leggo velocita potS
   
    {
      delayMicroseconds (delay_MicrosS);
      digitalWrite(pul, HIGH);
      delayMicroseconds(5); 
      digitalWrite(pul, LOW);
    }

  }

 void start_rapid() {
 // if(digitalRead(val_rapid)==0) {                                                    //se premo rapid
    delay_MicrosR = map(analogRead(potR), 0, 1023, 100, 3000);   //leggo potR
   
   
    {
      delayMicroseconds (delay_MicrosR);
      digitalWrite(pul, HIGH);
      delayMicroseconds(5); 
      digitalWrite(pul, LOW);
      
    }

  }




//}

  

void loop() {
 val_p_sx = digitalRead(puls_sx);
 val_p_dx = digitalRead(puls_dx);
 val_fcsx = digitalRead(fcsx);
 val_fcdx = digitalRead(fcdx);
 val_rapid = digitalRead(rapid);
 

 
if ((val_p_sx == 0)&&(val_p_dx==1)) {             //funziona a SX
    if (val_fcsx == 0) {
      digitalWrite(ledSx, HIGH);
      digitalWrite(dir, HIGH);
      digitalWrite(ena, HIGH);
       if (val_rapid==0){
        start_rapid();
       }
     else {
        start_normal();
       }
    }

    else  {   if (val_fcsx == 1)   {      //finecorsa sx aperto blocco movimento
      digitalWrite(ledSx, LOW); {
        digitalWrite(ena, LOW);
        Serial.println("finecorsa sinistra");
        tone_err();
       
      }
    }

  }
  }


 if ((val_p_dx == 0)&&(val_p_sx==1)){
    if (val_fcdx == 0){                                     //funziona a dx
      digitalWrite(ledDx, HIGH);
      digitalWrite(dir, LOW);
      digitalWrite(ena, HIGH);
        if (val_rapid==0){
        start_rapid();
       }
     else {
        start_normal();
       }
      
    }

    else {  if(val_fcdx == 1); {                  //finecorsa dx aperto blocco movimento
      digitalWrite(ledDx, LOW); {
        digitalWrite(ena, LOW);
        Serial.println("finecorsa destra");
        tone_err();
       
      }
    }

  }
 }
  if (((val_p_dx==0) && (val_p_sx==0))||((val_p_dx==1) && (val_p_sx==1))){
    digitalWrite(ena, LOW);
    Serial.println("Stop");
  }
   /*if ((val_p_dx==1) && (val_p_sx==1)){
     digitalWrite(ena, LOW);
  //   Serial.println("Stop 2");
  }                  */
 }

A wiring diagram, not Fritzing, could be of interest.

Accelstepper is probably a good idea - that's what it's for.

Personally, I find the library a bit confusing, but a little time reading the documentation would take care of that I expect.

In my own code I have handled basic accell/decel by changing the delay between steps a little at each step and then resetting to slow at a stop or change of direction.

May be @raunarde could use MobaTools instead. This may be a little bit easier to handle because you don't have to struggle with step generation in the sketch. This is done in timerinterrupts in the background. A documentation is provided.

for Railroader that ask me a diagram, I hope that img of tinkerkad is ok
the only difference are on LED pin because on tinkercad use the Arduino Uno and I haven't enough digital pin

I read that you post. thank you.
I try this night to modify my sketch.

My tutorial on Multi-tasking in Arduino has a complete stepper control example, using Accelstepper, controlled by user input and a sensor input.

Yes, that library has too many options and people don't usually get it right first time.

In setup use setMaxSpeed() and setAcceleration(). In loop call run(), and the only other calls you need to use are move() or moveTo() - and for monitoring progress distanceToGo() and for
emergency stop stop().

Using these gives fully asynchronous (non-blocking) operation which is normally what
you want for convenient control. Its vital not to use delay() anywhere, or blocking calls that prevent run() from being called frequently, otherwise you'll cause the motor
to stop or stutter.