The Parallax 2-Axis Joystick can be used for a wide variety of projects like wheeled robots, video games, or anything requiring user input. Like most joysticks, the Parallax 2-Axis Joystick uses two variable resistors (called potentiometers) to read the user’s input. Remember reading a potentiometer’s value back in Measure Volts [1]? Reading a joystick’s position is a matter of measuring two potentiometer voltages. One reports the left/right position and the other reports the up/down position.
The Parallax joystick contains two 10 kΩ potentiometers, one for each axis. We’ll read the value from the potentiometer using an analog to digital converter, or ADC, just like in Measure Volts. For our joystick, we’ll use one of the ADC’s inputs for each axis. In this case, up/down (U/D) is connected to channel 2 and left/right (L/R) is connected to channel 3. The potentiometer also needs to be powered, so we’ll attach the potentiometer’s pins labeled L/R+ and U/D+ to 5 V, and the GND pin to ground. Note that there are two of some of these pins—it doesn’t matter which one you use, they are electrically the same.
(1) Parallax 2-Axis Joystick (#27800) [2]
Note: This lesson is designed for the Propeller Activity Board (original or WX version). It is not compatible with the Propeller Board of Education, which uses a different A/D converter.
This test code will read both axes of the joystick and display voltage values for each. Since the potentiometer is connected to 5 V, the middle of the joystick (where it goes when you release it) will display 2.5 V on both axes. You may notice slight deviations from 2.5 V; this is explained more in the Did You Know? section.
The code includes the adcDCpropAB library with the command #include "adcDCpropAB.h". This library is designed for measuring voltages using the A/D converter on the Propeller Activity Board (original or WX version). The function adc_init gets called first, to tell it which Propeller I/O pins are connected to the A/D converter's /CS, SCL, DO and DI pins. Together these pins are called an SPI bus, over which the Propeller communicates with many peripheral devices. You will find the SPI pin numbers next to the A/D converter chip, which is right below the GND sockets on your board.
After that function, float x, y declares two floating point variables for storing measurements. Inside the while(1) loop, udV = adc_volts(2) stores the voltage at A/D2 into the udV variable, and lrV = adc_volts(3) does the same for A/D3. A couple of print calls display the two voltage values, followed by a 1/10 second pause before the loop repeats.
/* Joystick.c Simple demo with the Parallax 2-axis Joystick. */ #include "adcDCpropab.h" // Include adcDCpropAB #include "simpletools.h" // Include simpletools int main() // Main function { adc_init(21, 20, 19, 18); // CS=21, SCL=20, DO=19, DI=18 float lrV, udV; // Voltage variables while(1) // Loop repeats indefinitely { udV = adc_volts(2); // Check A/D 2 lrV = adc_volts(3); // Check A/D 3 putChar(HOME); // Cursor -> top-left "home" print("Up/Down = %.2f V %c\n", udV, CLREOL); // Display voltage print("Left/Right = %.2f V %c\n", lrV, CLREOL); // Display voltage pause(100); // Wait 1/10 s } }
Instead of displaying the voltage values from the joystick, let’s display a range representing the joystick's position. To do this, we’ll need to scale voltages going from 0 to 5 V into values from -100 to 100.
You may want to drive a servo with your joystick. Perhaps you want your servo to start at 90 degrees and move ±60 degrees from there. Write some code to do this scaling, and output the result for each axis on the terminal.
Hint: to display the “%” character in the serial terminal, we need to type two of them in the print command. This is because % acts as a special operator in print commands so it is necessary to have two % characters tell print to display a literal “%.”
Links
[1] https://learn.parallax.com/propeller-c-simple-circuits/measure-volts
[2] http://www.parallax.com/product/27800
[3] https://learn.parallax.com/propeller-c-set-simpleide
[4] https://learn.parallax.com/propeller-c-start-simple