from machine import Pin, ADC, PWM
import time
# Lee el movimiento horizontal
x = ADC(Pin(26)) conectado al pin GP26
button = Pin(15, Pin.IN, Pin.PULL_UP)
# pin GP14
servo = PWM(Pin(14))
# Los servos trabajan
servo.freq(50)
S --------
def map_value(val, in_min, in_max, out_min, out_max):
return int((val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
while True:
# Leer la posición horizontal
x_val = x.read_u16()
# Convertir la lectura del joystick
# en un ángulo para el servo
angle = map_value(x_val, 0, 65535, 0, 180)
# Convertir el ángulo en una señal PWM
#
# 2000 = 0°
# 8000 = 180°
duty = int(map_value(angle, 0, 180, 2000, 8000))
# Mover el servo a la posición calculada
servo.duty_u16(duty)
# Mostrar información en la consola
print("X:", x_val, "Ángulo servo:", angle, "Duty:", duty)
# Revisar si el botón está presionado
if button.value() == 0:
print("Botón presionado")
# Esperar un momento antes de repetir
time.sleep(0.1)