-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir.py
More file actions
37 lines (30 loc) · 987 Bytes
/
ir.py
File metadata and controls
37 lines (30 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import RPi.GPIO as GPIO
import time
import subprocess
import signal
# Define the GPIO pins connected to the IR sensor
IR_PIN = 23
# Setup GPIO mode and pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(IR_PIN, GPIO.IN)
try:
while True:
# Read the state of the IR sensor
ir_state = GPIO.input(IR_PIN)
# Check if an object is detected by the IR sensor
if ir_state == GPIO.LOW:
print("Object detected!")
# Open the file app.py using subprocess
process = subprocess.Popen(["python", "app.py"])
# Wait for 30 seconds
time.sleep(30)
# Terminate the subprocess after 30 seconds
process.terminate()
print("App terminated.")
else:
print("No object detected")
time.sleep(0.1) # Adjust sleep time as needed
except KeyboardInterrupt:
print("Exiting...")
finally:
GPIO.cleanup() # Clean up GPIO settings before exiting