I am trying to make a wired and wireless joystick, but what should I do?

I’m trying to make a wired and wireless joystick using two Arduino Leonardo, what should I do?

Digital 4 ~ 13 buttons 1 ~ 10

Analog A0 to A3 (digital 18 to 21) were vertically and horizontally

  1. Normal wired joystick when wired

  2. Connect the Arduino connected to the joystick with a battery

Connect the receiver to the computer and recognize it as a joystick

I want to write wirelessly

#include <Joystick.h>

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
10, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering

RF24 radio(2, 3);
const byte address[6] = “00001”;

void setup() {

// Initialize Button Pins
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(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);

// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);

radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}

// Last state of the buttons
int lastButtonState[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void loop() {

// Read pin values
for (int index = 0; index < 18; index++) {
int currentButtonState = !digitalRead(index + 4);
if (currentButtonState != lastButtonState[index]) {
switch (index) {
case 0: // Button 1
Joystick.setButton(0, currentButtonState);
break;
case 1: // Button 2
Joystick.setButton(1, currentButtonState);
break;
case 2: // Button 3
Joystick.setButton(2, currentButtonState);
break;
case 3: // Button 4
Joystick.setButton(3, currentButtonState);
break;
case 4: // Button 5
Joystick.setButton(4, currentButtonState);
break;
case 5: // Button 6
Joystick.setButton(5, currentButtonState);
break;
case 6: // Button 7
Joystick.setButton(6, currentButtonState);
break;
case 7: // Button 8
Joystick.setButton(7, currentButtonState);
break;
case 8: // Button 9
Joystick.setButton(8, currentButtonState);
break;
case 9: // Button 10
Joystick.setButton(9, currentButtonState);
break;
case 10: // Button
break;
case 11: // Button
break;
case 12: // Button
break;
case 13: // Button
break;
case 14: // UP
if (currentButtonState == 1) {
Joystick.setYAxis(-1);
} else {
Joystick.setYAxis(0);
}
break;
case 15: // DOWN
if (currentButtonState == 1) {
Joystick.setYAxis(1);
} else {
Joystick.setYAxis(0);
}
break;
case 16: // RIGHT
if (currentButtonState == 1) {
Joystick.setXAxis(-1);
} else {
Joystick.setXAxis(0);
}
break;
case 17: // LEFT
if (currentButtonState == 1) {
Joystick.setXAxis(1);
} else {
Joystick.setXAxis(0);
}
break;
}
lastButtonState[index] = currentButtonState;
radio.write(&lastButtonState[index], sizeof(lastButtonState));
Serial.println(lastButtonState[index]);
}
}
delay(20);
}