from machine import Pin, ADC, PWM
import time
# Joystick eje X
x = ADC(Pin(26)) # VRx conectado a GP26 (ADC0)
button = Pin(15, Pin.IN, Pin.PULL_UP) # Botón con resistencia pull-up
# Servo
servo = PWM(Pin(14))
servo.freq(50)
# Función para mapear valores
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:
# Lectura del eje X
x_val = x.read_u16()
# Mapear joystick (0–65535) a ángulo (0–180)
angle = map_value(x_val, 0, 65535, 0, 180)
# Convertir ángulo a pulso PWM
# Para la Pico: duty_u16 entre ~2000 (0°) y ~8000 (180°)
duty = int(map_value(angle, 0, 180, 2000, 8000))
servo.duty_u16(duty)
# Mostrar valores en consola
print("X:", x_val, "Ángulo servo:", angle, "Duty:", duty)
# Detectar botón
if button.value() == 0:
print("Botón presionado")
time.sleep(0.1)