How do I create a constant loop with an event that only occurs once? 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 &lt;= 150){
    personPresent();
    }


  if(distance &gt; 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.
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 &amp;&amp; distance &lt;= 150){
    personPresent();
    present = true;
} else if(present == true &amp;&amp; distance &gt; 150) {
    personAbsent();
    present = false;
}