[solved] keypad.h data type

Hi!

I want to use the keypad library for sending MIDI messages.
So instead if having characters mapped to the buttons i would need to have numbers from 0 to 127. The problem is that i don't know how to change the data type (char) to integer or byte.

I also could map the keypad to char and use switch/case but that is ugly because it consumes more processing power.

Anyone have any idea how to do this?

char key = 'a';
byte num ;

num = (byte) key;

It is called casting a variable.

To map a key to a number use an array.

Thank you! It works great. After a made some research, surprisingly, ASCII has characters from 0 to 255. Maybe more according to this site.

I forgot about casting data types, that was so basic.

So, i implemented the method Grumpy_Mike suggested and i got a little bit of a problem. When i press a certain key ("41" which is "(char) 0" in the keys[][] declaration) and hold it i get the RELEASE state (noteOn) message a few more times as i press other keys, then it settles down and doesn't appear again.

In the old code, the one with the "switch" statement it didn't do that. There's something off and i don't know what. I increased the debounce time but nothing changed.

//old
#include <Keypad.h>

bool debug = true;

const byte ROWS = 4; //four rows
const byte COLS = 8; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','4','5','6','7','8'},
{'q','w','e','r','t','y','u','i'},
{'a','s','d','f','g','h','j','k'},
{'z','x','c','v','b','n','m',','}
};
byte rowPins[ROWS] = {10, 11, 12, 13}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long loopCount;
unsigned long startTime;
String msg;

byte cmd, note = 55, velocity = 100;
byte shift = 41;

void setup() {
    Serial.begin(38400);
    //kpd.setDebounceTime(300);
}


void loop() {
    // 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.
            {
                if (kpd.key[i].kstate == PRESSED) cmd = 144;
                else if (kpd.key[i].kstate == RELEASED) cmd = 128;
                
                switch ( kpd.key[i].kchar ){
                  case '1': note = 0 + shift; break; //not used
                  case '2': note = 0 + shift; break; //not used
                  case '3': note = 0 + shift; break; //not used
                  case '4': note = 0 + shift; break; //not used
                  case '5': note = 0 + shift; break; //not used
                  case '6': note = 0 + shift; break; //not used
                  case '7': note = 0 + shift; break; //not used
                  case '8': note = 24 + shift; break;
                  
                  case 'q': note = 16 + shift; break;
                  case 'w': note = 17 + shift; break;
                  case 'e': note = 18 + shift; break;
                  case 'r': note = 19 + shift; break;
                  case 't': note = 20 + shift; break;
                  case 'y': note = 21 + shift; break;
                  case 'u': note = 22 + shift; break;
                  case 'i': note = 23 + shift; break; //ok
                  
                  case 'a': note = 8 + shift; break;
                  case 's': note = 9 + shift; break;
                  case 'd': note = 10 + shift; break;
                  case 'f': note = 11 + shift; break;
                  case 'g': note = 12 + shift; break;
                  case 'h': note = 13 + shift; break;
                  case 'j': note = 14 + shift; break;
                  case 'k': note = 15 + shift; break; //ok
                  
                  case 'z': note = 0 + shift; break;
                  case 'x': note = 1 + shift; break;
                  case 'c': note = 2 + shift; break;
                  case 'v': note = 3 + shift; break;
                  case 'b': note = 4 + shift; break;
                  case 'n': note = 5 + shift; break;
                  case 'm': note = 6 + shift; break;
                  case ',': note = 7 + shift;         //ok
                }

                if ( kpd.key[i].kstate == PRESSED || kpd.key[i].kstate == RELEASED ){  // avoids HOLD and IDLE state wich produces additional message. TREBUIE SA INCLUDA "SWITCH" PENTRU A ECONOMISI RESURSE
                 if (debug) {                                 
                  Serial.print(cmd);
                  Serial.print("__");
                  Serial.println(note);
                 }
                 else {
                  Serial.write(cmd);
                  Serial.write(note);
                  Serial.write(velocity);
                 }
               }
            }
        }
    }
}  // End loop
 //new
#include <Keypad.h>

bool debug = true;

const byte ROWS = 4;
const byte COLS = 8;
char keys[ROWS][COLS] = {
{(char)66,(char)66,(char)66,(char)66,(char)66,(char)66,(char)66,(char)24},
{(char)16,(char)17,(char)18,(char)19,(char)20,(char)21,(char)22,(char)23},
{(char) 8,(char) 9,(char)10,(char)11,(char)12,(char)13,(char)14,(char)15},
{(char) 0,(char) 1,(char) 2,(char) 3,(char) 4,(char) 5,(char) 6,(char) 7}
};

byte rowPins[ROWS] = {10, 11, 12, 13}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {2, 3, 4, 5, 6, 7, 8, 9}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long loopCount;
unsigned long startTime;

byte cmd, note = 55, velocity = 100;
byte shift = 41;

void setup() {
    Serial.begin(38400);
    kpd.setDebounceTime(30);
}

void loop() {
    // 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.        
              if ( kpd.key[i].kstate == PRESSED || kpd.key[i].kstate == RELEASED ){   // avoids HOLD and IDLE state wich produces additional message. TREBUIE SA INCLUDA "SWITCH" PENTRU A ECONOMISI RESURSE               

                if (kpd.key[i].kstate == PRESSED) cmd = 144;
                else if (kpd.key[i].kstate == RELEASED) cmd = 128;
                
                note = shift + (byte) kpd.key[i].kchar;  
                           
                if (debug) {                                 
                  Serial.print(cmd);
                  Serial.print("__");
                  Serial.println(note);
                }
                else {
                  Serial.write(cmd);
                  Serial.write(note);
                  Serial.write(velocity);
                }                                   
              }
            }
        }
    }
}  // End loop

Your two definitions of char keys are just the same but the first one is easier.

I think your problem is that you are using a libiary that is hiding what is happening from you. Unfortunately I am not that familiar withe that libiary either.

I just had a revelation!!! I believe the problem was because i was using "(char) 0" which in ASCII code is "Null char". Any other character worked fine.

And probably i'll go back to the first declaration method after i finish the program to make it a little bit faster.