from machine import Pin import time import neopixel LED_PIN = 8 NUM_LEDS = 12 np = neopixel.NeoPixel(Pin(LED_PIN), NUM_LEDS) # --- Helper functions ------------------------------------------------- def clear(): """Turn off all LEDs.""" np.fill((0, 0, 0)) np.write() def set_pixels(indices, color): """Set multiple LEDs to the same colour.""" for i in indices: np[i] = color np.write() # --- Traffic light colours -------------------------------------------- RED = (255, 0, 0) AMBER = (100, 25, 0) GREEN = (0, 255, 0) # --- Main loop --------------------------------------------------------- while True: # Red clear() set_pixels([0, 1], RED) time.sleep(3) # Red + Amber set_pixels([4, 5], AMBER) time.sleep(2) # Green clear() set_pixels([8, 9], GREEN) time.sleep(3) # Amber clear() set_pixels([4, 5], AMBER) time.sleep(1)