How to program a pushbutton to stay on and go off?

I just want to see an example code that does the following:

Lets say i have an bulb wired to an relay and arduino that switches relay on/off with a pushbutton.

Regardless of how long I hold the button down, I want the bulb to light up for just 1 second before turning off again.

If i release the button and push it again i want the same to happend.

I want to trigger a garage door opener and my opener must be hold down for 1 second before the door moves. If i hold the button too long nothing happennd.

Therefore i want to give power to the trigger for one second before it turns off regardless to how long i hold the button down.

Sorry for my bad english, but i just want to see an example code that gives me something to work with :slight_smile:

You need a program that checks when the button changes from not-pressed to pressed. And when that happens it switches on the relay and saves the value of millis(). Then it checks to see 1000 millisecs has elapsed and when it has it turns off the relay. Something like this - which should be in loop() or a function called from loop()

prevButtonState = newButtonState;
newButtonState = digitalRead(buttonPin);

if (newButtonState == LOW and prevButtonState == HIGH) { // assumes LOW when pressed
  digitalWrite(relayPin, HIGH);  // not sure if you need a HIGH or a LOW to turn the relay ON
  relayStartTime = millis();
}

if (millis() - relayStartTime >= 1000) {
  digitalWrite(relayPin, LOW);
}

...R

Thank you wery mutch :slight_smile: