Count Mosfet on duration and match with equal cool down period.

I am having trouble trying to implement this in code. What I am wanting to do is count how long the transistor goes on for, and match it with an equal cool down period, which prevents the user from trying to start the gas engine until a boolean is set to true.

Also, I need the transistor to be on for no longer than 3 seconds in addition to the above.

Examples:

1: Transistor is on for 500 milliseconds, user cancels starting engine and after a pause of 500ms, user tries to start the engine again for 1200 milliseconds more, so the transistor has been on for 1700 milliseconds now, but we need to subtract the 500ms pause from when they first cancelled. So the total is 1200 milliseconds now. On every pause we need to subtract; a pause basically means a user made the pin 'Low' themself.

2: If the transistor has been on for a total of 3000 milliseconds, we forcefully digitalWrite the pin 'Low' and begin counting the 3000 millisecond cool down period before the bCanStartEngine boolean is set back to true.

Thank you for your help offered! Here is some code I am trying, but am lost.

unsigned long currentMillis = millis();

  if (digitalRead(STARTER_TRANSISTOR_PIN == HIGH))
  {
    // Starter motor MOSFET on time before we need to cool it off
    
    if (currentMillis - previousStarterOnMillis > 3000)
    {
      previousStarterOnMillis = currentMillis; 

      bCanStartEngine = false;

      digitalWrite(STARTER_TRANSISTOR_PIN, LOW);

      previousStarterOffMillis = currentMillis;
    }
  }
  else
  {
    // Starter motor MOSFET cool off time

    if (currentMillis - previousStarterOffMillis > 3000)
    {
      previousStarterOffMillis = currentMillis; 

      bCanStartEngine = true;
    }
  }

You need to scrap that code and start over. You want to do something (start the motor) when the switch BECOMES pressed, NOT when the switch IS pressed.

Get the word millis out of your time variables It contributes NOTHING. Use names that make sense.

Some time variable names like motorStarted and motorStopped would make it MUCH clearer what each variable holds, and just might provide a clue as to what the problem is.

PaulS:
You need to scrap that code and start over. You want to do something (start the motor) when the switch BECOMES pressed, NOT when the switch IS pressed.

In other words, edge detection, not level detection.

PaulS:
You need to scrap that code and start over. You want to do something (start the motor) when the switch BECOMES pressed, NOT when the switch IS pressed.

Get the word millis out of your time variables It contributes NOTHING. Use names that make sense.

Some time variable names like motorStarted and motorStopped would make it MUCH clearer what each variable holds, and just might provide a clue as to what the problem is.

An example would help me out. Thanks.

An example would help me out.

The state change detection example wasn't good enough?

Or, are you referring to the proper names for the time variables? I could make some suggestions, but you probably wouldn't like them.

PaulS:
The state change detection example wasn't good enough?

Or, are you referring to the proper names for the time variables? I could make some suggestions, but you probably wouldn't like them.

I didn't see any code in your post, so not sure what example you were referring to.