How to write multiple if statements that depend on one another

Howdy folks, let me preface with this: I have very little experience with programming, but I am very interested in learning more about how all of this works! I am trying to utilize a light sensor for my current project and am not entirely sure how to go about it, or, if what I am trying to do is even possible. I tried to Google this before coming to the forum, but I'm not even sure what the right terms are for what I am trying to do, so bear with me!

This is the general idea: I want to run a light sensor that, when it detects light (from a laser), signals a green LED to turn on. When the laser shining on the sensor is obstructed however for more than 5 seconds, I would like it to turn on a red LED/activate a buzzer. The part I am struggling with though is: if the laser becomes unobstructed within the 5 second time frame, then the process for the red LED/buzzer being turned on is deactivated, and the green LED turns back on. Also, if the 5 seconds do pass, and the red LED/buzzer turns on and then the laser becomes unobstructed, the system resets back to its original point where only the green LED is running.

Is this possible, and if so, how might I go about this? I apologize if this isn't very clear, but I can try my best to answer any questions!

Thank you!

You can use [u]logical "AND"[/u] & "OR" statements, and [u]greater- than[/u] or less-than, etc., with your if-statements.

You can also use nested if-statements (where the 2nd if-statement doesn't get evaluated unless the 1st one is true, etc.

...Work on one little thing at a time an "develop" your code, testing as you go.

All quite possible.

The key is in determining whether 5 seconds has elapsed whilst monitoring a sensor. The delay() function is no good for this because it blocks program execution for the duration of the delay

So, you need a different, non blocking, method to determine how much time has passed since an event occurred. Meet your new friend the millis() function. When called it returns the number of milliseconds since the system was started or reset

So, when the event occurs save the value of millis() in a variable. If you check later whether the current value of millis() minus the start time is greater than 5 seconds then 5 seconds has passed. If so then act on it, if not then do anything needed in the program

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE