subroutines

Hi,
I'm new here and also new in programming arduino.
First project I started is a small home control system.
I want to program a subroutine for an time relay.
When I put the commands in the void loop, everything is working except that the output comes high after reset.
When i put the same commands in a subroutine, the output stays high after pushing the buttom ones.

int drukknop = 22;
int LED = 13;
long starttijd;

void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(drukknop,INPUT);
  //digitalWrite(LED,LOW);
}

void loop()
{
    if (digitalRead (drukknop) == LOW) {
        starttijd = millis();
        tijdrelais();
            }
}
 
void tijdrelais()
{
 if ((millis()- starttijd) < 2000){digitalWrite (LED,HIGH);}
         else {digitalWrite (LED,LOW);
}
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Schematic?

what do you mean ?

I want that after pushing the button, the output wil stay high during 5 minutes and then commes low again. (in the test it is 2 sec).

I want to see what you have got wired and how it is wired.
That is a schematic (circuit diagram).

only digital pin 22 is wired with a push button between ground and input and a pull up resistor of 1 K to 5V
The board is detecting the change of the button that's not the problem.

Vin: 5V---*
|

R 1 K
/
|
|
22----*
|
|
| push button
|
|
|
*----> Ground

You only call tijdrelais when the button is pressed. Move it out of the body of the if.

thanks. It's working

the only problem is that after reset op the arduino. The relay is going on for the programmed time. Why, I don't know because I force him low in the setup.

char drukknop = 22;
char LED = 13;
long starttijd;

void setup()
{
pinMode(LED,OUTPUT);
pinMode(drukknop,INPUT);
digitalWrite(LED,LOW);
}

void loop()
{
if (digitalRead (drukknop) == LOW) {
starttijd = millis();
}
tijdrelais();
}

void tijdrelais()
{
if ((millis()- starttijd) < 10000)
digitalWrite (LED,HIGH);
else
digitalWrite (LED,LOW);
}

It's because when you reset the arduino, millis is zero and so for the first ten seconds, this will be less than 10000:

(millis()- starttijd

One way to solve it is to turn the relay on when you detect the button press and use your subroutine to turn it off when millis()- starttijd is greater than 10K

thanks