import machine
import time
# Set up the GPIO pins
leds = [machine.Pin(i, machine.Pin.OUT, machine.Pin.PULL_DOWN) for i in range(14, 17)] # Set up LEDs
led_states = [False] * 3 # Set initial state of LEDs
car_status = ["STOP","WAIT","GO"]
button = machine.Pin(18, machine.Pin.IN, machine.Pin.PULL_DOWN) # Set up pushbuttons
# Define functions to turn on and off LEDs
def turn_on_led(led_num):
if led_num < 1 or led_num > 4:
return
leds[led_num - 1].value(1)
print (car_status[led_num-1])
led_states[led_num - 1] = True
def turn_off_led(led_num):
if led_num < 1 or led_num > 4:
return
leds[led_num - 1].value(0)
led_states[led_num - 1] = False
# Define function to start and end traffic jam
def start_traffic_jam():
for led in leds:
led.value(0) # Turn off all LEDs
for i in range(4): # Turn on LEDs one by one, simulating traffic jam
turn_on_led(i)
turn_off_led(i-1)
time.sleep(2)
def end_traffic_jam():
for led in leds:
led.value(0) # Turn off all LEDs
led.value(1) # Turn on green LED
# Main loop
while True:
# Check if any of the push buttons are pressed
end_traffic_jam()
if button.value():
print("Traffic jam started")
start_traffic_jam()
end_traffic_jam()
print("Traffic jam ended")