Go Down

Topic: The project is done (Old 49 keys casio) to midi using Arduino MEGA (Read 1 time) previous topic - next topic

Oday_Alasali

OMG dude I guess that I found out this riddle and I'm solving it, will reply when done.

Oday_Alasali

I'm done dude at last  ;D I've spent a whole 9 hours to find this out and now I'm finally done 100%

just I want a favor from you to check if this code is good in the long term because I don't know how to check that.

and the last thing but its doesnt matter if its unsolvable (there is a little noise when a tone is played but its not affecting the sound) but will be awesome if its solved .


Code: [Select]

#include <MIDI.h>
#include <Keypad.h>

const byte ROWS = 6; //six rows
const byte COLS = 9; //nine columns
 
char keys[COLS][ROWS] = {
  {48, 54, 60, 66, 72, 78},
  {84, 90, 96, 49, 55, 61},
  {67, 73, 79, 85, 91, 96},
  {50, 56, 62, 68, 74, 80},
  {86, 92, 97, 51, 57, 63},
  {69, 75, 81, 87, 93, 98},
  {52, 58, 64, 70, 76, 82},
  {88, 94, 99, 53, 59, 65},
  {71, 77, 83, 89, 95, 101}
};
 
byte rowPins[ROWS] = {26, 28, 30, 32, 34, 36,}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {A0, A1, A2, A3, A4, A5, A6, A7, A8}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte keyToMIDI[] = { 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,
                     66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,
                     84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,
                     102,103,104,105,106,107,108};
byte pressed = 32;
byte chanel = 0; // MIDI channel to use

void setup() {
 Serial.begin(115200); // set this the same as Hairless
}


void loop() {
 // Fills kpd.key[49] 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.
     {
       pressed = keyToMIDI[kpd.key[i].kchar - 36];
       switch (kpd.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
         case PRESSED:
           sendMIDI(chanel | 0x90, pressed, 100);
           break;
          case RELEASED:
           sendMIDI(chanel | 0x80, pressed, 64);
           break;
       }
     }
   }
 }
}  // End loop

void sendMIDI(byte type, byte note, byte velocity){
  Serial.write(type);
  Serial.write(note & 0x7F);
  Serial.write(velocity);
}

Grumpy_Mike

Well first off many congratulations on what you have achieved so far. You can remove that KeyToMIDI array as it dosn't do much, it is just a fancy way of adding an offset. This is your code with it removed:-
Code: [Select]
#include <MIDI.h>
#include <Keypad.h>

const byte ROWS = 6; //six rows
const byte COLS = 9; //nine columns
 
char keys[COLS][ROWS] = {
  {48, 54, 60, 66, 72, 78},
  {84, 90, 96, 49, 55, 61},
  {67, 73, 79, 85, 91, 96},
  {50, 56, 62, 68, 74, 80},
  {86, 92, 97, 51, 57, 63},
  {69, 75, 81, 87, 93, 98},
  {52, 58, 64, 70, 76, 82},
  {88, 94, 99, 53, 59, 65},
  {71, 77, 83, 89, 95, 101}
};
 
byte rowPins[ROWS] = {26, 28, 30, 32, 34, 36,}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {A0, A1, A2, A3, A4, A5, A6, A7, A8}; //connect to the column pinouts of the kpd

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

byte pressed = 32;
byte chanel = 0; // MIDI channel to use

void setup() {
 Serial.begin(115200); // set this the same as Hairless
}


void loop() {
 // Fills kpd.key[49] 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.
     {
       pressed = kpd.key[i].kchar + 12;
       switch (kpd.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
         case PRESSED:
           sendMIDI(chanel | 0x90, pressed, 100);
           break;
          case RELEASED:
           sendMIDI(chanel | 0x80, pressed, 64);
           break;
       }
     }
   }
 }
}  // End loop

void sendMIDI(byte type, byte note, byte velocity){
  Serial.write(type);
  Serial.write(note & 0x7F);
  Serial.write(velocity);
}


However, I am not seeing any MIDI note off messages on my lash up of what you have. You will not notice the lack of note off messages if your sounds are percussive, like a bell, or glockenspiel and so on. But if you have a sound in the Synth Pad range sounds 89 to 96 and many others the note will hang, that means it will not stop and keep playing even when the key is released.

Can you please:-
1) Test this code and see if it does what your original one does.
2) Check that you are getting the MIDI note off messages on your system by trying other sounds or running some sort of MIDI monitor to see what hairless is putting out.

Thanks

Oday_Alasali

I've tried this code and gone along with all sounds piano \ synths ....everything and its doing the job perfectly with ons and offs.

but as I said there is a little background noise happening when a note is playing, even thou its not harmful but would be great to find out why. And many thanks for handling me for this whole while because you were giving me clues.

Grumpy_Mike

Quote
but as I said there is a little background noise happening when a note is playing,
Unless you can see something on a MIDI monitor connected after hairless then I don't think it has anything to do with the keyboard. It is possibly something to do with the MIDI sound generator or your speaker system.

Oday_Alasali

Well I don't know what was happening but I was using the arduino through the front usb port I moved it to the rear port and that solved it.


Now I'm totally done with this and I hope that this whole post would help someone in the future

Many Thanks.

Go Up