print(">>> main.py ejecutándose correctamente")
from machine import Pin
import time
import main
class TrafficLight:
"""
Clase que encapsula el comportamiento del semáforo.
Recibe los números de pin para rojo, amarillo y verde.
"""
def __init__(self, red_pin: int, yellow_pin: int, green_pin: int):
# Pines configurados como salida
self.Red = Pin(red_pin, Pin.OUT)
self.Yellow = Pin(yellow_pin, Pin.OUT)
self.Green = Pin(green_pin, Pin.OUT)
# Iniciar con todas las luces apagadas
self.AllOff()
def AllOff(self) -> None:
"""Apaga todas las luces del semáforo."""
self.Red.off()
self.Yellow.off()
self.Green.off()
def RedOn(self) -> None:
"""Enciende la luz roja (y apaga las demás)."""
self.Red.on()
self.Yellow.off()
self.Green.off()
def YellowOn(self) -> None:
"""Enciende la luz amarilla (y apaga las demás)."""
self.Red.off()
self.Yellow.on()
self.Green.off()
def GreenOn(self) -> None:
"""Enciende la luz verde (y apaga las demás)."""
self.Red.off()
self.Yellow.off()
self.Green.on()
def RunTrafficCycle(light: TrafficLight,
green_time: float = 10.0,
yellow_time: float = 3.0,
red_time: float = 10.0) -> None:
"""
Ejecuta un ciclo completo del semáforo:
- Verde durante green_time segundos
- Amarillo durante yellow_time segundos
- Rojo durante red_time segundos
"""
# Verde
light.GreenOn()
time.sleep(green_time)
# Amarillo (transición)
light.YellowOn()
time.sleep(yellow_time)
# Rojo
light.RedOn()
time.sleep(red_time)
def MainLoop():
"""
Punto de entrada principal. Configura pines y ejecuta el semáforo en bucle.
Cambia los números de pin si usas otros pines en Wokwi o en hardware real.
"""
# --- CONFIGURACIÓN DE PINES ---
RED_PIN = 15
YELLOW_PIN = 2
GREEN_PIN = 4
# Crear instancia del semáforo
semaforo = TrafficLight(RED_PIN, YELLOW_PIN, GREEN_PIN)
# Tiempos (en segundos) - ajustables según necesidad
GREEN_TIME = 10.0
YELLOW_TIME = 3.0
RED_TIME = 10.0
print("Semáforo iniciado. Ctrl+C para detener.")
try:
# Bucle principal: repite ciclos indefinidamente
while True:
RunTrafficCycle(semaforo, GREEN_TIME, YELLOW_TIME, RED_TIME)
except KeyboardInterrupt:
# Si se detiene con Ctrl+C, apagar LEDs y salir
print("Deteniendo semáforo. Apagando luces...")
semaforo.AllOff()
# Ejecutar si se llama directamente
if __name__ == "__main__":
MainLoop()