Start a timer when detected an object, then stop timer if nothing is detected.

Hey I'm new to Arduino x c++ Language. Please respect this post, I'm only a young girl. I'm doing a project but this is a part I don't really know.

If the sensor detected an object, I wanted it to start a timer (5 mins) before the alarm triggers, but if the object suddenly left and the timer is not end yet I wanted it to go back from the start.

Some people said I need to use millis but I don not know how.

if (distance >= 50 || distance <= 0){
Serial.println("no object detected");
digitalWrite(Buzzer,LOW);
}

else {
Serial.println("object detected");
delay(100);
tone(Buzzer,400);
delay(500);
tone(Buzzer,800);
delay(100);

noTone(Buzzer);

Some people said I need to use millis but I don not know how.

In the loop() function when the object becomes detected save the value of millis() as the start time and set a boolean to true. When the object becomes not detected set the boolean to false

Later in loop() test the boolean. If it is true and if the current value of millis() minus the start time is greater than 5 minutes then sound the alarm and set the boolean to false

My preferred method in dealing with timings is to use a library called Simple Timer. If you do not have it install it. Create a timer object then simply used a lambda timing function.

#include <SimpleTimer.h>

// the timer object
SimpleTimer timer;


void SomeFunction()
{
  digitalWrite(pin, HIGH);
  timer.setInterval(5000L, []()
 {
   digitalWrite(pin, LOW);
   //do other stuff
 });

void loop()
{
  timer.run();
}

A great way to start is to study the Arduino State Change Detection example

and the Blink Without Delay example

Once you understand those two tutorials. It should be easy to write a program for your challenge.