Using Millis() twice in the same sketch

HI i´m trying to make a timer for a 2 dc motors. What I´m triying to do is, when serial imput is 'a', motor 1 start, then when serial imput is 'b', motor 2 start and motor 1 keep runnig for just 3000millisecond more.

I had been trying a lot of function whitout succses. I´m just a beginner programming.

If some can give a new idea, I will appreciate a lot.

Pd: I´m using a Adafruit Shield for dc motors.

My code is this :

//

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);


boolean motorRunning = false;
unsigned long motorStartMillis;
unsigned long interval = 3000;
unsigned long previousMillis = 0;        // will store last time LED was updated


void setup() {

Serial.begin(57600);
  
 
  pwm1.begin();
  pwm1.setPWMFreq(100);  // This is the maximum PWM frequency  
}

void loop()
{
   if (Serial.available() > 0) {
    int inByte = Serial.read();
    
    switch (inByte) {
    case 'a': 
      
      motor1();
              
      break;
    case 'b':    
      pwm1.setPWM(2, 0, 4000);
      pwm1.setPWM(3, 0, 4000);
      
      pwm1.setPWM(13, 0, 4000); 
      pwm1.setPWM(14, 0, 4000);
      
      break;
    //case 'c':    
    //  digitalWrite(4, HIGH);
      break;
   // case 'd':    
   //   digitalWrite(5, HIGH);
   //   break;
   // case 'e':    
   //   digitalWrite(6, HIGH);
   //   break;
    default:
      // turn all the LEDs off:
      pwm1.setPWM(0, 0, 0);
      pwm1.setPWM(1, 0, 0);
      
      pwm1.setPWM(11, 0, 0); 
      pwm1.setPWM(12, 0, 0);
      
      pwm1.setPWM(2, 0, 0);
      pwm1.setPWM(3, 0, 0);
      
      pwm1.setPWM(13, 0, 0); 
      pwm1.setPWM(14, 0, 0);
         
    } 
  }
}

void motor1() 
{
unsigned long currentMillis = millis();

      pwm1.setPWM(0, 0, 4000);
      pwm1.setPWM(1, 0, 4000);
      
      pwm1.setPWM(11, 0, 4000); 
      pwm1.setPWM(12, 0, 4000);
     
while ( millis() > currentMillis + 3000) {
      pwm1.setPWM(0, 0, 0);
      pwm1.setPWM(1, 0, 0);
      
      pwm1.setPWM(11, 0, 0); 
      pwm1.setPWM(12, 0, 0);
}
}
while ( millis() > currentMillis + 3000) {
      pwm1.setPWM(0, 0, 0);
      pwm1.setPWM(1, 0, 0);
      
      pwm1.setPWM(11, 0, 0); 
      pwm1.setPWM(12, 0, 0);
}

Since you just set currentMillis to now, is now going to be greater than a time three seconds in the future?

Of course not, so the while loop will never accomplish anything.

You need to record when you start the first motor, in a variable with a name that is better than currentMillis.

You need to, on every pass through loop(), independent of whether there is serial data to process, determine if the motor is running and has been running long enough.

You do not do that in motor1().

Have a look at how millis() is used in several things at a time.

And note that millis() should always be tested using subtraction so that you have no problem when it rolls over to 0 again.

...R