In this Video Lesson we show how to calibrate a joystick to report Angular Position. We do this using MicroPython on a Raspberry Pi Pico W. For your convenience, the code is included below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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) deg=math.atan2(vCal,hCal)*360/2/math.pi if hCal==0: hCal=1 if deg<0: deg=deg+360 mag=math.sqrt(hCal**2+vCal**2) if mag<=4: hCal=0 vCal=0 print(hCal,vCal,deg) time.sleep_ms(200) |