user19082: This is my program. The LED should turn off after four seconds.
int vraag = 7;
int gloeikaars = 5;
unsigned long previousMillis3;
boolean gloeistate = true;

void setup() {
    pinMode(gloeikaars, OUTPUT);
}

void loop() {
    int x = digitalRead(vraag);
    if (x == 0) {
        digitalWrite(gloeikaars, !LOW);
    } else {
        Gloeikaars();    
    }
}

// *********************************************************************
// Gloeikaars aansturing
// *********************************************************************
void Gloeikaars() {
    unsigned long huidig = millis();
    digitalWrite(gloeikaars, !HIGH);
    if ((gloeistate == true) && (huidig - previousMillis3 >= 4000)) {
        huidig = previousMillis3;
        digitalWrite(gloeikaars, !LOW);
        gloeistate = false;
    }
}

Can someone help me because it won't work?

user6628: Your previousMillis variable is never set.  Two changes:
In setup:
...
previousMillis3 = millis();
...

In loop:
...
previousMillis3 = huidig;
...


19082: <p>This is my program. The LED should turn off after four seconds.</p>

<pre class="lang-c++ prettyprint-override"><code>int vraag = 7;
int gloeikaars = 5;
unsigned long previousMillis3;
boolean gloeistate = true;

void setup() {
    pinMode(gloeikaars, OUTPUT);
}

void loop() {
    int x = digitalRead(vraag);
    if (x == 0) {
        digitalWrite(gloeikaars, !LOW);
    } else {
        Gloeikaars();    
    }
}

// *********************************************************************
// Gloeikaars aansturing
// *********************************************************************
void Gloeikaars() {
    unsigned long huidig = millis();
    digitalWrite(gloeikaars, !HIGH);
    if ((gloeistate == true) &amp;&amp; (huidig - previousMillis3 &gt;= 4000)) {
        huidig = previousMillis3;
        digitalWrite(gloeikaars, !LOW);
        gloeistate = false;
    }
}
</code></pre>

<p>Can someone help me because it won't work?</p>
 6628: <p>Your <code>previousMillis</code> variable is never set.  Two changes:</p>

<p>In setup:</p>

<pre><code>...
previousMillis3 = millis();
...
</code></pre>

<p>In loop:</p>

<pre><code>...
previousMillis3 = huidig;
...
</code></pre>
