Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- # demo of "BOTH" bi-directional edge detection
- # script by Alex Eames http://RasPi.tv
- # http://raspi.tv/?p=6791
- import RPi.GPIO as GPIO
- from time import sleep # this lets us have a time delay (see line 12)
- from datetime import datetime
- portToUse = 27
- lastTime = datetime.now()
- GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
- GPIO.setup(portToUse, GPIO.IN) # set GPIO as input (button)
- # Define a threaded callback function to run in another thread when events are detected
- def my_callback(channel):
- global lastTime
- nowTime = datetime.now()
- delta = nowTime - lastTime
- lastTime = nowTime
- if GPIO.input(portToUse): # if port == 1
- print "%d ms Rising on %d " % (delta.microseconds, portToUse)
- else: # if port != 1
- print "%d ms Falling on %d " % (delta.microseconds, portToUse)
- # when a changing edge is detected on port 27, regardless of whatever
- # else is happening in the program, the function my_callback will be run
- GPIO.add_event_detect(portToUse, GPIO.BOTH, callback=my_callback)
- print "Program will finish after 30 seconds or if you press CTRL+C\n"
- print "Make sure you have a button connected, pulled down through 10k resistor"
- print "to GND and wired so that when pressed it connects"
- print "GPIO port 27 (pin 22) to GND (pin 6) through a ~1k resistor\n"
- print "Also put a 100 nF capacitor across your switch for hardware debouncing"
- print "This is necessary to see the effect we're looking for"
- raw_input("Press Enter when ready\n>")
- try:
- print "When pressed, you'll see: Rising Edge detected on %d" % portToUse
- print "When released, you'll see: Falling Edge detected on %d" % portToUse
- sleep(30) # wait 30 seconds
- print "Time's up. Finished!"
- finally: # this block will run no matter how the try block exits
- GPIO.cleanup() # clean up after yourself
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement