Welding oscillator and modifications

Hi
I’ m a beginner and its my very first project so i might have many mistakes.
My project is producing an oscillator for welding. I want to control system with 3 potantiometer.

It will be like this: Automatic Welding Oscillator Weaver PLC Motorized Linear Type MIG Machine 220v - YouTube

  1. pot in A4 connection, controls oscillation range
  2. pot in A2 connection, controls oscillation speed
  3. pot in A0 connection, controls delay time between oscillations

My questions are;

  1. I can’ t control speed. What can i add to control speed with A2 connection?
  2. When it changes rotation, it stops and moves back suddenly. Probably it causes over heating of driver.
    So may i stop and force again slowly? To do this how may i modify codes?
  3. For this operation i use nema 23 with tb6560 and powering 24V 6,5A supply. Driver is heating too much. TB6560 switces are in this position;
    S1 0 S2 0 S3 0 S4 0 S5 0 S6 0
    To reduce overheating, maybe i have to adjust it in better set up?

Code below;

// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
void setup() {

// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);

}
void loop() {

int range = analogRead (A4);
int speed= analogRead (A2);
int wait= analogRead (A0);

digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

for(int x = 0; x < range; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
{ delay(wait);

{ digitalWrite(dirPin,LOW); //Changes the rotations direction

for(int x = 0; x < range; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
{ delay(wait);
}

}}}