/
button_int.py
executable file
·71 lines (53 loc) · 1.54 KB
/
button_int.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
# handle the button event
def buttonEventHandler (pin):
global button_push
print ("handling button event ", pin)
button_push += 1
while not GPIO.input(pin):
print ("False", button_push)
time.sleep(.5)
# turn the green LED on
GPIO.output(25,True)
time.sleep(1)
# turn the green LED off
GPIO.output(25,False)
# main function
def main():
global button_push
# tell the GPIO module that we want to use
# the chip's pin numbering scheme
GPIO.setmode(GPIO.BCM)
# setup pin 23 as an input
# and set up pins 24 and 25 as outputs
GPIO.setup(23,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)
# tell the GPIO library to look out for an
# event on pin 23 and deal with it by calling
# the buttonEventHandler function
GPIO.add_event_detect(23,GPIO.FALLING, bouncetime=200)
GPIO.add_event_callback(23,buttonEventHandler)
# turn off both LEDs
GPIO.output(25,False)
GPIO.output(24,True)
# make the red LED flash
while True:
GPIO.output(24,True)
time.sleep(1)
GPIO.output(24,False)
time.sleep(1)
print ("main ", button_push)
if button_push > 7:
button_push = 0
GPIO.cleanup()
if __name__=="__main__":
try:
button_push=2
main()
finally:
GPIO.cleanup()
print("Closed Everything. END")
#End