Created
June 1, 2019 15:44
-
-
Save seantibor/374289381b08fcb426f39e7df6782b90 to your computer and use it in GitHub Desktop.
A traffic light using Adafruit Itertools
This file contains 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 neopixel | |
import time | |
import board | |
from adafruit_itertools.adafruit_itertools import cycle | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False) | |
RED = (255, 0, 0) | |
YELLOW = (255, 255, 0) | |
GREEN = (0, 255, 0) | |
red_duration = 6 | |
yellow_duration = 3 | |
green_duration = 6 | |
red_lights = [3, 4, 5, 6] | |
yellow_lights = [2, 7] | |
green_lights = [8, 9, 0, 1] | |
traffic_light = cycle( | |
[ | |
(GREEN, green_duration, green_lights), | |
(YELLOW, yellow_duration, yellow_lights), | |
(RED, red_duration, red_lights), | |
] | |
) | |
while True: | |
pixels.fill(0) | |
time.sleep(0.2) | |
light, duration, lights = next(traffic_light) | |
for light_num in lights: | |
pixels[light_num] = light | |
pixels.show() | |
time.sleep(duration) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment