SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By caleb12134
#168597
Hey, I am very confused on how to use the joystick. I read the getting started manual and uploaded the example code. I wanted to control a servo and make it turn left and then I move the joystick right it moves right. I don't understand how the joystick works and how I would do that. How does the arduino get info on the joystick and use it. Can someone give me an example code please. (I am not being lazy I just want to understand how to use it so I can learn and use it with other things :) )
By Mee_n_Mac
#168649
If you're using this shield and joystick;
https://www.sparkfun.com/products/9824
https://www.sparkfun.com/products/9032
... and read this tutorial ;
https://www.sparkfun.com/tutorials/272
... then you know the joystick has 2 independant potentiometers (pots) and that the shield brings the vertical pot output voltage to the Uno's A0 pin. The horizontal pot goes to A1. As you move the stick, the voltage divider of the pot(s) change, giving a voltage somewhere btw 0 and Vcc (= 5v for an UNO).
https://learn.sparkfun.com/tutorials/voltage-dividers
The A/D converter (ADC) reads that voltage and gives a number btw 0 and 1023. If you want to command a servo to move under joystick control then you need to turn 0-1023 in the 0-180 degrees the servo code can use. You might look at this.
http://arduino.cc/en/Reference/Map#.UwVdB4WmWVI
and this ;
http://arduino.cc/en/Tutorial/Knob#.UwVdq4WmWVI
as well as the sample code.

This has a good depiction of the mechanics of an analog joystick.
http://electronics.howstuffworks.com/joystick3.htm