from machine import Pin, I2C
import time
from pico_i2c_lcd import I2cLcd
# Configuración de pines
SW = Pin(15, Pin.IN, Pin.PULL_UP)
DT = Pin(14, Pin.IN)
CLK = Pin(13, Pin.IN)
pinEnabled = Pin(5, Pin.OUT, value=0)
pinStep = Pin(4, Pin.OUT)
pinDirection = Pin(0, Pin.OUT)
# Configuración del LCD
i2c = I2C(id=1, scl=Pin(27), sda=Pin(26), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Variables de estado
valor_anterior = CLK.value()
SW_presionado = False
stepsPerRevolution = 200 # 1.8 grados / paso
while True:
# Detectar cambios en el pin CLK
if valor_anterior != CLK.value():
valor_anterior = CLK.value()
if CLK.value() == False:
if DT.value() == False: # Contra reloj
pinDirection.off()
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr('Contra Reloj')
for i in range(stepsPerRevolution):
pinStep.on()
time.sleep_ms(10)
pinStep.off()
time.sleep_ms(10)
# Verificar si el botón SW fue presionado
if SW.value() == False and not SW_presionado:
SW_presionado = True
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr('Stop')
break
else: # Sentido horario
pinDirection.on()
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr('Sentido Horario')
for i in range(stepsPerRevolution):
pinStep.on()
time.sleep_ms(10)
pinStep.off()
time.sleep_ms(10)
if SW.value() == False and not SW_presionado:
SW_presionado = True
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr('Stop')
break
# Restablecer estado del botón
if SW.value() == True:
SW_presionado = False