The joystick works by using two potentiometers, one for each axis. A potentiometer is an electrical device that varies the voltage level of the output signal. This output is an analog signal and it ranges from 0 to 5 volts in my setup. You can see the potentiometers in the image below. They are the green and white squares under the joystick.
I used an Arduino Uno which has a microprocessor and an analog to digital converter to interpret the two analog signals output from the joystick. The analog to digital converter takes the analog signal and converts it to a ten bit number (between 0 and 1023). The number is then processed by the microprocessor on the Arduino. Using the map function and servo library (described below), the microprocessor outputs a signal that moves the servos accordingly.
Functions and Libraries Called
To help with processing the code, I used the map function and called the servo library. The servo library works by taking an input number and transforming it to a signal that a servo can understand. This number needs to be between 0 and 180, which refer to the angle that we want the servo to be pointing. If you remember from above, the analog to digital converter gave me a number that was between 0 and 1023. Inputting a number too large can break the servo, so I needed to convert it somehow. That is where the map function came in. It linearly converts one range of numbers to another range of numbers. So if you thought about your position between 0 and 1023 as a percentage, the map function would give you the same position percentage between 0 and 180. Since the board could only turn about 60 degrees on each axis, I used a range of about 70 to 130 instead of the full 180 degree servo rotation. If you look at the code you will notice there was some fine tuning involved for each axis.