How do you query one or two joysticks? The answer is immediate using 2 QC joysticks on a breakout with 5 PINs.
Joystick control sensor capable of detecting x-axis and y-axis tilt.
Pin Connections:
- Joystick Pin GND to Microcontroller Pin GND (Ground)
- Joystick Pin +5V to Microcontroller Pin +5V / works also with 3.3V
- Joystick Pin VRx to Microcontroller Pin Analog 0
- Joystick Pin VRy to Microcontroller Pin Analog 1
- Joystick Pin SW to Microcontroller Pin Digital 4
// Joystick 1:
const int VRx1 = 0; // Connect to Analog Pin 0
const int VRy1 = 1; // Connect to Analog Pin 1
const int SW1 = 4; // Connect to Digital Pin 4
// Joystick 2:
const int VRx2 = 2; // Connect to Analog Pin 0
const int VRy2 = 3; // Connect to Analog Pin 1
const int SW2 = 5; // Connect to Digital Pin 4
void setup() {
// Joystick 1:
pinMode(SW1, INPUT);
digitalWrite(SW1, HIGH);
// Joystick 2:
pinMode(SW2, INPUT);
digitalWrite(SW2, HIGH);
Serial.begin(9600);
}
void loop() {
// Joystick 1:
Serial.print("x-axis tilt: ");
Serial.println(analogRead(VRx1));
Serial.print("y-axis tilt: ");
Serial.println(analogRead(VRy1));
delay(800);
// Joystick 2:
Serial.print("x-axis tilt: ");
Serial.println(analogRead(VRx2));
Serial.print("y-axis tilt: ");
Serial.println(analogRead(VRy2));
delay(800);
}
The bandwidth of the X and Y axes is reflected with the values between 0 and a maximum of 1023 - the rest position being around 500.
Comments