Skip to content

Instantly share code, notes, and snippets.

@harrislapiroff
Last active August 29, 2015 13:57
Show Gist options
  • Save harrislapiroff/9539236 to your computer and use it in GitHub Desktop.
Save harrislapiroff/9539236 to your computer and use it in GitHub Desktop.
RPi Chasing Lights
#!/usr/bin/env python
import signal
import sys
import time
from RPi import GPIO
LIGHT_ORDER = (7, 12, 16, 18, 22, 11,) # The number of the GPIO pins in the order lights are wired to them
DELAY = .05
def exit(signal, frame):
for light in LIGHT_ORDER:
GPIO.output(light, GPIO.LOW)
GPIO.cleanup()
print "\n"
sys.exit(0)
def main():
# Set Up the GPIO board
GPIO.setmode(GPIO.BOARD)
for light in LIGHT_ORDER:
GPIO.setup(light, GPIO.OUT)
# Set Up to catch interrupt and turn out lights
signal.signal(signal.SIGINT, exit)
# Chase
i = -1
num_lights = len(LIGHT_ORDER)
while True:
prev = i
i = (i + 1) % num_lights
print "On: %d, Off: %d" % (i, prev)
GPIO.output(LIGHT_ORDER[prev], GPIO.LOW)
GPIO.output(LIGHT_ORDER[i], GPIO.HIGH)
time.sleep(DELAY)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment