OK.
I modified the code to work with the new version of Arduino IDE.
I have not done thorough test of analog sticks (yet), since my only PSP stick seems to be defect....Order on the way....
Code: Select all
//
// Using Library https://github.com/MHeironimus/ArduinoJoystickLibrary
//
#include <Joystick.h>
// If you have only one analog stick, set this to 1,
// If you have 2, set this to 2,
// If you do not have any at all, set this to 0.
// In theory, 4 are possible, but this script is limited to 2.
int AnalogSticks = 2;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_JOYSTICK,
14, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
true, true, false, // Rx, Ry, no Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
// Analog stick input pins, invert, and deadzone:
int deadzone = 4; // Values under 5 will return 0
int x1Pin = A0;
int y1Pin = A1;
int x2Pin = A2;
int y2Pin = A3;
int InvertX1 = 0;
int InvertY1 = 0;
int InvertX2 = 0;
int InvertY2 = 0;
// Button input pins. Please take care not to use a value twice!:
// If you do NOT use a button listed here, please assign it to a FREE, UNSUSED pin !!THIS IS IMPORTANT!!
int PinA = 1;
int PinB = 0;
int PinX = 2;
int PinY = 3;
int PinStart = 4;
int PinSelect = 5;
int PinDPadUp = 6;
int PinDPadDown = 7;
int PinDPadLeft = 8;
int PinDPadRight = 9;
int PinL1 = 10;
int PinL2 = 16;
int PinR1 = 14;
int PinR2 = 15;
// Button mapping (1-14). Please take care not to use a value twice!:
// If you do NOT use a button in the input map, set it to 0 here.
int A = 0;
int B = 1;
int X = 2;
int Y = 3;
int Start = 4;
int Select = 5;
int DPadUp = 6;
int DPadDown = 7;
int DPadLeft = 8;
int DPadRight = 9;
int L1 = 10;
int L2 = 11;
int R1 = 12;
int R2 = 13;
// Variable declarations for analog sticks
int x1Zero, y1Zero;
int x1Value, y1Value;
int x2Zero, y2Zero;
int x2Value, y2Value;
int x1Min, x1Max;
int y1Min, y1Max;
int x2Min, x2Max;
int y2Min, y2Max;
int x1F1, x1F2;
int y1F1, y1F2;
int x2F1, x2F2;
int y2F1, y2F2;
void tAnalogSticks(){
// Read analog sticks and respect the deadzone value
x1Value = analogRead(x1Pin) - x1Zero;
y1Value = analogRead(y1Pin) - y1Zero;
x2Value = analogRead(x2Pin) - x2Zero;
y2Value = analogRead(y2Pin) - y2Zero;
if(abs(x1Value) < deadzone) {
x1Value = 0;
}
if(abs(y1Value) < deadzone) {
y1Value = 0;
}
if(abs(x2Value) < deadzone) {
x2Value = 0;
}
if(abs(y2Value) < deadzone) {
y2Value = 0;
}
// Autocalibration of analog sticks (by Helder + wermy)
if (x1Value > 0 && x1Value > x1Max) {
x1Max = x1Value;
} else if (x1Value < 0 && x1Value < x1Min) {
x1Min = x1Value;
}
if (y1Value > 0 && y1Value > y1Max) {
y1Max = y1Value;
} else if (y1Value < 0 && y1Value < y1Min) {
y1Min = y1Value;
}
float x1sMax = abs(x1Max);
if (x1Value < 0) {
x1sMax = abs(x1Min);
}
float y1sMax = abs(y1Max);
if (y1Value < 0) {
y1sMax = abs(y1Min);
}
if (x2Value > 0 && x2Value > x2Max) {
x2Max = x2Value;
} else if (x2Value < 0 && x2Value < x2Min) {
x2Min = x2Value;
}
if (y2Value > 0 && y2Value > y2Max) {
y2Max = y2Value;
} else if (y2Value < 0 && y2Value < y2Min) {
y2Min = y2Value;
}
float x2sMax = abs(x2Max);
if (x2Value < 0) {
x2sMax = abs(x2Min);
}
float y2sMax = abs(y2Max);
if (y2Value < 0) {
y2sMax = abs(y2Min);
}
int16_t x1Final = (((float)x1Value / x1sMax)*127);
int16_t y1Final = (((float)y1Value / y1sMax)*127);
int16_t x2Final = (((float)x2Value / x2sMax)*127);
int16_t y2Final = (((float)y2Value / y2sMax)*127);
// Check if Axis need to be inverted, and if yes, do so
if (InvertX1 == 0) {
x1F1 = 127;
x1F2 = -127;
}
else {
x1F1 = -127;
x1F2 = 127;
}
if (InvertY1 == 0) {
y1F1 = 127;
y1F2 = -127;
}
else {
y1F1 = -127;
y1F2 = 127;
}
if (InvertX2 == 0) {
x2F1 = 127;
x2F2 = -127;
}
else {
x2F1 = -127;
x2F2 = 127;
}
if (InvertY2 == 0) {
y2F1 = 127;
y2F2 = -127;
}
else {
y2F1 = -127;
y2F2 = 127;
}
// Check how many analog sticks have been specified (0, 1, 2)
// and disable (center) any that aren't used.
if (AnalogSticks != 1 && AnalogSticks != 2) {
x1Final = 0;
y1Final = 0;
x2Final = 0;
y2Final = 0;
}
if (AnalogSticks == 1) {
x2Final = 0;
y2Final = 0;
}
Joystick.setXAxis(map(x1Final, x1F1, x1F2, 0, 255));
Joystick.setYAxis(map(y1Final, y1F1, y1F2, 0, 255));
Joystick.setRxAxis(map(x2Final, x2F1, x2F2, 0, 255));
Joystick.setRyAxis(map(y2Final, y2F1, y2F2, 0, 255));
}
void tButtonStates(){
// Button states are checked with digitalRead
// For a common ground PCB, it checks for a LOW state
// If it detects one on any button pin, it will return a corresponding
// value counting from 1-8192 and later on these values are added together,
// else the value is 0
if (digitalRead(PinA) == LOW) {
Joystick.setButton(A, 1);
}
else {
Joystick.setButton(A, 0);
}
if (digitalRead(PinB) == LOW) {
Joystick.setButton(B, 1);
}
else {
Joystick.setButton(B, 0);
}
if (digitalRead(PinX) == LOW) {
Joystick.setButton(X, 1);
}
else {
Joystick.setButton(X, 0);
}
if (digitalRead(PinY) == LOW) {
Joystick.setButton(Y, 1);
}
else {
Joystick.setButton(Y, 0);
}
if (digitalRead(PinStart) == LOW) {
Joystick.setButton(Start,1);
}
else {
Joystick.setButton(Start,0);
}
if (digitalRead(PinSelect) == LOW) {
Joystick.setButton(Select,1);
}
else {
Joystick.setButton(Select,0);
}
if (digitalRead(PinDPadUp) == LOW) {
Joystick.setButton(DPadUp,1);
}
else {
Joystick.setButton(DPadUp,0);
}
if (digitalRead(PinDPadDown) == LOW) {
Joystick.setButton(DPadDown,1);
}
else {
Joystick.setButton(DPadDown,0);
}
if (digitalRead(PinDPadLeft) == LOW) {
Joystick.setButton(DPadLeft,1);
}
else {
Joystick.setButton(DPadLeft,0);
}
if (digitalRead(PinDPadRight) == LOW) {
Joystick.setButton(DPadRight,1);
}
else {
Joystick.setButton(DPadRight,0);
}
if (digitalRead(PinL1) == LOW) {
Joystick.setButton(L1,1);
}
else {
Joystick.setButton(L1,0);
}
if (digitalRead(PinL2) == LOW) {
Joystick.setButton(L2,1);
}
else {
Joystick.setButton(L2,0);
}
if (digitalRead(PinR1) == LOW) {
Joystick.setButton(R1,1);
}
else {
Joystick.setButton(R1,0);
}
if (digitalRead(PinR2) == LOW) {
Joystick.setButton(R2,1);
}
else {
Joystick.setButton(R2,0);
}
}
void setup(){
// Declare buttons as inputs
pinMode(1, INPUT_PULLUP);
pinMode(0, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
// Declare specified pins as analog inputs for analog sticks
pinMode(x1Pin, INPUT);
pinMode(y1Pin, INPUT);
pinMode(x2Pin, INPUT);
pinMode(y2Pin, INPUT);
// Measure 'neutral' position for analog sticks
x1Zero = analogRead(x1Pin);
y1Zero = analogRead(y1Pin);
x2Zero = analogRead(x2Pin);
y2Zero = analogRead(y2Pin);
Joystick.setXAxisRange(-127, 127);
Joystick.setYAxisRange(-127, 127);
Joystick.setRxAxisRange(-127, 127);
Joystick.setRxAxisRange(-127, 127);
Joystick.setXAxis(0);
Joystick.setYAxis(0);
Joystick.setRxAxis(0);
Joystick.setRyAxis(0);
Joystick.begin();
}
void loop(){
tButtonStates();
tAnalogSticks();
}