from machine import Pin, PWM
from utime import sleep
# Definición de pines de los motores
motor_izqA = PWM(Pin(14), freq=490)
motor_derA = PWM(Pin(15), freq=490)
motor_izqB = Pin(12, Pin.OUT)
motor_derB = Pin(13, Pin.OUT)
# Definición de pines de los sensores
sensor_i = Pin(10, Pin.IN)
sensor_d = Pin(9, Pin.IN)
# Inicialización de los motores
def inicializar_motores():
motor_izqA.duty(0)
motor_derA.duty(0)
motor_izqB.value(0)
motor_derB.value(0)
# Funciones de control de los motores
def avanzar(tiempo):
motor_izqA.duty(500)
motor_derA.duty(500)
motor_izqB.value(1)
motor_derB.value(1)
sleep(tiempo)
detener(0)
def detener(tiempo):
motor_izqA.duty(0)
motor_derA.duty(0)
motor_izqB.value(0)
motor_derB.value(0)
sleep(tiempo)
def izquierda(tiempo):
motor_izqA.duty(0)
motor_derA.duty(500)
motor_izqB.value(0)
motor_derB.value(1)
sleep(tiempo)
detener(0)
def derecha(tiempo):
motor_izqA.duty(500)
motor_derA.duty(0)
motor_izqB.value(1)
motor_derB.value(0)
sleep(tiempo)
detener(0)
# Bucle principal de ejecución
while True:
inicializar_motores()
lec_i = sensor_i.value()
lec_d = sensor_d.value()
if lec_i == 0 and lec_d == 0:
avanzar(0.5)
elif lec_i == 1 and lec_d == 0:
izquierda(0.5)
elif lec_i == 0 and lec_d == 1:
derecha(0.5)
else:
detener(0.5)