Button short-press led on for like 5 sec.

Hi im looking for someone hwo knows alot and have 5 minuttes in spare.

What im searching for is a sketch which uses the millis function.
a short press on a button and led goes on and stay´s on for 5 sec.
easy with the “delay” but a pain in my … :’( with millis…
i searched for many sketches IN THE INTERNET and there are a 1000 with blinking delays!! but no one with a “off delay” function…

any one have a sketch like that laying around…

const int ledPin = 8;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 5000;
const int buttonPin = 2;
int buttonState = 0;

void setup() {

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop()
{
buttonState = digitalRead(buttonPin);

unsigned long currentMillis = millis();

Serial.println(buttonState);

if (buttonState == HIGH){
currentMillis = previousMillis;

if (ledState == LOW)
ledState = HIGH;

digitalWrite(ledPin, ledState);
}

if(currentMillis - previousMillis >= interval); {

ledState = HIGH;

}

*/
}

const byte buttonPin = 2;
const byte ledPin = 13;
unsigned long previousTime;

void setup()
{
 pinMode(buttonPin, INPUT_PULLUP);
 pimMode(ledPin, OUTPUT);
}

void loop()
{
 byte myButton = digitalRead(buttonPin);
 
 if(myButton == LOW) // button is pressed
 {
   digitalWrite(ledPin, HIGH);
   previousTime = millis();
 }
 
 if((millis() - previousTime) >= 5000UL)
   digitalWrite(ledPin, LOW);
}

The demo several things at a time illustrates how to use millis()

There is some neat code for reading a button here.

...R

Instead of looking for code to copy and paste, try understanding how the code you have works. Once you get that then you can use this trick anywhere you want without having to find someone else's code.

Think about it, it is a simple concept. Note the time when something happens. Then later, you can subtract that from the current time to find out how long it has been since that thing happened. It really is just that simple.