Arduino Pro Micro - keypad.h and joystick.h libraries - how to access key

Hello

I have problem with my newly built button box for PC. I have connected monostable and bistable switches and I would like to use some bistable switches to change state from 0 to 1 and to 0 again when I push them and same action when I release them.

As far as I was able to write is to change behaviour of all keys to monostable or bistable. How to select some specific buttons to act as a bistable?

My code (for monostable buttons):

//BUTTON BOX 45 buttons
//Arduino Pro Micro
//Tested in WIN7 + ETS2
//V1.0

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

#define NUMBUTTONS 45
#define NUMROWS 9
#define NUMCOLS 5


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,42,43,44},
};


byte rowPins[NUMROWS] = {21,20,19,18,15,14,16,10,9};
byte colPins[NUMCOLS] = {8,7,6,5,3};

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

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  JOYSTICK_TYPE_JOYSTICK, 45, 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 )  
            {
            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;
            }
           }  
         }
     }
}

Part of code when buttons act as a bistable buttons:

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:
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              break;
                    case HOLD:
                    case RELEASED:
                              Joystick.setButton(buttbx.key[i].kchar, 1);
                              delay(100);
                              Joystick.setButton(buttbx.key[i].kchar, 0);
                    case IDLE:
                    break;
            }
           }  
         }
     }
}

It works somehow good, but I would like to have chance to pick some buttons to act as a monostable. In that solution when I press and release button it only one time goes from 0 to 1 and 0 again. And if I press and hold and release button it goes to 0 - 1 - 0 when hold and after release it does it again 0 - 1 - 0.

Thank You in advance for help.

Here is one way to do it. An array of flags, indexed by button number, to say if the button is bistable or not:

//BUTTON BOX 45 buttons
//Arduino Pro Micro
//Tested in WIN7 + ETS2
//V1.0


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


#define NUMBUTTONS 45
#define NUMROWS 9
#define NUMCOLS 5


// Set the switches to 'true' and the buttons to 'false'
boolean isBistable[45] = 
{
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false,
  false, false, false, false, false
};


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, 42, 43, 44},
};




byte rowPins[NUMROWS] = {21, 20, 19, 18, 15, 14, 16, 10, 9};
byte colPins[NUMCOLS] = {8, 7, 6, 5, 3};


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


Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                   JOYSTICK_TYPE_JOYSTICK, 45, 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())
    return;  // Nothing to do


  for (int i = 0; i < LIST_MAX; i++)
  {
    byte buttonNumber = buttbx.key[i].kchar;
    if ( buttbx.key[i].stateChanged )
    {
      if (isBistable[buttonNumber])
      {
        // Bistable (toggle) switch
        switch (buttbx.key[i].kstate)
        {
          case PRESSED:
          case RELEASED:
            // Act as momentary press on both switch on and switch off
            Joystick.setButton(buttonNumber, 1);
            delay(100);
            Joystick.setButton(buttonNumber, 0);
            break;


          case HOLD:
          case IDLE:
          default:
            break;
        }
      }
      else
      { 
        // Monostable (momentary) button
        switch (buttbx.key[i].kstate)
        {
          case PRESSED:
            Joystick.setButton(buttonNumber, 1);
            break;
            
          case RELEASED:
            Joystick.setButton(buttonNumber, 0);
            break;
            
          case HOLD:
          case IDLE:
          default:
            break;
        }
      }
    }
  }
}

Hello

Thank You very much! I will check it later. Great - I hope that will solve my problem:)

Hello

It works great! Thank You very much John.