Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A traffic light using Adafruit Itertools
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