HCSR04 Sensor and Programming Serial Monitor Outputs to execute block of code

Hello I'm very new to c++ and I am currently working on a project that requires a block of code to be executed when all conditions are met:

Condition 1: the object must be detected within 80cm or less.

Condition 2: the object must be within that 0-80cm range for 5 or more seconds.

Condition 3: when both condition 1 and 2 are met, when the object is no longer in range (more than 80cm), execute block of code.

I was recommended using a timer library for this, but a simpler idea I had was to take advantage of the serial monitor displaying all the distances each second.

Is there a way to program this:
If the next 5 or more distances (consecutively) that are recorded each second are within 0-80cm
Confirm that the block of code can be executed. (boolean maybe? idk)
Execute code when distance recorded is greater than 80cm.

Thanks!

fusrada:
Is there a way to program this:
If the next 5 or more distances (consecutively) that are recorded each second are within 0-80cm
Confirm that the block of code can be executed. (boolean maybe? idk)
Execute code when distance recorded is greater than 80cm.

Completely yes, you can refer to the code in Arduino - Ultrasonic Sensor triggers LED tutorial and using millis() for timing.
Try to write the code, if it does not work as expected, post code and wiring diagram in this thread