-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
48 lines (37 loc) · 1.47 KB
/
sensor.py
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
38
39
40
41
42
43
44
45
46
47
import RPi.GPIO as GPIO
import time
# Set the GPIO mode to BCM (Broadcom SOC channel numbering)
GPIO.setmode(GPIO.BCM)
# Set the pin number connected to the touch sensor
TOUCH_PIN = 12
# Set the GPIO pin as an input
GPIO.setup(TOUCH_PIN, GPIO.IN)
# Variable to track the touch sensor state
prev_touch_state = GPIO.LOW # Assuming the sensor is not touched initially
# Variable to track the start time of touch
touch_start_time = None
try:
while True:
touch_state = GPIO.input(TOUCH_PIN)
if touch_state != prev_touch_state:
if touch_state == GPIO.HIGH:
# Sensor touched event
print("Touch sensor is touched!")
# Record the start time when the touch is detected
touch_start_time = time.time()
else:
# Sensor released event
print("Touch sensor is released!")
# Reset the touch start time when sensor is released
touch_start_time = None
if touch_start_time is not None:
# Check if the touch has been held for 3 seconds
if time.time() - touch_start_time >= 3:
print("Activated")
# Reset touch start time after activation
touch_start_time = None
prev_touch_state = touch_state
time.sleep(0.1) # A small delay to debounce the input
except KeyboardInterrupt:
# Clean up the GPIO settings on program exit
GPIO.cleanup()