Rotary Switch as analog input but problems

Hi,
I made a rotary switch act like a potentiometer with soldering resistors for the legs except between 1 and 12. I soldered the common to A3, leg 1 and leg 12 to GND and 5V. It works well until I need to use it as a momentary push button. As a always pushed button, I can use and switch between input.

My problem is that I use a Nextion display where I can bind buttons to switch screens but the software I use only accept momentary push buttons for this. Is there any easy solution for this to not to use this much line of code and make it as momentary push button from an analog input?

The other problem I have is if the code is using this rotary switch as an input my rotary encoders are not working and I can’t find the connection between this. If I don’t use the part where the rotary switch is declared I can use the encoders without any problem.

#include <Keypad.h>
#include <Joystick.h>

#define ENABLE_PULLUPS
#define NUMROTARIES 2
#define NUMBUTTONS 22
#define NUMROWS 4
#define NUMCOLS 4

unsigned long time;

byte buttons[NUMROWS][NUMCOLS] = {
  {0, 1, 2, 3},
  {4, 5, 6, 7},
  {8, 9, 10, 11},
  {12, 13, 14, 15},
};

struct rotariesdef {
  byte pin1;
  byte pin2;
  int ccwchar;
  int cwchar;
  volatile unsigned char state;
};

rotariesdef rotaries[NUMROTARIES] {
  {10, 11, 28, 29, 0},
  {12, 13, 30, 31, 0},

};

#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0

#ifdef HALF_STEP
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char ttable[6][4] = {
  // R_START (00)
  {R_START_M,            R_CW_BEGIN,     R_CCW_BEGIN,  R_START},
  // R_CCW_BEGIN
  {R_START_M | DIR_CCW, R_START,        R_CCW_BEGIN,  R_START},
  // R_CW_BEGIN
  {R_START_M | DIR_CW,  R_CW_BEGIN,     R_START,      R_START},
  // R_START_M (11)
  {R_START_M,            R_CCW_BEGIN_M,  R_CW_BEGIN_M, R_START},
  // R_CW_BEGIN_M
  {R_START_M,            R_START_M,      R_CW_BEGIN_M, R_START | DIR_CW},
  // R_CCW_BEGIN_M
  {R_START_M,            R_CCW_BEGIN_M,  R_START_M,    R_START | DIR_CCW},
};
#else
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6

const unsigned char ttable[7][4] = {
  // R_START
  {R_START,    R_CW_BEGIN,  R_CCW_BEGIN, R_START},
  // R_CW_FINAL
  {R_CW_NEXT,  R_START,     R_CW_FINAL,  R_START | DIR_CW},
  // R_CW_BEGIN
  {R_CW_NEXT,  R_CW_BEGIN,  R_START,     R_START},
  // R_CW_NEXT
  {R_CW_NEXT,  R_CW_BEGIN,  R_CW_FINAL,  R_START},
  // R_CCW_BEGIN
  {R_CCW_NEXT, R_START,     R_CCW_BEGIN, R_START},
  // R_CCW_FINAL
  {R_CCW_NEXT, R_CCW_FINAL, R_START,     R_START | DIR_CCW},
  // R_CCW_NEXT
  {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
};
#endif

byte rowPins[NUMROWS] = {2, 4, 5, 8};
byte colPins[NUMCOLS] = {3, 6, 7, 9};

Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);

Joystick_ Joystick;

void setup() {
  Joystick.begin();
  rotary_init();
}

void loop() {

  CheckAllEncoders();

  RotaryS();

  CheckAllButtons();

}

void CheckAllButtons(void) {
  if (buttbx.getKeys())
  {
    for (int i = 0; i < LIST_MAX; i++)
    {
      if ( buttbx.key[i].stateChanged )
      {
        switch (buttbx.key[i].kstate) {
          case PRESSED:
          case HOLD:
            Joystick.setButton(buttbx.key[i].kchar, 1);
            break;
          case RELEASED:
          case IDLE:
            Joystick.setButton(buttbx.key[i].kchar, 0);
            break;
        }
      }
    }
  }
}


void rotary_init() {
  for (int i = 0; i < NUMROTARIES; i++) {
    pinMode(rotaries[i].pin1, INPUT);
    pinMode(rotaries[i].pin2, INPUT);
#ifdef ENABLE_PULLUPS
    digitalWrite(rotaries[i].pin1, HIGH);
    digitalWrite(rotaries[i].pin2, HIGH);
#endif
  }
}


unsigned char rotary_process(int _i) {
  unsigned char pinstate = (digitalRead(rotaries[_i].pin2) << 1) | digitalRead(rotaries[_i].pin1);
  rotaries[_i].state = ttable[rotaries[_i].state & 0xf][pinstate];
  return (rotaries[_i].state & 0x30);
}

void CheckAllEncoders(void) {
  for (int i = 0; i < NUMROTARIES; i++) {
    unsigned char result = rotary_process(i);
    if (result == DIR_CCW) {
      Joystick.setButton(rotaries[i].ccwchar, 1); delay(50); Joystick.setButton(rotaries[i].ccwchar, 0);
    };
    if (result == DIR_CW) {
      Joystick.setButton(rotaries[i].cwchar, 1); delay(50); Joystick.setButton(rotaries[i].cwchar, 0);
    };
  }
}


void RotaryS(void) {
  //Rotary Switch
  int RS = analogRead(A3);
  RS = map(RS, 0, 1023, 1, 12);

  //1
  if (RS == 1) {
    Joystick.setButton(16, 1);
  }
  else {
    Joystick.setButton(16, 0);
  }

  //2
  if (RS == 2) {
    Joystick.setButton(17, 1);
  }
  else {
    Joystick.setButton(17, 0);
  }

  //3
  if (RS == 3) {
    Joystick.setButton(18, 1);
  }
  else {
    Joystick.setButton(18, 0);
  }

  //4
  if (RS == 4)  {
    Joystick.setButton(19, 1);
  }
  else {
    Joystick.setButton(19, 0);
  }

  //5
  if (RS == 5)  {
    Joystick.setButton(20, 1);
  }
  else {
    Joystick.setButton(20, 0);
  }

  //6
  if (RS == 6)  {
    Joystick.setButton(21, 1);
  }
  else {
    Joystick.setButton(21, 0);
  }

  //7
  if (RS == 7)  {
    Joystick.setButton(22, 1);
  }
  else {
    Joystick.setButton(22, 0);
  }

  //8
  if (RS == 8)  {
    Joystick.setButton(23, 1);
  }
  else {
    Joystick.setButton(23, 0);
  }

  //9
  if (RS == 9)  {
    Joystick.setButton(24, 1);
  }
  else {
    Joystick.setButton(24, 0);
  }

  //10
  if (RS == 10)  {
    Joystick.setButton(25, 1);
  }
  else {
    Joystick.setButton(25, 0);
  }

  //11
  if (RS == 11)  {
    Joystick.setButton(26, 1);
  }
  else {
    Joystick.setButton(26, 0);
  }

  //12
  if (RS == 12)  {
    Joystick.setButton(27, 1);
  }
  else {
    Joystick.setButton(27, 0);
  }
}

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

RS = map(RS,1023,0,1023,0); Interesting
https://www.arduino.cc/reference/en/language/functions/math/map/

“The other problem is that I use rotary encoders too, but only one can work.”
Don’t include extraneous information.