switching form leonardo as joystick to unojoy

Hello everyone

I made a H-shifter for sim racing on pc. I created this with limit switches and a arduino leonardo. Since the leonardo was placed poorly, the connector almost broke off, since the arduino uno has a more robust connector I replaced it. Now since I'm using an arduino I also to Unojoy because this is the only way making an arduino uno as an input for your pc I know of. This was the original code I was running:

#include <Joystick.h>

Joystick_ Joystick;


void setup() {
  // put your setup code here, to run once:
for (int pin=0; pin!=13; pin++){
  pinMode(pin, INPUT_PULLUP);
}
pinMode(A0, OUTPUT);
Joystick.begin();
}

void loop() {
  digitalWrite(A0, LOW);//I used this pin as a ground
  Joystick.setXAxis(255/2);//making sure the joystick is in the middle position, so games don't do anything
  Joystick.setYAxis(255/2);
  for(int index=0; index!=13; index++){
    if(index>=6){//all pins above 6 and 6 itself get inverted since the switches are connected differently
      Joystick.setButton(index, !(digitalRead(index)));
    }
    else{
      Joystick.setButton(index, (digitalRead(index)));
    }
  }
  delay(1);
}

The code was working fine. Now I want to get the same result in Unojoy. But I don't see a instruction that works in the same way as the Joystick.setButton does, in there you can just give it a number but on unojoy u need to work with the button like for example an xbox controller. But this makes it harder to make it. I still tried this with no properly working result. This is the code I used:

#include "UnoJoy.h"

void setup() {

for (int pin=0; pin!=13; pin++){
  pinMode(pin, INPUT_PULLUP);
}
pinMode(A0, OUTPUT);
setupUnoJoy();
delay(100);
}

void loop() {
  //Joystick.setXAxis(255/2);
  //Joystick.setYAxis(255/2);
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
  delay(1);
}

dataForController_t getControllerData(void){
  int pin=0;
  dataForController_t controllerData = getBlankDataForController();
  controllerData.triangleOn = digitalRead(pin);
  pin++;
  controllerData.circleOn = digitalRead(pin);
  pin++;
  controllerData.crossOn = digitalRead(pin);
  pin++;
  controllerData.squareOn = digitalRead(pin);
  pin++;  
  controllerData.l2On = digitalRead(pin);
  pin++;
  controllerData.r2On = digitalRead(pin);
  pin++;
  controllerData.l1On = digitalRead(pin);
  pin++;
  controllerData.r1On = digitalRead(pin);
  pin++;
  controllerData.selectOn = digitalRead(pin);
  pin++;
  controllerData.startOn = digitalRead(pin);
  pin++;
  controllerData.l3On = digitalRead(pin);
  pin++;
  controllerData.r3On = digitalRead(pin);
  pin++;
  controllerData.dpadUpOn = digitalRead(pin);
}

Setting the arduino uno as joystick is no problem (putting it in DFU mode).
Thank you for helping,
TaNium

Have you thought about using an arduino Micro? They have the same ATmega32u4 so your code does not need to change.

Yes, and I ordered some but the problem is that the port isn't that robust so I would like to get it working on an arduino uno where the port is pretty robust. If I want to change to an arduino micro than I completly need to redesign I'm don't want to do this if there is an easier solution.

Hello I managed to solve the problem. I just binded every button with a button of the unojoy library and I works great now. I also forgot in the code I mentioned

return controllerData;

at the bottom.
This is the code I use. It uses al possible or almost all possible button of the unojoy library.

#include "UnoJoy.h"

void setup() {

for (int pin=1; pin<14; pin++){//don't use pin 0 since I think it is a communication pin and was giving me issues
  pinMode(pin, INPUT_PULLUP);
}
pinMode(A0,INPUT_PULLUP);//here are also button connected
pinMode(A2, INPUT_PULLUP);
pinMode(A5, INPUT_PULLUP);
pinMode(A1, OUTPUT);
pinMode(A4, OUTPUT);
digitalWrite(A1,LOW);//gnd for connecting wires since it was easier then soldering them togheter
digitalWrite(A4,LOW);
setupUnoJoy();
delay(100);
}

void loop() {
  //Joystick.setXAxis(255/2);
  //Joystick.setYAxis(255/2);
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
  delay(1);
}

dataForController_t getControllerData(void){
  dataForController_t controllerData = getBlankDataForController();
 controllerData.triangleOn = !digitalRead(A5);//some pins are inverted because I used NO insted of NC or vise versa
  controllerData.circleOn = !digitalRead(1);
  controllerData.squareOn = !digitalRead(2);
  controllerData.crossOn = !digitalRead(3);
  
  controllerData.r1On = !digitalRead(4);
  controllerData.r2On = !digitalRead(5);
  controllerData.l1On = !digitalRead(6);
  controllerData.l2On = !digitalRead(7);
  controllerData.r3On = digitalRead(8);
  controllerData.l3On = digitalRead(9);
  
  
  controllerData.dpadUpOn = digitalRead(10);
  controllerData.dpadDownOn = digitalRead(11);
  controllerData.dpadLeftOn = digitalRead(12);
  controllerData.dpadRightOn = digitalRead(13);
  
  controllerData.startOn = digitalRead(A0);
  controllerData.selectOn = digitalRead(A2);
  
  return controllerData;//this is the part I forgot in my first code
}