Created
July 11, 2016 05:25
-
-
Save rxseger/c6b7e82cdda67b1fc588f5f620aba53f to your computer and use it in GitHub Desktop.
Raspberry Pi 3 GPIO testing pushbutton input and LED output
This file contains hidden or 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/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