Skip to content

Instantly share code, notes, and snippets.

@seantibor
Created June 1, 2019 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seantibor/374289381b08fcb426f39e7df6782b90 to your computer and use it in GitHub Desktop.
Save seantibor/374289381b08fcb426f39e7df6782b90 to your computer and use it in GitHub Desktop.
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