from machine import Pin
import time
# Configuracion de Pines GPIO
pinRojo = Pin(23, Pin.OUT)
pinAmarillo = Pin(22, Pin.OUT)
pinVerde = Pin(21, Pin.OUT)
# Constatantes de Tiempo, segundos
tiempoRojo = 3
tiempoVerde = 3
tiempoAmarillo = 2
# Funciones de Control
def iniciarLuces():
pinRojo.value(0)
pinAmarillo.value(0)
pinVerde.value(0)
def cicloRojo():
pinRojo.value(1)
pinAmarillo.value(0)
pinVerde.value(0)
time.sleep(tiempoRojo)
def cicloVerde():
pinRojo.value(0)
pinAmarillo.value(0)
pinVerde.value(1)
time.sleep(tiempoVerde)
def cicloAmarillo():
pinRojo.value(0)
pinAmarillo.value(1)
pinVerde.value(0)
time.sleep(tiempoAmarillo)
def mainSemaforo():
iniciarLuces()
while True:
cicloRojo()
cicloVerde()
cicloAmarillo()
# Ejecución
try:
mainSemaforo()
except KeyboardInterrupt:
# Asegura apagar las luces si la simulación se detiene.
iniciarLuces()