Skip to content

Instantly share code, notes, and snippets.

@fvdbosch
Last active March 25, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvdbosch/96e831aa248066b0ade65d8cd1ae33ff to your computer and use it in GitHub Desktop.
Save fvdbosch/96e831aa248066b0ade65d8cd1ae33ff to your computer and use it in GitHub Desktop.
GPIO Button Adapter Demo
#!/usr/bin/env python
import time
import scrollphathd
import RPi.GPIO as GPIO
# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Button callback
def my_callback(channel):
scrollphathd.clear()
if(channel == 5):
scrollphathd.write_string("1")
elif(channel == 6):
scrollphathd.write_string("2")
elif(channel == 12):
scrollphathd.write_string("3")
elif(channel == 13):
scrollphathd.write_string("4")
elif(channel == 16):
scrollphathd.write_string("5")
elif(channel == 26):
scrollphathd.write_string("6")
else:
scrollphathd.write_string("0")
scrollphathd.show()
# Wait for button presses
GPIO.add_event_detect(5, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(6, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(12, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(13, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(16, GPIO.FALLING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(26, GPIO.FALLING, callback=my_callback, bouncetime=300)
# Pause here, exit on Enter
raw_input("Press Enter to exit\n>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment