Problem with toggle on off on switches in keypad and joystick library

Hello,
I have problem with keypad and joystick library, what am i doing is button box with keypad and two on - off - on toggles. In code below i would like to implement toggle so if button 52 and 53 are released it will press joystick button 56, same with 54 and 55 then press joystick button 57.

//MicroUSBButtonBox by Les O'Reilly
//
//Arduino IDE 1.6.6 (or above) !
//
//
//This Makes a ZERO Axis with 56 Button USB Controller (Game pad)
//Keypad will configure the Buttons 0-55 

//A3,A2,A1,A0 Are the Row Pins for the Keypad D0-D10,D14-D16 are the Column Pins
//Button 0 wires as A3+D16 Where A3 is Row and D13 is Column, Button 13 - A3+D0
//Button 14 wires as A2+D16 ...Button 27 A2+D0
//Button 28 wires as A1+D16...Button 41 A1+D0
//Button 42 wires as A0+D16....Button 55 A0+D0


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


#define NUMBUTTONS 56
#define NUMROWS 4
#define NUMCOLS 14


//define the symbols on the buttons of the keypads
byte buttons[NUMROWS][NUMCOLS] = {
  {0,1,2,3,4,5,6,7,8,9,10,11,12,13},
  {14,15,16,17,18,19,20,21,22,23,24,25,26,27},
  {28,29,20,31,32,33,34,35,36,37,38,39,40,41},
  {42,43,44,45,46,47,48,49,50,51,52,53,54,55},
};


byte rowPins[NUMROWS] = {21,20,19,18}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {16,15,14,10,9,8,7,6,5,4,3,2,1,0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS); 

//initialize an Joystick with 56 buttons;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, 
  JOYSTICK_TYPE_JOYSTICK, 60, 0,
  false, false, false, false, false, false,
  false, false, false, false, false);


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


void loop() { 
CheckAllButtons();

}
  
void CheckAllButtons(void) {
                if (buttbx.getKeys())
            {
              for (int i = 0; i < LIST_MAX; i++)
            {
              if ( buttbx.key[i].stateChanged )
                if(i >= 1 && i <= 56) {
                          // do toggle things
              }
               else {
              // do non-toggle things
                }
               
                switch (buttbx.key[i].kstate)
                    {
                      case HOLD:
                         Joystick.setButton(56, 0);
                   
                         case IDLE:
                          case RELEASED:
                          Joystick.setButton(56, 1);
                     
                          break;
}
            
 
                switch (buttbx.key[i].kstate) {
                case PRESSED:
                case HOLD:
                  Joystick.setButton(buttbx.key[i].kchar, 1);
                  delay(0);
                    break;
                  case RELEASED:
                  case IDLE:
                Joystick.setButton(buttbx.key[i].kchar, 0);
                  delay(0);
                  break;
                     }
            }
                
}
}

What is your question ?

Is it possible to make that? When i added else if, the loop stopped working and no button presses.

I tried something like this to make toggle work, but now i cannot get switch7 release status, it wont press joystick button 46


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

#define NUMBUTTONS 42
#define NUMROWS 6
#define NUMCOLS 7


//define the symbols on the buttons of the keypads
byte buttons[NUMROWS][NUMCOLS] = {
  {0, 1, 2, 3, 4, 5, 6},
  {7,8, 9, 10, 11, 12, 13},
  {14,15, 16, 17, 18, 19, 20},
  {21,22, 23, 24, 25, 26, 27},
  {28,29, 30, 31, 32, 33, 34},
  {35,36, 37, 38, 39, 40, 41},
  };


byte rowPins[NUMROWS] = {21, 20, 19, 18, 15, 14}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {6, 5, 4, 3, 2, 1, 0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);

//initialize an Joystick with 56 buttons;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                   JOYSTICK_TYPE_JOYSTICK, 50, 0,
                   false, false, false, false, false, false,
                   false, false, false, false, false);

ezButton toggleSwitch7(7);
ezButton toggleSwitch8(8);
ezButton toggleSwitch9(9);
ezButton toggleSwitch10(10);

void setup() {
  Joystick.begin();
  Serial.begin(9600);
  toggleSwitch7.setDebounceTime(50);
  toggleSwitch8.setDebounceTime(50);
  toggleSwitch9.setDebounceTime(50);
  toggleSwitch10.setDebounceTime(50);
}


void loop() {
  CheckAllButtons();
  toggleSwitch7.loop();
  toggleSwitch8.loop();
  toggleSwitch9.loop();
  toggleSwitch10.loop();
  int btn1State = toggleSwitch7.getState();
  int btn2State = toggleSwitch8.getState();
  int btn3State = toggleSwitch9.getState();
  int btn4State = toggleSwitch10.getState();


  if(toggleSwitch7.isPressed())
    Joystick.setButton(43,1);
    
    
    
  if(toggleSwitch7.isReleased())
    Joystick.setButton(46,1);
     
    
    
  if(toggleSwitch8.isPressed())
    Joystick.setButton(44,1);
     
    Joystick.setButton(45,0);

  if(toggleSwitch8.isReleased())
    Joystick.setButton(44,0);
  
   Joystick.setButton(45,1);

  if(toggleSwitch9.isPressed())
    

  if(toggleSwitch9.isReleased())
    
  if(toggleSwitch10.isPressed())
    

  if(toggleSwitch10.isReleased())
    ;
    
  /*if(digitalRead(switch_pin7) == HIGH){
   Joystick.setButton(43, 1);
    Joystick.setButton(46, 0);
    Serial.println("ON");
  }
  /*if(digitalRead(switch_pin7) == LOW){
    Joystick.setButton(43, 0);
    Joystick.setButton(46, 1);
    Serial.println("OFF");
  }*/
  
}

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

Please clean up your code before you post. It's full of things like

        if (i >= 1 && i <= 42)
        {
        }
        else
        {
        }

and calls and variables that you don't use

  int btn1State = toggleSwitch7.getState();
  int btn2State = toggleSwitch8.getState();
  int btn3State = toggleSwitch9.getState();
  int btn4State = toggleSwitch10.getState();

I tried to clean the mess, now keypad binding works but toggles won't, when i connect toggle switch7 to pin 7 and GND, button 44 appears but it wont go off when its released, still i dont know how to implement toggle to this code to make it work.


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

#define NUMBUTTONS 42
#define NUMROWS 6
#define NUMCOLS 7


//define the symbols on the buttons of the keypads
byte buttons[NUMROWS][NUMCOLS] = {
  {0, 1, 2, 3, 4, 5, 6},
  {7, 8, 9, 10, 11, 12, 13},
  {14, 15, 16, 17, 18, 19, 20},
  {21, 22, 23, 24, 25, 26, 27},
  {28, 29, 30, 31, 32, 33, 34},
  {35, 36, 37, 38, 39, 40, 41},
};


byte rowPins[NUMROWS] = {21, 20, 19, 18, 15, 14}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {6, 5, 4, 3, 2, 1, 0}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);

//initialize an Joystick with 56 buttons;
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                   JOYSTICK_TYPE_JOYSTICK, 50, 0,
                   false, false, false, false, false, false,
                   false, false, false, false, false);

ezButton toggleSwitch7(7);
ezButton toggleSwitch8(8);
ezButton toggleSwitch9(9);
ezButton toggleSwitch10(10);

void setup() {
  Joystick.begin();
  Serial.begin(9600);
  toggleSwitch7.setDebounceTime(50);
  toggleSwitch8.setDebounceTime(50);
  toggleSwitch9.setDebounceTime(50);
  toggleSwitch10.setDebounceTime(50);
}


void loop() {
  CheckAllButtons();
  toggleSwitch7.loop(); // MUST call the loop() function first
  toggleSwitch8.loop(); // MUST call the loop() function first
  toggleSwitch9.loop();
  toggleSwitch10.loop();



  if (toggleSwitch7.isPressed())
    Joystick.setButton(43, 1);
  Joystick.setButton(40, 0);

  if (toggleSwitch7.isReleased())
    Joystick.setButton(43, 0);
  Joystick.setButton(45, 1);

  if (toggleSwitch8.isPressed())
    Joystick.setButton(44, 1);


  if (toggleSwitch8.isReleased())
    Joystick.setButton(44, 0);


   if (toggleSwitch7.isPressed())
    Joystick.setButton(43, 1);
  Joystick.setButton(47, 0);

  if (toggleSwitch7.isReleased())
    Joystick.setButton(43, 0);
  Joystick.setButton(47, 1);

  if (toggleSwitch8.isPressed())
    Joystick.setButton(44, 1);
    Joystick.setButton(47, 0);

  if (toggleSwitch8.isReleased())
    Joystick.setButton(44, 0);
     Joystick.setButton(47, 1);

  if (toggleSwitch9.isPressed())
      Joystick.setButton(45, 1);

    if (toggleSwitch9.isReleased())
      Joystick.setButton(45, 0);
      if (toggleSwitch10.isPressed())
      Joystick.setButton(46, 1);

        if (toggleSwitch10.isReleased())
          Joystick.setButton(46, 0);

  /*if(digitalRead(switch_pin7) == HIGH){
    Joystick.setButton(43, 1);
    Joystick.setButton(46, 0);
    Serial.println("ON");
    }
    /*if(digitalRead(switch_pin7) == LOW){
    Joystick.setButton(43, 0);
    Joystick.setButton(46, 1);
    Serial.println("OFF");
    }*/

}

void CheckAllButtons(void) {
  if (buttbx.getKeys())
  {
    for (int i = 0; i < LIST_MAX; i++)
    {


      switch (buttbx.key[i].kstate) {
        case PRESSED:
          Joystick.setButton(buttbx.key[i].kchar, 1);
          delay(0);
          break;
        case HOLD:

        case RELEASED:
          Joystick.setButton(buttbx.key[i].kchar, 0);
          delay(0);
          break;
        case IDLE:
          break;
      }
    }
  }
}

Just to make things perfectly clear, can you please post a diagram of the switch circuits? If the keypad is working, you can leave that out if it's not connected with the toggles at all.

Do the other three toggles work, or not?

So the toggles works when they are like this

if (toggleSwitch7.isPressed())
    Joystick.setButton(43, 1);


  if (toggleSwitch7.isReleased())
    Joystick.setButton(43, 0);


  if (toggleSwitch8.isPressed())
    Joystick.setButton(44, 1);


  if (toggleSwitch8.isReleased())
    Joystick.setButton(44, 0);


  if (toggleSwitch9.isPressed())
    Joystick.setButton(45, 1);

  if (toggleSwitch9.isReleased())
    Joystick.setButton(45, 0);
  if (toggleSwitch10.isPressed())
    Joystick.setButton(46, 1);

  if (toggleSwitch10.isReleased())
    Joystick.setButton(46, 0);


Here is type of toggle
when i tried to use code like this

if (toggleSwitch7.isPressed())
    Joystick.setButton(43, 1);
Joystick.setButton(47, 0);

  if (toggleSwitch7.isReleased())
    Joystick.setButton(43, 0);
Joystick.setButton(47, 1);

  if (toggleSwitch8.isPressed())
    Joystick.setButton(44, 1);
Joystick.setButton(47, 0);

  if (toggleSwitch8.isReleased())
    Joystick.setButton(44, 0);
Joystick.setButton(47, 1);

  if (toggleSwitch9.isPressed())
    Joystick.setButton(45, 1);

  if (toggleSwitch9.isReleased())
    Joystick.setButton(45, 0);
  if (toggleSwitch10.isPressed())
    Joystick.setButton(46, 1);

  if (toggleSwitch10.isReleased())
    Joystick.setButton(46, 0);

all toggles stopped working and it only shows button 48 active.

When 3 way toggle is in off state i would like to use another button for example button 48 (joystick.setButton(47,1);

Does that library enable the internal pullup resistors on the input pins? You would need it with that circuit.

I can't comment on your code logic at this point...

do you mean high and low state?

I mean, configure the inputs with the INPUT_PULLUP option so they will actually default to a HIGH state when the switch contacts are open:
https://docs.arduino.cc/learn/microcontrollers/digital-pins