Output outside loop

Hi everyone,

I am using a maxbotix ultrasound sensor to measure distance. Now I want to give an output to a digital out only twice, with a delay in between as long as the distance is smaller than a given distance.

if (disVal <= 16 {
    digitalWrite(muisPin, LOW);
    digitalWrite(muisPin, HIGH);

Does anyone know who I can get this piece of code to only work once (in the void loop() in keeps running) ?


void loop()
    static byte done_it = 0;

    if (!done_it)
        if (somecondition)


"static" causes the variable to retain its value between function calls; it also means the variable is initialized once, at the beginning of program execution (as opposed to once every function call).

The opposite of static is automatic, and automatic is the default. Automatic variables get allocated and initialized once per function call and do not retain their values between calls.

hope this helps.


Thank you very much for the info about static variables. I have the code working the way I like now. And in the future I will most likely use the static more often.

Cheers Erik