Stepper Motor Speed

Hello All,
This is the first time posting so forgive me. I am trying to code for a stepper motor using a driver. I cannot get the rpms anywhere close enough for what I need. I think I am doing it wrong in the coding. If someone can just take a look at my code and see if I am getting the highest rpms with the code I am using In the setup I have a Home move that is pretty straight forward using a micro switch. Then in loop I am moving the stepper in both directions with a pot to control the min /max speed.
Arduino Mega
DM542T driver
nema 23 stepper motor 1.8 deg 3.0 amp with brake
Here is my code

//stepper motor and driver controls
int pulse=6; //pulsing the stepper motor
int dir=8;   //direction of stepper motor
int speed=1;  // delay between pulses
int brake=A15; //stepper motor brake  LOW=off HIGH
//control inputs
int airCyl=A11;  // this controls the forward moving air cylinder HIGH=forward LOW=back
// Limit switches
int sw1=4;    //home optical switch 0=not activated 1=activated
int sw2=5;    //end optical switch

//varibles and macros
int steps=2;    //used to set home pos after homing is completed
int potPin=A4; //analog pin for poteniometer for speed
int distance = 0;
int buttonState = 0;
int onOrOffState = 0;
int endLimit = 0;
//int start=10;
int speedCount = 100;
int goBack = 0;

//Define the pins Used

void setup() {
  delay(5000);
 
  pinMode(brake, OUTPUT);  //sets A15 to output
  pinMode(airCyl, OUTPUT);
  Serial.begin(9600);
  pinMode(pulse, OUTPUT);  //set pulse pin to output to stepper driver
  pinMode(dir, OUTPUT);  //set direction control to stepper driver
  pinMode(sw1, INPUT_PULLUP); // Set home switch to input
  pinMode(sw2, INPUT_PULLUP);//set end/limit switch to input
  pinMode(10, INPUT_PULLUP);//start button

 //Homing procedure for Motor
 //Start moving up twoards the home switch
  digitalWrite(brake, LOW);// turn stepper motor brake off
  delay(10);
 while (!digitalRead(sw1))  { //Do this until sw1 is activated
  digitalWrite(dir, LOW);  //HIGH=COunter Clockwise LOW=Clockwise
  digitalWrite(pulse, HIGH);  //start puling the motor
  delay(speed);                  //sets the speed of the homing
  digitalWrite(pulse, LOW);
  delay(speed);
  //Serial.println(pulse);
 }
 // Move off of home switch SW1
 while (digitalRead(sw1))  {  //do this untill sw1 is not activated
  digitalWrite(dir, HIGH);
  digitalWrite(pulse, HIGH);
  delayMicroseconds(1);
  digitalWrite(pulse, LOW);
  delay(1);
 }
 
 distance = 0;  //Reset distance varible to 0
  //Serial.println(onOrOffState);  //debugging info should not be read
digitalWrite(brake, HIGH);  //turn off stepper motor brake
}// end of set up

void loop() {
 
 
   //Start button wait  
  buttonState = digitalRead(10);
  if (buttonState == LOW)  { //Low means button is pressed
      onOrOffState = 1; 
      digitalWrite(brake, LOW);// Turn stepper motor brake off
      digitalWrite(airCyl, LOW);//Move the platform forward
      delay(5000);//Adjustable delay time for platform to move and heat up tube
      
  }
  // button push changes state and now motorl moves twoards end switch
    while (onOrOffState == 1) {
     //speed control
   if(speedCount == 0) {
     speed = analogRead(potPin); //analog adjust to speed of movement of platform
      speed = map(speed,0, 1023, 25, 1);// 0-1023 arduino voltage maped to 25-1 pulse speed to motor
      //Serial.println(speed);//debugging info
      speedCount = 100;}//time delay for reading the analog read on speed poteniometer
    digitalWrite(dir, HIGH);
    digitalWrite(pulse, HIGH);
    delayMicroseconds(1);
    digitalWrite(pulse, LOW);
    delay(1);
    distance = distance + 1;//start counting distance from home position
    speedCount = speedCount - 1;//start counting for read on speed potinometer
    //Serial.println(speedCount);//debugging info
   Serial.println(distance);//debugging info
     endLimit = digitalRead(5);//setting endlimit varible to pin 5 or sw2
        if (digitalRead(sw2))  { //Do this until sw2 is activated
        digitalWrite(airCyl, HIGH);//move platform back
        //delay(10);
        onOrOffState = 0;
       goBack = 1;}
             
    while(goBack == 1) {// move back the same amount of steps that it moved forward
          
                
          digitalWrite(dir, LOW);
          digitalWrite(pulse, HIGH);
          delayMicroseconds(speed);
          digitalWrite(pulse, LOW);
          delay(1);
          distance = distance - 1;// minus the distance
          Serial.println(distance);
            if (distance == 0)  {//when distance hits 0 set goback varible to 0
             goBack = 0;}
    }
        }    
            
    


          
      
    
  }

I suspect the voltage you are running the stepper at is really too low. I see you do not wait for the brake to be released. Is it really instantaneous? Please identify the power supply you are using for the motor.
Paul

I am running a 24 vdc 15a power supply. That should be plenty for a 3 amp motor.

kashari:
I am running a 24 vdc 15a power supply. That should be plenty for a 3 amp motor.

Yes, it does. What happens when you run other stepper test programs?
Paul

I doubt your motor can go from zero to 1000 steps per second instantly, you need to accelerate gradually. Check out the AccelStepper library.
https://www.airspayce.com/mikem/arduino/AccelStepper/

Its completely essential to ramp step rates with a large stepper such as NEMA17 or larger if you
want full performance. You have to accelerate the rotor smoothly or it will simply stall immediately
on a large change in step-rate. Libraries like AccelStepper are a good starting point.

If the motor has an electromagnetic brake it may need much more than 10ms to release (think 0.5s or
so - the magnetic field has to die right down in the solenoid).