Reading musical keyboard keys.

Hey. I have a keyboard that is connected like a keypad. I have 5 rows and 12 columns. I have those wires hooked up to my Arduino MEGA like so: odd pins from 31 to 53 are the columns connections and the even pins from 44 to 52 are connected as rows. I need somehow read, which key is pressed. I tried keypad library, but it did not work. I was using code down bellow, but the key numbers were wrong and some of them had the same number.

/*this keypad tutorial number one by omar tarek 29-06-2014 */
byte h=0,v=0;    //variables used in for loops
const unsigned long period=50;  //little period used to prevent error
unsigned long kdelay=0;        // variable used in non-blocking delay 
const byte rows=5;             //number of rows of keypad
const byte columns=12;            //number of columnss of keypad
const byte Output[rows]={44,46,48,50,52}; //array of pins used as output for rows of keypad
const byte Input[columns]={31,33,35,37,39,41,43,45,47,49,51,53}; //array of pins used as input for columnss of keypad
byte keypad() // function used to detect which button is used 
{
 static bool no_press_flag=0;  //static flag used to ensure no button is pressed 
  for(byte x=0;x<columns;x++)   // for loop used to read all inputs of keypad to ensure no button is pressed 
  {
     if (digitalRead(Input[x])==HIGH);   //read evry input if high continue else break;
     else
     break;
     if(x==(columns-1))   //if no button is pressed 
     {
      no_press_flag=1;
      h=0;
      v=0;
     }
  }
  if(no_press_flag==1) //if no button is pressed 
  {
    for(byte r=0;r<rows;r++) //for loop used to make all output as low
    digitalWrite(Output[r],LOW);
    for(h=0;h<columns;h++)  // for loop to check if one of inputs is low
    {
      if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it) continue
      continue;
      else    //if one of inputs is low
      {
          for (v=0;v<rows;v++)   //for loop used to specify the number of row
          {
          digitalWrite(Output[v],HIGH);   //make specified output as HIGH
          if(digitalRead(Input[h])==HIGH)  //if the input that selected from first sor loop is change to high
          {
            no_press_flag=0;                //reset the no press flag;
            for(byte w=0;w<rows;w++) // make all outputs as low
            digitalWrite(Output[w],LOW);
            return v*4+h;  //return number of button 
            
          }
          }
      }
    }
  }
 return 50;
}
void setup() 
{
  for(byte i=0;i<rows;i++)  //for loop used to make pin mode of outputs as output
  {
  pinMode(Output[i],OUTPUT);
  }
  for(byte s=0;s<columns;s++)  //for loop used to makk pin mode of inputs as inputpullup
  {
    pinMode(Input[s],INPUT_PULLUP);
  }
  Serial.begin(9600); //to use serial monitor we set the buad rate
}
void loop()
{
  if(millis()-kdelay>period) //used to make non-blocking delay
  {
    kdelay=millis(); 
  
 
switch (keypad())  //switch used to specify which button
   {
            case 0:
            Serial.println("1");
       break;  
            case 1:
            Serial.println("2");
       break;
            case 2:
            Serial.println("3");
       break;
            case 3:
            Serial.println("4");
       break;
            case 4:
            Serial.println("5");
       break;
            case 5:
            Serial.println("6");
       break;
            case 6:
            Serial.println("7");
       break;
            case 7:
            Serial.println("8");
       break;
            case 8:
            Serial.println("9");
       break;
            case 9:
            Serial.println("10");
       break;
            case 10:
            Serial.println("11");
       break;
            case 11:
            Serial.println("12");
       break;
            case 12:
            Serial.println("13");
       break;
            case 13:
            Serial.println("14");
       break;
            case 14:
            Serial.println("15");
       break;
            case 15:
            Serial.println("16");
       break;
            case 16:
            Serial.println("17");
       break;
            case 17:
            Serial.println("18");
       break;  
            case 18:
            Serial.println("19");
       break;
            case 19:
            Serial.println("20");
       break;
            case 20:
            Serial.println("21");
       break;
            case 21:
            Serial.println("22");
       break;
            case 22:
            Serial.println("23");
       break;
            case 23:
            Serial.println("24");
       break;
            case 24:
            Serial.println("25");
       break;
            case 25:
            Serial.println("26");
       break;
            case 26:
            Serial.println("27");
       break;
            case 27:
            Serial.println("28");
       break;
            case 28:
            Serial.println("29");
       break;
            case 29:
            Serial.println("30");
       break;
            case 30:
            Serial.println("31");
       break;
            case 31:
            Serial.println("32");
       break;
            case 32:
            Serial.println("33");
       break;
            case 33:
            Serial.println("34");
       break;
            case 34:
            Serial.println("35");
       break;  
            case 35:
            Serial.println("36");
       break;
            case 36:
            Serial.println("37");
       break;
            case 37:
            Serial.println("38");
       break;
            case 38:
            Serial.println("39");
       break;
            case 39:
            Serial.println("40");
       break;
            case 40:
            Serial.println("41");
       break;
            case 41:
            Serial.println("42");
       break;
            case 42:
            Serial.println("43");
       break;
            case 43:
            Serial.println("44");
       break;
            case 44:
            Serial.println("45");
       break;
            case 45:
            Serial.println("46");
       break;
            case 46:
            Serial.println("47");
       break;
            case 47:
            Serial.println("48");
       break;
            case 48:
            Serial.println("49");
       break;
       
       default:
            
       break;
}
}
}

And here is the output from the serial port after pressing all those keys from right to left: 6
8
8
18
12
11
9
10
9
8
7
6
5
4
3
2
16
15
14
13
12
11
10
9
8
7
6
20
20
19
17
16
15
14
13
12
11
10
24
23
22
21
20
19
18
17
16
15
14

Thank you for any suggestions :slight_smile:

Does it work in the tutorial? If so, you probably have a wiring error.

This tutorial was only for 4 x 3 keypad, so I extended it. But todays morning I solved it by using this code

const int row_pin[] = {46,48,50,52}; 
const int col_pin[] = {47,49,51,53}; // defining row and column pins as integer arrays

const int rows = 4, cols = 4; //defining the multi dimensional array size constants 

const char key[rows][cols] = {               // defining characters //for keystrokes in Multidimensional Array
  {
    '1','2','3','z'   }
  ,   
  {
    '4','5','6','v'   }
  ,
  {
    '7','8','9','u'   }
  ,
  {
    '*','0','#','s'   }  
};

void setup(){

  Serial.begin(9600); //begin the serial communication

  for(int i = 0; i<rows; i++)
  {
    pinMode(row_pin[i],OUTPUT); //Configuring row_pins as Output Pins
    digitalWrite(row_pin[i],HIGH);//write HIGH to all row pins

    if(i<cols)//we only have 3 columns
    {
      pinMode(col_pin[i],INPUT_PULLUP);//configure column pin as Input and activate internal //Pullup resistor
    }//end of if

  }//end of for loop

}//end of setup


void loop(){
  char key = read_key();
  if(key !='\n'){
    Serial.println(key);
    delay(100);
  }
}//end of loop

char read_key(){
  for(int row = 0;row < rows;row++)
  {
    for(int row_write = 0;row_write < rows; row_write++)
    {
    digitalWrite(row_pin[row_write],HIGH);
    }
 
    digitalWrite(row_pin[row],LOW);
    //Serial.println(row_pin[row]);

    for(int col = 0;col<cols;col++)
    {
      int col_state = digitalRead(col_pin[col]);
      if(col_state == LOW)
      {
        return key[row][col];
      }//end of if 
    }//end of col for loop
  }//end of row for loop
  return '\n';
}//end of read_key

Its optimized a little bit and works just fine. Thank you for your help :slight_smile: