Simple Code – Traffic Light

Assume that Red light for 20 seconds, Yellow light for 5 seconds and Green light for 30 seconds.

Code Sample

from nanoleafapi import Nanoleaf, NanoleafDigitalTwin, RED, ORANGE, YELLOW, GREEN, LIGHT_BLUE, BLUE, PINK, PURPLE, WHITE
nl = Nanoleaf("10.10.3.230")
import time
digital_twin = NanoleafDigitalTwin(nl)
BLACK = (0,0,0)
red_light_id = 28716
yellow_light_id = 20150
green_light_id = 47886

digital_twin.set_all_colors(BLACK)
while True:
    print("Red Light for 20 seconds")
    digital_twin.set_color(red_light_id, RED)
    digital_twin.sync()
    time.sleep(15)
    print("Yellow Light for 5 seconds")
    digital_twin.set_color(yellow_light_id, YELLOW)
    digital_twin.sync()
    time.sleep(5)
    print("Red & Yellow Lights off")
    digital_twin.set_color(red_light_id, BLACK)
    digital_twin.set_color(yellow_light_id, BLACK)
    digital_twin.sync()
    print("Green Light for 30 seconds") 
    digital_twin.set_color(green_light_id, GREEN)
    digital_twin.sync()
    time.sleep(30)
    print("Green Light off") 
    digital_twin.set_color(green_light_id, BLACK)
    digital_twin.sync()
    print("Yellow Light for 5 seconds")
    digital_twin.set_color(yellow_light_id, YELLOW)
    digital_twin.sync()
    time.sleep(5)
    nl.set_color(BLACK)
    digital_twin.set_color(yellow_light_id, BLACK)
    digital_twin.sync()
    print("Yellow Light off")
    digital_twin.set_color(red_light_id, RED)
    digital_twin.sync()
    

Leave a Reply

Your email address will not be published. Required fields are marked *