-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuttontest.py
More file actions
executable file
·65 lines (44 loc) · 1.25 KB
/
Copy pathbuttontest.py
File metadata and controls
executable file
·65 lines (44 loc) · 1.25 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
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
# import time # for sleep
import time # for sleep
# for poweroff
from subprocess import call
#------
# init
#------
GPIO.setmode(GPIO.BCM) # use the GPIO names, _not_ the pin numbers on the board
# pins BCM BOARD
ledPin = 27 # pin 13
switchPin = 17 # pin 11
# buttonCounter
buttonPressed = 0
# setup
print('setup...')
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # waits for LOW
# switch detection by interrupt, falling edge, with debouncing
def my_callback(answer):
global buttonPressed
buttonPressed += 1
print 'Button on GPIO ' + str(answer) + ' pushed the ' + str(buttonPressed) + ' time.'
# add button pressed event detector
GPIO.add_event_detect(switchPin, GPIO.FALLING, callback=my_callback, bouncetime=200)
# timing
secs = 5
#------
# loop
#------
print('Press button now (5 secs)...!')
# turn LED on
GPIO.output(ledPin, GPIO.LOW)
# delay
time.sleep(secs)
# turn LED off
GPIO.output(ledPin, GPIO.HIGH)
# cleanup
GPIO.remove_event_detect(switchPin)
GPIO.cleanup()