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 28 29 30 31 32 33 34 35 36 37 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== 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) |

