Skip to content

Instantly share code, notes, and snippets.

@rxseger rxseger/gptest.py
Created Jul 11, 2016

Embed
What would you like to do?
Raspberry Pi 3 GPIO testing pushbutton input and LED output
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
BTN_G = 11 # G17
BTN_R = 12 # G18
BTN_Y = 13 # G27
BTN_B = 15 # G22
LED_G = 29 # G5
LED_R = 31 # G6
LED_Y = 32 # G12
LED_B = 33 # G13
GPIO.setup([BTN_G, BTN_R, BTN_Y, BTN_B], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup([LED_G, LED_R, LED_Y, LED_B], GPIO.OUT, initial=GPIO.LOW)
x = GPIO.HIGH
blink = False
blink_delay = 0.1
while True:
[g, r, y, b] = [GPIO.input(BTN_G), GPIO.input(BTN_R), GPIO.input(BTN_Y), GPIO.input(BTN_B)]
#print [g,r,y,b]
# light corresponding LED when pushbutton of same color is pressed
GPIO.output(LED_G, not g)
GPIO.output(LED_R, not r)
GPIO.output(LED_Y, not y)
GPIO.output(LED_B, not b)
# when green and red pressed, enter blink mode
if g and r:
blink = True
elif g or r:
blink = False
blink_delay = 0.1
if blink:
if y: blink_delay += 0.1
if b: blink_delay -= 0.1
if blink_delay < 0: blink_delay = 0
GPIO.output([LED_G, LED_R, LED_Y, LED_B], x)
if x == GPIO.LOW:
x = GPIO.HIGH
else:
x = GPIO.LOW
time.sleep(blink_delay)
time.sleep(0.01)
# TODO: interrupts vs polling?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.