PS2 style 2 axis directional joystick, which is compatible with the Arduino interface. Product code for this item is HCARDU0019 and it can be purchased here.
1..............GND
2..............+5V
3..............X-Axis (analogue)
4..............Y-Axis (analogue)
5..............Push button (Connects to GND)
Code: Select all
#define JOYS_VRX_DIO A0 /* Selects the input pin for the joysticks X-Axis */
#define JOYS_VRY_DIO A1 /* Selects the input pin for the joysticks Y-Axis */
#define JOYS_SW_DIO 2 /* Selects the input pin for the joysticks push button */
/* Initialises serial and DIO */
void setup()
{
/* Sets up the serial port for displaying the status of the sensor */
Serial.begin(9600);
/* Configures the DIO pin that the joysticks push button will be connected
to. As it has no pull-up we will need to enable the Arduino's internal pull-up */
pinMode(JOYS_SW_DIO, INPUT);
digitalWrite(JOYS_SW_DIO, HIGH); // turns on pull-up resistors
}
/* Main program loop */
void loop()
{
/* Reads the current position of the joysticks X & Y axis via the analogue pins */
Serial.print("X axis: ");
Serial.print(analogRead(JOYS_VRX_DIO));
Serial.print(" Y axis: ");
Serial.print(analogRead(JOYS_VRY_DIO));
/* Reads the state of the push button and if pressed, outputs the state to the
serial port */
if (!digitalRead(JOYS_SW_DIO))
{
Serial.println(" Button pressed !");
}else
{
Serial.println();
}
}