Created
February 19, 2016 03:57
-
-
Save testeddoughnut/b6bb2968940fe4abb01e to your computer and use it in GitHub Desktop.
blink the lights on the pi
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
import asyncio | |
import RPi.GPIO as GPIO | |
from time import sleep | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(5,GPIO.OUT) | |
GPIO.setup(6,GPIO.OUT) | |
GPIO.setup(17,GPIO.OUT) | |
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
def blink_the_lights(): | |
for x in range(3): | |
for x in [5,6,17]: | |
GPIO.output(x, GPIO.HIGH) | |
sleep(.5) | |
for x in [5,6,17]: | |
GPIO.output(x, GPIO.LOW) | |
sleep(.5) | |
@asyncio.coroutine | |
def do_the_lights(): | |
while True: | |
for x in [5,6,17]: | |
GPIO.output(x, GPIO.HIGH) | |
sleep(1) | |
GPIO.output(x, GPIO.LOW) | |
yield from asyncio.sleep(1) | |
def main(): | |
loop = asyncio.get_event_loop() | |
def my_callback(channel): | |
print("Blinking the lights!") | |
loop.call_soon(blink_the_lights) | |
GPIO.add_event_detect(18, GPIO.FALLING, callback=my_callback, bouncetime=300) | |
try: | |
print("Running the lights... press ctrl+c to stop") | |
loop.create_task(do_the_lights()) | |
loop.run_forever() | |
except: | |
print("\nStopping the lights!") | |
finally: | |
GPIO.cleanup() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment