How do I run a DC motor for a specific amount of time and then have it stop?

So I am writing a code for a University project involving a back massager and I am having trouble getting it all together. The basic gist is the motors in the massager should be able to run at various speeds and this can be adjustable through 2 buttons (one to increase and the other to decrease). Additionally, I want to include an auto shut-off which will automatically shut off the motors after 20 minutes of work and have the machine reset and ready to use. For now this is all I am interested in but I don’t know how to do it. Here is what I have done so far:

int speedPin = 5; //for motor 1
int speedPin2 = 6; //for motor 2
int dir1 = 4; //direction motor 1
int dir2 = 3; //direction motor 1 
int dir11 = 7; //direction motor 2
int dir22 = 2; //direction motor 2
int BP1 = 8; //button 1
int BP2 = 9; //button 2
int B1Val; //button 1 value
int B2Val; //button 2 value
int mSpeed = 0; //motor speed
int dT = 500; //delay time

unsigned long time_since_last_reset = 0;
unsigned long interval = 60000;
void setup() {
  // put your setup code here, to run once:
  pinMode(speedPin, OUTPUT);
  pinMode(speedPin2, OUTPUT);
  pinMode(dir1, OUTPUT);
  pinMode(dir2, OUTPUT);
  pinMode(dir11, OUTPUT);
  pinMode(dir22, OUTPUT);

  pinMode(BP1, INPUT);
  pinMode(BP2, INPUT);
  digitalWrite(BP1, HIGH);
  digitalWrite(BP2, HIGH);

  Serial.begin(9600);
  delay(150);
}

void loop() {
  // put your main code here, to run repeatedly:
  time_since_last_reset = millis();
  while ((millis() - time_since_last_reset) < interval) {
    B1Val = digitalRead(BP1);
    B2Val = digitalRead(BP2);

    Serial.print("Motor Speed ");
    Serial.println(mSpeed);


    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, LOW);
    digitalWrite(dir11, HIGH);
    digitalWrite(dir22, LOW);
    analogWrite(speedPin, mSpeed);
    analogWrite(speedPin2, mSpeed);

    if (B1Val == 0) {
      mSpeed = mSpeed + 10;
      delay(dT);
    }
    if (B2Val == 0) {
      mSpeed = mSpeed - 10;
      delay(dT);
    }
    if (mSpeed < 0) {
      mSpeed = 0;
    }
    if (mSpeed > 255) {
      mSpeed = 255;
    }
    
    if (mSpeed == 10) {
      mSpeed = 100;
    }
    if (mSpeed == 90 || mSpeed == 95) {
      mSpeed = 0;
    }
    
    if (mSpeed == 0) {
      analogWrite(speedPin, 0);
      analogWrite(speedPin2, 0);
    }
    if (mSpeed > 0) {
      digitalWrite(dir1, HIGH);
      digitalWrite(dir2, LOW);
      digitalWrite(dir11, HIGH);
      digitalWrite(dir22, LOW);
      analogWrite(speedPin, mSpeed);
      analogWrite(speedPin2, mSpeed);
    }    
  }
}

You will need to use millis() timing for that. Introductory tutorial here.

It looks like the basic idea is already in the posted program, but perhaps you don't fully understand it yet.

I think you need a variable (boolean) to tell you if the machine is in use. If it is, use your existing code. If it isn't read the buttons - when one is pressed, record the time, set a reasonable speed, set running to true. Use that time as the time_since_last_reset.

Can you clarify what you’re doing with this logic:

        if (mSpeed < 0) 
        {
            mSpeed = 0;
        }
        
        if (mSpeed > 255) 
        {
            mSpeed = 255;
        }
        
        if (mSpeed == 10) 
        {
            mSpeed = 100;
        }
        
        if (mSpeed == 90 || mSpeed == 95) 
        {
            mSpeed = 0;
        }

Specifically, if mSpeed equals 10 make it 100 and if it equals 90 or 95, make it zero. Seems like you’d get strange behavior with this but maybe you have good reasons for this.