In this lesson we show how to get intuitive calibrated results from a joystick, connected to a Raspberry Pi Pico W, using microPython. This lesson will make it easy to incorporate a joystick into Pico W projects.
For your convenience, the code we develop in the video above is included below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import machine import time import math hPin=27 vPin=26 hJoy= machine.ADC(hPin) vJoy= machine.ADC(vPin) while True: hVal=hJoy.read_u16() vVal=vJoy.read_u16() hCal=int(-.00306*hVal+100.766) vCal=int(.00306*vVal-100.766) mag=math.sqrt(hCal**2+vCal**2) if mag<=4: hCal=0 vCal=0 print(hCal,vCal) time.sleep_ms(200) |