Keypad _Keyboard output - Switch

Hi All

Im stuck in my project.

I have a 2 Row and 5 Col in my keypad Matrix

5 Buttons
5 Switches

For now everything works fine with the buttons and switches
But if i actuate the switch in pos. on, the other buttons not responding until i pull the switch down.
I know why this happend but not how i have to handle this.

What the switch should do

Position on = Keyboard → L
Position off = Keyboard → L

Thanks a lot!

#include <Keypad.h>
#include <Keyboard.h>


#define ENABLE_PULLUPS
#define NUMROTARIES 4
#define NUMBUTTONS 10
#define NUMROWS 2
#define NUMCOLS 5


//define the symbols on the buttons of the keypads
char buttons[NUMROWS][NUMCOLS] = {
  {'q','w','e','r','t'},
  {'y','u','i','o','p'},

};



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

rotariesdef rotaries[NUMROTARIES] {
  {0,1,KEY_RIGHT_ARROW,KEY_LEFT_ARROW,0},
  {2,3,KEY_DOWN_ARROW,KEY_UP_ARROW,0},
  {4,5,KEY_PAGE_UP,KEY_PAGE_DOWN,0},
  {6,7,KEY_HOME,KEY_END,0},
};


// 
//Encoders code from Ben Buxton
//More info: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html
// 

#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] = {0,1}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {2,3,4,5,6}; //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() {
  Keyboard.begin();
  rotary_init();
}



void loop() { 

  CheckAllEncoders();

  CheckAllButtons();

}


void CheckAllButtons(void) {
  char key = buttbx.getKey();
  if (key != NO_KEY)  {
  //  Keyboard.press(KEY_LEFT_CTRL);
  //  Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(key);
    delay(150);
    Keyboard.releaseAll();
  }//Keyboard.write(key);
}


/* 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) {
      Keyboard.write(result == DIR_CCW ? rotaries[i].ccwchar : rotaries[i].cwchar ); 
    }
  }
}

I think the problem is using .getKey() instead of .getKeys(). See the Keypad example MultiKey.

The important part is this:

    // Fills kpd.key[ ] array with up-to 10 active keys.
    // Returns true if there are ANY active keys.
    if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
        {
            if ( kpd.key[i].stateChanged )   // Only find keys that have changed state.
            {
               /* Here you can sort out what to do based on the key and the
                  new state.  You only need to handle PRESSED and RELEASED.
               kpd.key[i].kstate  // IDLE, PRESSED, HOLD, or RELEASED
               kpd.key[i].kchar  // The letter from you keypad layout
               kpd.key[i].kcode  // The index into your eypad layout
               */
            }
        }
   }

Thanks you so much! :slight_smile:

But for now i have this error message:

Buttonbox_pre_Master1:101:5: error: ‘kpd’ was not declared in this scope
if (kpd.getKeys())
^~~
exit status 1
‘kpd’ was not declared in this scope

Maybe im too far away from understanding the keypad lib. :o
I think i buy a Multiplexer for use all the switches as normal digital inputs :-/

Here the new code.

#include <Keypad.h>
#include <Keyboard.h>


#define ENABLE_PULLUPS
#define NUMROTARIES 4
#define NUMBUTTONS 10
#define NUMROWS 2
#define NUMCOLS 5


//define the symbols on the buttons of the keypads
char buttons[NUMROWS][NUMCOLS] = {
  {'q','w','e','r','t'},
  {'y','u','i','o','p'},
};



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

rotariesdef rotaries[NUMROTARIES] {
  {0,1,KEY_RIGHT_ARROW,KEY_LEFT_ARROW,0},
  {2,3,KEY_DOWN_ARROW,KEY_UP_ARROW,0},
  {4,5,KEY_PAGE_UP,KEY_PAGE_DOWN,0},
  {6,7,KEY_HOME,KEY_END,0},
};


// 
//Encoders code from Ben Buxton
//More info: http://www.buxtronix.net/2011/10/rotary-encoders-done-properly.html
// 

#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] = {0,1}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {2,3,4,5,6}; //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() {
  Keyboard.begin();
  rotary_init();
}



void loop() { 

  CheckAllEncoders();

  CheckAllButtons();

}


void CheckAllButtons(void) {
//  char key = buttbx.getKey();
//  if (key != NO_KEY)  {
//    Keyboard.press(key);
//    delay(150);
//    Keyboard.releaseAll();
//  }//Keyboard.write(key);
if (kpd.getKeys())
    {
        for (int i=0; i<LIST_MAX; i++)   // Scan the whole key list.
        {
            if ( kpd.key[i].stateChanged )   // Only find keys that have changed state.
            { kpd.key[i].kstate  // IDLE, PRESSED, HOLD, or RELEASED
               /* Here you can sort out what to do based on the key and the
                  new state.  You only need to handle PRESSED and RELEASED.
               kpd.key[i].kstate  // IDLE, PRESSED, HOLD, or RELEASED
               kpd.key[i].kchar  // The letter from you keypad layout
               kpd.key[i].kcode  // The index into your eypad layout
               */
            }
        }
   }



}


/* 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) {
      Keyboard.write(result == DIR_CCW ? rotaries[i].ccwchar : rotaries[i].cwchar ); 
    }
  }
}

You called your Keypad "buttbx". Use that name instead of the name from the example. After that you only have to get rid of the (do-nothing) statement (which is missing a ';'):

        buttbx.key[i].kstate  // IDLE, PRESSED, HOLD, or RELEASED

I would recommend replacing it with a "switch" statement based on "buttbx.key_.kcode" (0, 1, 2, 3...) or "buttbx.key*.kchar" ('q', 'w', 'e', 'r'...) to separate out keys or groups of keys and decide what to do in each case based on the new state.*_

Thanks a lot

Due to have to little pins anyway i decided to buy a multiplexer and using common digital ins. But i go to test your advice for sure :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.