stepper.h defining stepper speed?

Hi guys, I’m new to Arduino and coding but have been having a bit of fun while learning. Recently I made a circuit using a A4899 stepper driver and it does exactly as I want however I’m struggling trying to figure out the code to do different things and was hoping you guys could help me learn. For example, in the code below I can understand to a degree how it’s working. What I’d like to know is what defines the initial speed? I realize that the delay will make my motor go faster or slower but based on what? Is this defined in the stepper.h library definitions? Is max speed when delay is set to 0 and cannot go any faster? Also, what If I wanted it go X speed in one direction and a different speed in the opposite direction? Any help understanding is much appreciated.

#include <Stepper.h>



// define a constant value named stepPin and assign the value 9 to it
// this assumes digital pin 9 of Arduino is attached to the step input of driver
#define stepPin 9
 
// define a constant value named dirPin and assign the value 8
// this assumes digital pin 8 of Arduino is attached to the step input of your driver
#define dirPin 8
 
// define the pins on N.O. (normally open) buttons
#define button1 2
#define button2 3
 
// setup() loop, the Arduino runs through this once
void setup() {
  // digital pins on the Arduino can only be either set as an output or input - in our case we want to send data to the driver, so we choose output
  pinMode(stepPin , OUTPUT);
  pinMode(dirPin , OUTPUT);
 
  // define button pins as input pullup type
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
 
  // set an initial value of low to both our step and dir pins, we could easily write false or 0 instead of LOW
  digitalWrite(stepPin , LOW);
  digitalWrite(dirPin , LOW);
}
 
// loop() loop, the Arduino continuously cycles through this as fast as it can
void loop() {
 
  if (digitalRead(button1) == LOW && digitalRead(button2) == HIGH) { // if button1 is pressed and button2 is not pressed
    digitalWrite(dirPin, LOW); // move in the LOW direction
  } else if (digitalRead(button1) == HIGH && digitalRead(button2) == LOW) { // if button1 is not pressed and button2 is pressed
    digitalWrite(dirPin, HIGH); // move in HIGH direction
  }
 
  if (digitalRead(button1) == LOW || digitalRead(button2) == LOW) { // if either button is pressed
 
    // set a HIGH value to our step pin, this turns the voltage on for that pin
    digitalWrite(stepPin , HIGH);
 
    // let's wait here for 50 milliseconds; note the units, this means 0.05s
    delay(2);
 
    // let's set our step pin to false, this turns the voltage off for that pin and gives us the on/off cycle we need
    digitalWrite(stepPin , LOW);
 
    // wait another 50 milliseconds after which time we loop back to the beginning of the loop() loop
    delay(2);
 
  }
}

Whats an A4899? Do you mean an A4988?

Anyway such stepper drivers are very simple - you send a step pulse, it steps the motor. Nothing
more to it. Generating step pulses in real time with a suitable profile (ramping speed up and down)
is where the cleverness lies. Suggest you play with AccelStepper library, and then have a look in
its source code to see one way of achieving smooth motor control.

I did mean the A4988 and thanks for the suggestion of the AccelStepper lib.

You mentioned the step pulse, what/how is that defined? Is that data contained within the library/s? Or does this statement mean that it can only go as fast as the Arduino is capable? (that might not make sense) Sorry for my lack of terminology.
"// loop() loop, the Arduino continuously cycles through this as fast as it can void loop()"

Thanks again

The AccelStepper lib is only capable to pulse your stepper with ca. 4000 steps/sec.
Be aware that the library is very comfortable (providing for acceleration/deceleration, which is pretty complicated if you would try to do it on your own)), but that needs computing power (and time).

Therefor you only can gain speed over that 4000 steps/sec using AccelStepper with a faster Arduino.