-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
33 lines (29 loc) · 770 Bytes
/
button.py
File metadata and controls
33 lines (29 loc) · 770 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
#button
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
class Button:
def __init__(self, pin):
self.pin=pin
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
self.state=1
self.lastdebounce=0
def run(self):
elapsedtime=time.time()-self.lastdebounce
if elapsedtime<0.01:
return False
self.lastdebounce=time.time()
state=GPIO.input(self.pin)
#print(self.state,state)
pushed=False
if self.state>state:
pushed=True
self.state=state
return pushed
if __name__=='__main__':
btn=Button(27)
while True:
time.sleep(0.001)
if btn.run()==True:
print('pressed')