Code: Select all
#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv
# http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# GPIO 23 & 17 set up as inputs, pulled up to avoid false detection.
# Both ports are wired to connect to GND on button press.
# So we'll be setting up falling edge detection for both
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# GPIO 24 set up as an input, pulled down, connected to 3V3 on button press
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
time_stamp = time.time()
time_stamp2 = time.time()
# now we'll define two threaded callback functions
# these will run in another thread when our events are detected
def my_callback():
global time_stamp # put in to debounce
time_now = time.time()
if (time_now - time_stamp) >= 0.3:
print "falling edge detected on 17"
time_stamp = time_now
def my_callback2():
global time_stamp2 # put in to debounce - note we need a second time stamp
time_now = time.time()
if (time_now - time_stamp2) >= 0.3:
print "falling edge detected on 23"
time_stamp2 = time_now
print "Make sure you have a button connected so that when pressed"
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n"
print "You will also need a second button connected so that when pressed"
print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)\n"
print "You will also need a third button connected so that when pressed"
print "it will connect GPIO port 17 (pin 11) to GND (pin 14)"
raw_input("Press Enter when ready\n>")
# when a falling edge is detected on port 17, regardless of whatever
# else is happening in the program, the function my_callback will be run
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback)
# when a falling edge is detected on port 23, regardless of whatever
# else is happening in the program, the function my_callback2 will be run
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback2)
try:
print "Waiting for rising edge on port 24"
GPIO.wait_for_edge(24, GPIO.RISING)
print "Rising edge detected on port 24. Here endeth the third lesson."
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit
Traceback (most recent call last):
File "test1.py", line 6, in <module>
GPIO.setmode(GPIO.BCM)
AttributeError: 'module' object has no attribute 'setmode'
I tried GPIO.BOARD too and it still not working. I already installed RPi.GPIO most current version 0.5.1a. Please help me, your help would be appreciated.