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;
}
}