from machine import Pin
from time import sleep_ms
# ==========================================
# FUNCIONES DE BOTONES
# ==========================================
def inicio(x):
global motor_on
motor_on = True
def paro(x):
global motor_on, interrumpir_ciclo
interrumpir_ciclo = False
motor_on = False
IN1.value(0)
IN2.value(0)
IN3.value(0)
IN4.value(0)
def reversible(x):
global direccion, interrumpir_ciclo
if direccion == 0:
direccion = 1
else:
direccion = 0
led_rev.value(direccion)
interrumpir_ciclo = True
def cambiar_secuencia(x):
global secuencias
secuencia = (secuencia +1)%3
actualizar_leds()
# ==========================================
# CONTROL MOTOR
# ==========================================
def mover(y):
IN1.value(int(y[0]))
IN2.value(int(y[1]))
IN3.value(int(y[2]))
IN4.value(int(y[3]))
# ==========================================
# LEDS DE SECUENCIA
# ==========================================
def actualizar_leds():
led_normal.value(0)
led_medio.value(0)
# PASO NORMAL
if secuencia == 0:
led_normal.value(1)
# PASO COMPLETO
elif secuencia == 1:
led_normal.value(1)
led_medio.value(1)
# MEDIO PASO
elif secuencia == 2:
led_medio.value(1)
# ==========================================
# MOTOR
# ==========================================
IN1 = Pin(19, Pin.OUT)
IN2 = Pin(21, Pin.OUT)
IN3 = Pin(22, Pin.OUT)
IN4 = Pin(23, Pin.OUT)
# ==========================================
# BOTONES
# ==========================================
btn_inicio = Pin(12, Pin.IN, Pin.PULL_UP)
btn_paro = Pin(13, Pin.IN, Pin.PULL_UP)
btn_rev = Pin(14, Pin.IN, Pin.PULL_UP)
btn_sec = Pin(27, Pin.IN, Pin.PULL_UP)
btn_inicio.irq(trigger=Pin.IRQ_FALLING, handler=inicio)
btn_paro.irq(trigger=Pin.IRQ_FALLING, handler=paro)
btn_rev.irq(trigger=Pin.IRQ_FALLING, handler=reversible)
btn_sec.irq(trigger=Pin.IRQ_FALLING, handler=cambiar_secuencia)
# ==========================================
# LEDS
# ==========================================
# Estado general
led_inicio = Pin(15, Pin.OUT) # Inicio
led_paro = Pin(2, Pin.OUT) # Paro
led_rev = Pin(4, Pin.OUT) # Reversible
# Secuencias
led_normal = Pin(5, Pin.OUT) # Paso normal
led_medio = Pin(18, Pin.OUT) # Medio paso
# ==========================================
# SECUENCIAS DEL MOTOR
# ==========================================
# NORMAL (Wave Drive)
normal = [
"1000",
"0100",
"0010",
"0001"
]
# PASO COMPLETO
paso_completo = [
"1100",
"0110",
"0011",
"1001"
]
# MEDIO PASO
medio_paso = [
"1000",
"1100",
"0100",
"0110",
"0010",
"0011",
"0001",
"1001"
]
# ==========================================
# VARIABLES
# ==========================================
motor_on = False
direccion = 0
secuencia = 0
interrumpir_ciclo = False
led_rev.value(0)
actualizar_leds()
# ==========================================
# PROGRAMA PRINCIPAL
# ==========================================
while True:
if motor_on:
led_inicio.value(1)
led_paro.value(0)
actualizar_leds()
# Selección de secuencia
if secuencia == 0:
tabla = normal
elif secuencia == 1:
tabla = paso_completo
else:
tabla = medio_paso
# Dirección
pasos = tabla if direccion == 0 else tabla[::-1]
# Movimiento
for patron in pasos:
if not motor_on or interrumpir_ciclo:
interrumpir_ciclo = False # Resetear bandera
break
mover(patron)
sleep_ms(15)
# Pequeña pausa entre ciclos
sleep_ms(1)
else:
led_inicio.value(0)
led_paro.value(1)
led_normal.value(0)
led_medio.value(0)
IN1.value(0)
IN2.value(0)
IN3.value(0)
IN4.value(0)
sleep_ms(50)