Last active
July 24, 2019 08:59
-
-
Save ritiek/eb268fc0d917e0d11d077511dfe28dcd to your computer and use it in GitHub Desktop.
Detect 4-pin push switch presses with Raspberry Pi's GPIO pins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code stolen shamelessly from | |
# https://www.raspberrypi.org/forums/viewtopic.php?p=876947&sid=44c21629719b08f74de2b3b465db8d5c#p876947 | |
# I merely modified some bits | |
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode(GPIO.BCM) | |
pin = 4 | |
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
presses = 1 | |
def my_callback(channel): | |
global presses | |
print("callback called " + str(presses) + " times") | |
presses += 1 | |
GPIO.add_event_detect(pin, GPIO.FALLING, callback=my_callback, bouncetime=500) | |
print("Waiting") | |
while True: | |
try: | |
time.sleep(5) | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment