from machine import Pin
import time
# Configuración de pines
Led_verde = Pin(13, Pin.OUT)
Led_amarillo = Pin(9, Pin.OUT)
Led_rojo = Pin(5, Pin.OUT)
button_on = Pin(0, Pin.IN, Pin.PULL_UP) # Botón para encender
button_off = Pin(1, Pin.IN, Pin.PULL_UP) # Botón para apagar
# Variable para controlar el estado del semáforo
running = False
# Variable para registrar el estado anterior de los botones
last_button_on_state = 1
last_button_off_state = 1
def check_buttons():
global running, last_button_on_state, last_button_off_state
current_button_on_state = button_on.value()
current_button_off_state = button_off.value()
if current_button_on_state == 0 and last_button_on_state == 1:
running = True # Encender semáforo
elif current_button_off_state == 0 and last_button_off_state == 1:
running = False # Apagar semáforo
time.sleep(0.2) # Antirrebote
last_button_on_state = current_button_on_state
last_button_off_state = current_button_off_state
while True:
check_buttons() # Revisa el estado de los botones en cada ciclo
if running: # Secuencia del semáforo
Led_verde.on()
for i in range(5):
check_buttons()
if not running:
break
time.sleep(0.1)
Led_verde.off()
Led_amarillo.on()
for i in range(5):
check_buttons()
if not running:
break
time.sleep(0.1)
Led_amarillo.off()
Led_rojo.on()
for i in range(5):
check_buttons()
if not running:
break
time.sleep(0.1)
Led_rojo.off()
else: # Apagar todos los LEDs
Led_rojo.off()
Led_amarillo.off()
Led_verde.off()