from machine import Pin
import time
# Pin assignments
car_red = 12
car_yellow = 13
car_green = 11
button = 10
ped_red = 8
ped_green = 7
cross_time = 5000
# Time since button pressed
change_time = 0
# Initialize the pins
car_red_pin = Pin(car_red, Pin.OUT)
car_yellow_pin = Pin(car_yellow, Pin.OUT)
car_green_pin = Pin(car_green, Pin.OUT)
ped_red_pin = Pin(ped_red, Pin.OUT)
ped_green_pin = Pin(ped_green, Pin.OUT)
button_pin = Pin(button, Pin.IN)
# Turn on the green lights
car_green_pin.on()
ped_red_pin.on()
def change_lights():
car_green_pin.off() # Green off
car_yellow_pin.on() # Yellow on
time.sleep(4) # Wait 2 seconds
car_yellow_pin.off() # Yellow off
car_red_pin.on() # Red on
time.sleep(2) # Wait 1 second
ped_red_pin.off() # Pedestrian red off
ped_green_pin.on() # Pedestrian green on
time.sleep(cross_time / 1000) # Wait for preset time period
# Flash the pedestrian green
for _ in range(10):
ped_green_pin.on()
time.sleep(0.25)
ped_green_pin.off()
time.sleep(0.25)
ped_red_pin.on() # Turn pedestrian red on
time.sleep(0.5)
car_red_pin.off() # Red off
car_yellow_pin.on() # Yellow on
time.sleep(1)
car_yellow_pin.off() # Yellow off
car_green_pin.on()
# Record the time since last change of lights
global change_time
change_time = time.ticks_ms()
# Main loop
while True:
state = button_pin.value()
# Check if button is pressed and it is over 5 seconds since last button press
if state == 1 and (time.ticks_ms() - change_time) > 5000:
change_lights()