How to start a stepper motor and change delay length from touchscreen input

I'm fairly new to arduino, I've dabbled with it here and there but I'm working on my biggest project yet and need some assistance getting it to work properly. Basically I have a touchscreen with a virtual numberpad and start/stop buttons on it, I want to be able to take the input from the numberpad and change the amount of time between each time the motor runs (so input 5000 on the number pad and the motor will output the set amount of steps every 5 seconds). That's the first issue.

The second issue, and the most important is this; I want the motor to start running once the start button is pressed on the touchscreen and to stop once when the "stop" button is pressed. So far I have been able to get the motor to take the inputted amount of steps once when the button is pressed (in this case I have it set to take 25 steps). I believe this issue could be solved with a while loop but I have no idea how to implement it into this situation.

For reference I am using an Elegoo 2.8" TFT touch screen and I have modified one of the default example programs that comes with it to fit my needs.

TL;DR: I want the number pad on the touchscreen to control the length of the delay between steps, and when I press the start button I want the motor to run until I press the stop button.

void setup(void) {

     // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT); 
  pinMode(dirPin, OUTPUT);
  Serial.println(F("TFT LCD test"));

  Serial.println(F("Using Elegoo 2.8\" TFT Arduino Shield Pinout"));
  Serial.println(F("Using Elegoo 2.8\" TFT Breakout Board Pinout"));

  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());


  uint16_t identifier = tft.readID();
  if(identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if(identifier == 0x4535) {
    Serial.println(F("Found LGDP4535 LCD driver"));
  }else if(identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if(identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if(identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else if(identifier==0x0101)
       Serial.println(F("Found 0x9341 LCD driver"));
  }else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));

  // create buttons
  for (uint8_t row=0; row<5; row++) {
    for (uint8_t col=0; col<3; col++) {
      buttons[col + row*3].initButton(&tft, BUTTON_X+col*(BUTTON_W+BUTTON_SPACING_X), 
                 BUTTON_Y+row*(BUTTON_H+BUTTON_SPACING_Y),    // x, y, w, h, outline, fill, text
                  BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolors[col+row*3], ILI9341_WHITE,
                  buttonlabels[col + row*3], BUTTON_TEXTSIZE); 
      buttons[col + row*3].drawButton();
  // create 'text field'
  tft.drawRect(TEXT_X, TEXT_Y, TEXT_W, TEXT_H, ILI9341_WHITE);

// Print something in the mini status bar with either flashstring
void status(const __FlashStringHelper *msg) {
  tft.fillRect(STATUS_X, STATUS_Y, 240, 8, ILI9341_BLACK);
  tft.setCursor(STATUS_X, STATUS_Y);
// or charstring
void status(char *msg) {
  tft.fillRect(STATUS_X, STATUS_Y, 240, 8, ILI9341_BLACK);
  tft.setCursor(STATUS_X, STATUS_Y);
#define MINPRESSURE 10
#define MAXPRESSURE 1000
void loop(void) {
  /*TSPoint p;
  p = ts.getPoint(); 
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);

  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
 // p = ts.getPoint(); 
  if (ts.bufferSize()) {
  } else {
    // this is our way of tracking touch 'release'!
    p.x = p.y = p.z = -1;
  // Scale from ~0->4000 to tft.width using the calibration #'s
  if (p.z != -1) {
    p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
    p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
    Serial.print("("); Serial.print(p.x); Serial.print(", "); 
    Serial.print(p.y); Serial.print(", "); 
    Serial.print(p.z); Serial.println(") ");
   if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = (tft.height()-map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
  // go thru all the buttons, checking if they were pressed
  for (uint8_t b=0; b<15; b++) {
    if (buttons[b].contains(p.x, p.y)) {
      //Serial.print("Pressing: "); Serial.println(b);
      buttons[b].press(true);  // tell the button it is pressed
    } else {
      buttons[b].press(false);  // tell the button it is NOT pressed

  // now we can ask the buttons if their state has changed
  for (uint8_t b=0; b<15; b++) {
    if (buttons[b].justReleased()) {
      // Serial.print("Released: "); Serial.println(b);
      buttons[b].drawButton();  // draw normal
    if (buttons[b].justPressed()) {
        buttons[b].drawButton(true);  // draw invert!
        // if a numberpad button, append the relevant # to the textfield
        if (b >= 3) {
          if (textfield_i < TEXT_LEN) {
            textfield[textfield_i] = buttonlabels[b][0];
    textfield[textfield_i] = 0; // zero terminate
           // fona.playDTMF(buttonlabels[b][0]);

        // clr button! delete char
        if (b == 1) {
          textfield[textfield_i] = 0;
          if (textfield > 0) {
            textfield[textfield_i] = ' ';

        // update the current text field
        tft.setCursor(TEXT_X + 2, TEXT_Y+10);
        tft.setTextColor(TEXT_TCOLOR, ILI9341_BLACK);

        // start motor
        if (b == 0) { 
           digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
           // Set pulses (25+) for how much you want the motor to move each time)
           for(int x = 0; x < 25; x++) {

          delay(5000); //this delay is what I want to be able to change with the numberpad
          status(F("Starting motor"));
          Serial.print("Starting motor"); Serial.print(textfield);

         // stop motor
        if (b == 2) {
          digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
          // Set pulses (25+) for how much you want the motor to move each time)
          for(int x = 0; x < 25; x++) {
          status(F("Stopping motor"));
      delay(100); // UI debouncing

IMHO you need to break that sort of program into separate functions to make it manageable. Have a look at Planning and Implementing a Program

The first part of your question involves two separate tasks - which can be explored in two separate short programs - {A} getting a value from the TFT and {B} managing the time that your motor runs

The second issue will be solved by not using delay() or delayMicroseconds(). Have a look at how millis() is used to manage timing without blocking in Several things at a time and in the Planning tutorial


// Get rid of the commented out crap BEFORE posting code here

/* Learn about the Stepper and AccelStepper libraries */