Hi everyone,
I have a lot of experience using PICMicro controllers, but I'm absolutely new with Arduino.
Now I'm trying to make a digital joystick for a friend, using an Arduino Leonardo.
Found a simple joystick program with 16 buttons that works great, but uses an analog XY axis.
Since I only need 8 buttons, it would be great to have 4 buttons for X and Y, just like you would have in an arcade joystick.
Here's the code:
#include <Joystick.h>
#define PINS 16
#define ENABLE_ANALOG1 true
int X1 = A2;
int Y1 = A3;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, PINS, 0,
true, true, false, false, false, false, false, false, false, false, false);
class CButton {
public:
int pin = NULL;
int lastState = 0;
CButton(int p) {
pin = p;
}
};
CButton Buttons[PINS] ={0,1,2,3,4,5,6,7,8,9,10,16,14,15,18,19};
void setup() {
for (int i = 0 ; i < PINS ; i++) {
pinMode(Buttons[i].pin, INPUT_PULLUP);
}
Joystick.begin();
if (ENABLE_ANALOG1) {
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
}
}
void JButtonStates() {
if (ENABLE_ANALOG1) {
Joystick.setXAxis(analogRead(X1) - 512);
Joystick.setYAxis(analogRead(Y1) - 512);
}
for (int i = 0; i < PINS; i++) {
int currentState = !digitalRead(Buttons[i].pin);
if (currentState != Buttons[i].lastState) {
Joystick.setButton(i, currentState);
Buttons[i].lastState = currentState;
}
}
}
void loop() {
JButtonStates();
delay(50);
}
I think my friend could simply remap a few buttons for UP/DOWN/LEFT/RIGHT, but it would be awesome if Windows would just detect it as an XY axis when you use the test program in Windows (see attached picture).
So my question:
Is this possible, and how do I do that ?