if you use Bounce2 to do the heavy lifting:
#include <Bounce2.h>
const byte LedPin = 13;
const byte InputPin = 5;
const unsigned long MaxRunTime = 5000;
unsigned long startMillis;
Bounce input;
void setup(){
pinMode(LedPin, OUTPUT);
input.attach(InputPin, INPUT_PULLUP);
}
void loop(){
input.update();
if(input.rose()){
startMillis = millis();
digitalWrite(LedPin, HIGH);
}
else if(input.fell() || millis() - startMillis >= MaxRunTime){
digitalWrite(LedPin, LOW);
}
}
Only note, the write to low ma be executed multiple times. Not a problem for digitalWrite() but might be for other stuff you place there.