from machine import Pin, Timer
#Definimos variables accesibles globalmente
LedVerde = Pin(27, Pin.OUT)
LedAmarillo = Pin(25, Pin.OUT)
LedRojo = Pin(33, Pin.OUT)
Contador = 0
Estado = 0
Switch = Pin(2,Pin.IN,Pin.PULL_UP)
#-----Definimos Funciones---#
def Estado0():
LedRojo.value(1)
LedAmarillo.value(0)
LedVerde.value(0)
print("Estado = "+str(Estado))
def Estado1():
LedRojo.value(1)
LedAmarillo.value(1)
LedVerde.value(0)
print("Estado = "+str(Estado))
def Estado2():
LedRojo.value(0)
LedAmarillo.value(0)
LedVerde.value(1)
print("Estado = "+str(Estado))
def Estado3():
LedRojo.value(0)
LedAmarillo.value(1)
LedVerde.value(0)
print("Estado = "+str(Estado))
def Estado4():
LedRojo.value(0)
LedAmarillo.value(0)
LedVerde.value(0)
def FSM(t):
global Contador
global Estado
Contador = Contador + 1
if Estado<4 and Switch.value(): #switch.value() = tener un 1 en el switch xd
Estado = 4
Estado4()
elif Estado == 4 and Switch.value():
Estado = 5
Estado3()
elif Estado == 5:
Estado = 4
Estado4()
elif Estado == 4 and not Switch.value():
Estado = 0
Contador = 0
Estado0()
elif Estado == 0 and Contador == 8:
Estado = 1
Contador = 0
Estado1()
elif Estado == 1 and Contador == 2:
Estado = 2
Contador = 0
Estado2()
elif Estado == 2 and Contador == 7:
Estado = 3
Contador = 0
Estado3()
elif Estado == 3 and Contador == 3:
Estado = 0
Contador = 0
Estado0()
Estado0()
elTimer = Timer(1)
elTimer.init(mode=Timer.PERIODIC, period=1000,callback = FSM)