user41736: I am currently working in void loop() and have setup a distance meter that constantly checks the distance.
Once the distance threshold is met, a function is called. Now I only need this function to be called ONCE. But because the distance meter is constantly scanning (which I need), the function is also being repeatedly called. I tried making some sort of timer but I can't figure it out. Can someone please have a look at this?
if (distance <= 150){
    personPresent();
    }


  if(distance > 150){
    personAbsent();
      }

I need certain events within the if statement to occur only once. E.g., I want to register when there is a personPresent and have that event not fire repeatedly.
In this case, personPresent() is attached to a GPIO pin which triggers a buzzer sound. As it is now, the buzzer keeps on ringing as long as distance is less then 150. I need the buzzer to make a sound only ONCE and then not buzz for at least 5 minutes.

user38949: What you want is not to check the distance but to check that the distance condition changed.
static boolean present = false;

Inside loop:
if (present == false && distance <= 150){
    personPresent();
    present = true;
} else if(present == true && distance > 150) {
    personAbsent();
    present = false;
}


41736: <p>I am currently working in void loop() and have setup a distance meter that constantly checks the distance.</p>

<p>Once the distance threshold is met, a function is called. Now I only need this function to be called ONCE. But because the distance meter is constantly scanning (which I need), the function is also being repeatedly called. I tried making some sort of timer but I can't figure it out. Can someone please have a look at this?</p>

<pre><code>if (distance &lt;= 150){
    personPresent();
    }


  if(distance &gt; 150){
    personAbsent();
      }
</code></pre>

<p>I need certain events within the if statement to occur only once. E.g., I want to register when there is a personPresent and have that event not fire repeatedly.</p>

<p>In this case, personPresent() is attached to a GPIO pin which triggers a buzzer sound. As it is now, the buzzer keeps on ringing as long as distance is less then 150. I need the buzzer to make a sound only ONCE and then not buzz for at least 5 minutes.</p>
 38949: <p>What you want is not to check the distance but to check that the distance condition changed.</p>

<pre><code>static boolean present = false;
</code></pre>

<p>Inside loop:</p>

<pre><code>if (present == false &amp;&amp; distance &lt;= 150){
    personPresent();
    present = true;
} else if(present == true &amp;&amp; distance &gt; 150) {
    personAbsent();
    present = false;
}
</code></pre>
