Sensor, if statement activating loop once.

So here is my code. I simply want the loop to play once when the sensor is activated. My problem is, if the sensor is covered I want the loop to play once, although when the sensor is covered for a long period of time, the loop doesn’t stop. How do I fix this?

sensor_test.ino (391 Bytes)

How do I fix this?

Set a boolean to false then when the sensor is activated only respond if the boolean is false and set the boolean to true and take the required actions

Should you need to activate the sensor again then set the boolean to false