I Need Help For My ButtonBox Code

Hello guys. When I want to use “On / Off Sw” in the code below, the switch sends “1” twice when it is in the “on” section. I want him to always send “1” when the switch is in the “On” section. Not twice “1”. I will use 2 “On / Off Sw”. I’m gonna put these in place of the buttons.

The switch I use is this: Red illuminated LED Toggle Switch With Missile Style Flick Cover Car Dash 12V TM|switch styles|switch switchswitch with led - AliExpress

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


#define ENABLE_PULLUPS
#define NUMROTARIES 4
#define NUMBUTTONS 25
#define NUMROWS 5
#define NUMCOLS 5


//define the symbols on the buttons of the keypads
byte buttons[NUMROWS][NUMCOLS] = {
  {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},
};



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

rotariesdef rotaries[NUMROTARIES] {
  {0,1,26,27,0},
  {2,3,28,29,0},
  {4,5,30,31,0},
  {6,7,32,33,0},
};



#define DIR_CCW 0x10
#define DIR_CW 0x20

#define R_START 0x0
#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},
};


byte rowPins[NUMROWS] = {21,20,19,18,15}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {14,16,10,9,8}; //connect to the column pinouts of the keypad

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



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



void loop() {

  CheckAllEncoders();

  CheckAllButtons();

}


void CheckAllButtons(void) {
      if (buttbx.getKeys())
    {
       for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
        {
            switch (buttbx.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
                    case PRESSED:
                    case HOLD:
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              break;
                    case RELEASED:
                    case IDLE:
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                              break;
            }
              
         }
     }
}


/* Call this once in setup(). */
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
  }
}


/* Read input pins and process for events. Call this either from a
 * loop or an interrupt (eg pin change or timer).
 *
 * Returns 0 on no event, otherwise 0x80 or 0x40 depending on the direction.
 */
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);
    };
  }
}

why don't you write a small code just for the switch and get that to work as you need. Then you can integrate in your larger piece of code

Try this switch statement instead:

      switch (buttbx.key[i].kstate)    // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
      {
        case PRESSED:
          Joystick.setButton(buttbx.key[i].kchar, 1);
          break;

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

        default:
      }

First of all, thank you for your reply. I tried the code you wrote. Sends "1" once in the code. This way, the buttons, enc and toggle "on / off / on" are working properly. But unfortunately the toggle "on / off" does not work properly. Because when this button is "on", it must be "1" continuously. For this, I thought I should use "HOLD" in the line of code. When I set "HOLD" and "PRESSED" together in the code section to "1", it gives "1" twice. What kind of change do you think I need to make?

To get more than 1 joystick event for each button state change you will have to ignore the return value from .getKeys() which only returns ‘true’ when a button has changed state. If you only act on state changes you will only get ‘1’ on the change from IDLE to PRESSED and from PRESSED to HOLD.

void CheckAllButtons(void)
{
  buttbx.getKeys();  // Ignore the return value and always check the key list.

  for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
  {
    switch (buttbx.key[i].kstate)    // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
    {
      case PRESSED:
      case HOLD:
        Joystick.setButton(buttbx.key[i].kchar, 1);
        break;
      case RELEASED:
        Joystick.setButton(buttbx.key[i].kchar, 0);
        break;
    }
  }
}

I tried the code you wrote. Everything works perfectly, but I have a little problem.

My problem:

I’ve assigned the “On / Off” toggle as the engine start button. When I set the “On / Off” toggle to “on”, the engine starts but it doesn’t stop the engine when I turn Toggle “off”. I think it should send 1 when it’s off. Let’s say “On” is assigned as the joystick1 button. Toggle “Off” should also be assigned as the joystick1 button. So I can turn off the engine I turned on.

The ways I’ve tried for a solution:

1-) I tied Toggle’s two legs to the same place. I tied his other leg to gnd. It did not work.

2-) I tried the code you wrote in the forum :

void CheckAllButtons(void)
{
  buttbx.getKeys();  // Ignore the return value and always check the key list.

  for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
  {
    switch (buttbx.key[i].kstate)    // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
    {
      case PRESSED:
      case HOLD:
        Joystick.setButton(buttbx.key[i].kchar, 1);
        break;
      case RELEASED:
        Joystick.setButton(buttbx.key[i].kchar, 0);
        break;
    }
  }
}

3-) I wrote a code for Toggle’s “Off” leg. It did not work.

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

I have spent the past 12 hours trying to figure out something like what I believe you are trying to accomplish. There are hundreds of posts with no answers. I dont have the answer to my problem yet. I am going to continue searching because I am close.

I have the code figured out to make a 32 switch box that uses toggle switches.

When you flip the toggle switch on it pulses a button for 100 ms. then turns it off.
Then when you turn the switch back off again. It presses a button in the windows usb game controller one time again for 100ms.

I am using a Arduino Leonardo (so my code will reflect this) I am 100% new to this. I got the Arduino kit and ran through those experiements but my main project is making a push button box for both Truck simulators. (I can use it for flightsim too I am sure)

Anyhow here is what I have.

See the attached code. I think it is a perfectly clean version of 32 buttons doing exactly what you want. There are no rotary buttons like everyone else’s code.

What I am trying to figure out is how to code this so that some switches can be pushbuttons and some can be toggle switches that stay in their positions.

I cannot figure that out yet.

I suspect this bit of code

      if (buttbx.getKeys())
    {
       for (int i=0; i<LIST_MAX; i++)   
        {
           if ( buttbx.key[i].stateChanged )   
            {
            switch (buttbx.key[i].kstate)

is a variable. and I need to make it a number between 1-32 to correspond to a button.

attached is my code that will work to make all your toggle switches work like you intend.

Simple_32_Button_Box-ToggleSwitch.ino (1.59 KB)