-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttonTest.py
More file actions
53 lines (41 loc) · 1.12 KB
/
buttonTest.py
File metadata and controls
53 lines (41 loc) · 1.12 KB
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
48
49
50
51
52
53
from time import sleep,time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
global button_state
global button_toggle
buttontoggle = False
button_state = 0
def buttonStatus(setStatus=None):
global button_state
if setStatus != None:
button_state = setStatus
return button_state
start = 0
def my_callback(channel):
global start
global buttontoggle
if GPIO.input(15) == 1:
if buttontoggle == False:
start = time()
buttontoggle = True
#print("Button pressed")
else:
#print("Button released")
buttontoggle = False
elapsed = time() - start
print(elapsed)
if elapsed >= 6:
buttonStatus(2)
print("button status 2")
elif elapsed >= 0.1:
buttonStatus(1)
print("Button status 1")
else:
print("button status 0")
buttonStatus(0)
GPIO.add_event_detect(15, GPIO.BOTH, callback=my_callback, bouncetime=200)
if __name__ == '__main__':
while True:
print(buttonStatus())
sleep(1)