# 20260722
#https://wokwi.com/projects/380345349190727681
from machine import Pin, PWM, ADC
from time import sleep, sleep_us
#import _thread
#m1_dir_sensor = Pin(0, Pin.IN, Pin.PULL_UP)
m1_auto = Pin(0, Pin.IN, Pin.PULL_UP) #manual/automatico motor1
#m2_auto = Pin(2, Pin.IN, Pin.PULL_UP) #manual/automatico motor2
lm1_lejos = Pin(18, Pin.IN, Pin.PULL_UP) #Limite de carrera 1 motor 1
lm1_cerca = Pin(19, Pin.IN, Pin.PULL_UP) #Limite de carrera 2 motor 1
#lm2_lejos = Pin(21, Pin.IN, Pin.PULL_UP) #Limite de carrera 1 motor 2
#lm2_cerca = Pin(22, Pin.IN, Pin.PULL_UP) #Limite de carrera 2 motor 2
boton_iz = Pin(2, Pin.IN, Pin.PULL_UP)
boton_de = Pin(3, Pin.IN, Pin.PULL_UP)
#boton_ar = Pin(5, Pin.IN, Pin.PULL_UP)
#boton_ab = Pin(6, Pin.IN, Pin.PULL_UP)
led_iz = Pin(6, Pin.OUT)
led_de = Pin(7, Pin.OUT)
#led_ar = Pin(9, Pin.OUT)
#led_ab = Pin(10, Pin.OUT)
m1_dir = Pin(14, Pin.OUT)
m1_pul = Pin(15, Pin.OUT)
#m2_dir = Pin(17, Pin.OUT)
#m2_pul = Pin(18, Pin.OUT)
sensor_m1 = ADC(0)
sensor_m2 = ADC(1)
pot_veloc = ADC(2)
# Parametros de funcionamiento
rpm = 100
spv = 200 #Pasos por vuelta del motor
paso_rosca = 4 #Paso rosca del tornillo en mm
microstep = 4 # 1 = Full step, 2 = Half Step
# 4 = 1/4, 8 = 1/8, 16 = 1/16, 32 = 1/32
stepsxmm = spv * microstep / paso_rosca #Calcula pasos/mm
def rpm_2_delay(rpm):
global delay
rev_por_seg = rpm / 60 #Convierte RPM a Revs/Segundo
steps_por_seg = spv * microstep * rev_por_seg #Calcula pasos/Segundo
delay = round((2 / steps_por_seg) * 1000000) #Calcula pausa en microseconds
return delay
def girar_m1():
m1_pul.high()
sleep_us(delay)
m1_pul.low()
sleep_us(delay)
def get_ADCh():
global sensor_m1, def_h
deform_h = sensor_m1.read_u16() #Lee valor de sensor de desplazamiento
def_h = round(deform_h / 6554, 1) #Determina el desplazamiento de 0 a 10
return(def_h)
while True:
rpm_2_delay(rpm)
## MODO MANUAL HORIZONTAL ##
while m1_auto.value() == True:
while (boton_iz.value() == 0 and boton_de.value() == 1 and lm1_lejos.value() == 0): #Pulsador izquierda
m1_dir.low()
led_iz.on()
girar_m1()
led_iz.off()
while (boton_iz.value() == 0 and boton_de.value() == 1 and lm1_lejos.value() == 1): #Pulsador izquierda + llega a lim izquierdo (lejos)
led_iz.toggle()
sleep(0.3)
while (lm1_lejos.value() == 1 and (not (boton_iz.value() == 1 and boton_de.value() == 0))): #Está en lim izq y no se pulsa a la derecha
led_iz.toggle()
sleep(0.3)
led_iz.off()
while (boton_iz.value() == 1 and boton_de.value() == 0 and lm1_cerca.value() == 0): #Pulsador derecha
m1_dir.high()
led_de.on()
girar_m1()
led_de.off()
while (boton_iz.value() == 1 and boton_de.value() == 0 and lm1_cerca.value() == 1): #Pulsador derecha + llega a lim derecha (cerca)
led_de.toggle()
sleep(0.3)
while (lm1_cerca.value() == 1 and (not (boton_iz.value() == 0 and boton_de.value() == 1))): #Está en lim izq y no se pulsa a la derecha
led_de.toggle()
sleep(0.3)
led_de.off()
while m1_auto.value() == False:
get_ADCh()
print(def_h)
while (def_h >=5 and lm1_lejos.value() == 0 and m1_auto.value() == False): #Aleja si la distancia es menor a la minima
m1_dir.low()
led_iz.on()
girar_m1()
get_ADCh()
led_iz.off()
while (lm1_lejos.value() == 1 and m1_auto.value() == False):
led_iz.toggle()
sleep(0.3)
while (def_h <3 and lm1_cerca.value() == 0 and m1_auto.value() == False): #Acerca si la distancia es mayor a la maxima
m1_dir.high()
led_de.on()
girar_m1()
get_ADCh()
led_de.off()
while (lm1_cerca.value() == 1 and m1_auto.value() == False):
led_de.toggle()
sleep(0.3)