from machine import Pin
import time
redA = Pin(23, Pin.OUT); yellowA = Pin(22, Pin.OUT); greenA = Pin(16, Pin.OUT)
redB = Pin(21, Pin.OUT); yellowB = Pin(5, Pin.OUT); greenB = Pin(4, Pin.OUT)
redC = Pin(27, Pin.OUT); yellowC = Pin(26, Pin.OUT); greenC = Pin(2, Pin.OUT)
redD = Pin(32, Pin.OUT); yellowD = Pin(33, Pin.OUT); greenD = Pin(13, Pin.OUT)
emergency = False
pedestrian = False
night = False
density = 2
while True:
# Emergency Vehicle Mode
if emergency:
for _ in range(5):
redA.on(); redB.on(); redC.on(); redD.on()
time.sleep(0.5)
redA.off(); redB.off(); redC.off(); redD.off()
time.sleep(0.5)
# Pedestrian Crossing Mode
elif pedestrian:
redA.on(); redB.on(); redC.on(); redD.on()
time.sleep(5)
redA.off(); redB.off(); redC.off(); redD.off()
# Night Mode
elif night:
for _ in range(5):
yellowA.on(); yellowB.on(); yellowC.on(); yellowD.on()
time.sleep(1)
yellowA.off(); yellowB.off(); yellowC.off(); yellowD.off()
time.sleep(1)
# Normal Traffic Cycle
else:
if density == 1:
green_time = 2
elif density == 2:
green_time = 4
else:
green_time = 6
# Side A Green
redA.off(); redB.on(); redC.on(); redD.on()
greenA.on()
time.sleep(green_time)
greenA.off(); yellowA.on()
time.sleep(2)
yellowA.off(); redA.on()
# Side B Green
redB.off(); redA.on(); redC.on(); redD.on()
greenB.on()
time.sleep(green_time)
greenB.off(); yellowB.on()
time.sleep(2)
yellowB.off(); redB.on()
# Side C Green
redC.off(); redA.on(); redB.on(); redD.on()
greenC.on()
time.sleep(green_time)
greenC.off(); yellowC.on()
time.sleep(2)
yellowC.off(); redC.on()
# Side D Green
redD.off(); redA.on(); redB.on(); redC.on()
greenD.on()
time.sleep(green_time)
greenD.off(); yellowD.on()
time.sleep(2)
yellowD.off(); redD.on()