How to Stop DC Motors after a given time

So I'm building a self-propelled vehicle made up of 3 wheels and want them all to stop spinning after after given amount of seconds/milliseconds. How would I write the programming to do so? Right now they just spin forever. This is what I currently have for just wheel/motor:

int motor1Speed = 100;

int motorPin1 = 9;

void setup() {
}

void loop() {
analogWrite(motorPin1, motor1Speed);
delay(2);
}

int motor1Speed = 100;

int motorPin1 = 9;
long int stop1;

void setup() {
  stop1=millis()+3000;  // 3 seconds
}

void loop() {
  if (millis()<stop1) {
     analogWrite(motorPin1, motor1Speed);
  } else {
     analogWrite(motorPin1, 0);
  }
  
}

Eventually you need some kind of logic that sets stop1 to a future time according to make the motor run again.

@rw950431 do you have any idea how I could do this: " Eventually you need some kind of logic that sets stop1 to a future time according to make the motor run again."
because I'm trying to make a loop with a Button. When the button is pressed it stops running the dc motor after 3 seconds, but the next time I press the button it does not work. Any idea how I could code, so the button works everytime it is pushed t?

Save the value of millis() when you press the button as in this pseudo code

if button is pressed
   timeButtonPressed = millis();

then let loop() check repeatedly to see if the 3 seconds has elapsed

if (millis() - timeButtonPressed >= 3000) {
    // code to stop the motor
}

...R

rw950431:

long int stop1;

//...
stop1=millis()+3000;  // 3 seconds
//..

No. That is a terrible thing to show somebody new. millis() variables are 'unsigned long' and unsigned math is always guaranteed to work when you do subtraction to calculate elapsed time. Never project out into the future and wait for millis() to catch up. In this case, it works, but will not if this is a long running program and millis() every rolls over.