import machine
import utime
# Define GPIO pins
led_red = machine.Pin(13, machine.Pin.OUT)
led_yellow = machine.Pin(9, machine.Pin.OUT)
led_green = machine.Pin(5, machine.Pin.OUT)
button_red = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_UP)
button_yellow = machine.Pin(21, machine.Pin.IN, machine.Pin.PULL_UP)
button_green = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_UP)
# Initial durations (in seconds)
red_duration = 10
yellow_duration = 2
green_duration = 10
def update_duration(led, button):
"""Updates the duration of a specific LED."""
global red_duration, yellow_duration, green_duration
while button.value() == 0: # While button is pressed
# Get new duration from serial input (or other input method)
new_duration = input("Enter new duration for " + led + " light (in seconds): ")
try:
new_duration = int(new_duration)
if new_duration > 0:
if led == "red":
red_duration = new_duration
elif led == "yellow":
yellow_duration = new_duration
elif led == "green":
green_duration = new_duration
print(led + " light duration updated to", new_duration, "seconds.")
break # Exit loop after successful update
else:
print("Invalid input. Duration must be a positive number.")
except ValueError:
print("Invalid input. Please enter a number.")
# Main loop
while True:
# Red light
led_red.on()
update_duration("red", button_red)
utime.sleep(red_duration)
led_red.off()
# Yellow light
led_yellow.on()
update_duration("yellow", button_yellow)
utime.sleep(yellow_duration)
led_yellow.off()
# Green light
led_green.on()
update_duration("green", button_green)
utime.sleep(green_duration)
led_green.off()