Last active
August 29, 2015 14:08
-
-
Save kcranley1/26e393c233cc57330292 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python2.7 | |
# script by Sparks N Smoke based on Alex Eames at http://RasPi.tv | |
import RPi.GPIO as GPIO | |
from time import sleep | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Button 0 | |
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Button 1 | |
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Button 2 | |
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP) # Button 3 | |
GPIO.setup(24, GPIO.OUT) # LED Red | |
GPIO.setup(25, GPIO.OUT) # LED Green | |
GPIO.setup(26, GPIO.OUT) # LED Blue | |
def my_callback1(GPIO24): | |
GPIO.output(24, 1) # Red ON | |
sleep(0.5) | |
GPIO.output(24, 0) # Red OFF | |
print "BUTTON 1 PRESSED - main thread interrupted by another thread" | |
def my_callback2(GPIO25): | |
GPIO.output(25, 1) # Green ON | |
sleep(0.5) | |
GPIO.output(25, 0) # Green OFF | |
print "BUTTON 2 PRESSED - main thread interrupted by another thread" | |
def my_callback3(GPIO26): | |
GPIO.output(26, 1) # Blue ON | |
sleep(0.5) | |
GPIO.output(26, 0) # Blue OFF | |
print "BUTTON 3 PRESSED - main thread interrupted by another thread" | |
GPIO.add_event_detect(21, GPIO.FALLING, callback = my_callback1, bouncetime = 300) | |
GPIO.add_event_detect(22, GPIO.FALLING, callback = my_callback2, bouncetime = 300) | |
GPIO.add_event_detect(23, GPIO.FALLING, callback = my_callback3, bouncetime = 300) | |
try: | |
print "Main thread waiting for falling edge on port 20 - ie BUTTON 0" | |
GPIO.wait_for_edge (20, GPIO.FALLING) | |
print "Falling edge on port 20 detected. BUTTON 0 PRESSED" | |
except KeyboardInterrupt: | |
GPIO.cleanup() # Reset ports on CTRL-C | |
finally: | |
GPIO.cleanup() # Reset on normal exit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment