MIDI Piano

I am currently attempting to create a midi controller with my Arduino UNO which will take inputs from push buttons beneath giant piano keys and play different notes for each button. I am using a usb-midi converter which i purchased on Amazon to send the serial data to the computer, and using Ableton Live to interpret the data and play notes. Currently I am running into issues, and I am not sure whether they are due to my code, or the Live software. Live currently has trouble recognizing the signals, and often will play two notes at once when I press only one button. Another problem I run into is that the notes will often transfer both when the button is pushed and released. Ideally it will only transmit when the button is initially pushed. Can anybody suggest any improvements to my code, or perhaps suggest a good alternative to Ableton live? Below is my code, and attached is a screenshot of the midi signals my computer receives when each key is pressed.

const int switchPin = 5;  
const int switchPin1 = 9;
const int switchPin2 = 10;
const int LEDpin = 13;    

 // Variables: 
 void setup() {
     pinMode(switchPin, INPUT);
   pinMode(switchPin1, INPUT);
   pinMode(LEDpin, OUTPUT);
   
   Serial.begin(31250);
 }
int keyPush = 0;
int keyPush1 = 0;
int keyPush2 = 0;
int loopMaxOutput = 50;
 void loop() {
     //KEY 1  
   //If key is pressed, play note once
       if (digitalRead(switchPin) == 1 && keyPush == 0) {   
   //Loop sends the noteOn function multiple times. When sent only once, the computer did not register it.
           for(int i = 0; i < loopMaxOutput; i++)
           {
             noteOn(0x90, 40, 0x60);
           }
         keyPush = 1;
      }   
     //KEY 2 
      if (digitalRead(switchPin1) == 1 && keyPush1 == 0) {
      
        for(int i = 0; i < loopMaxOutput; i++)
        {
           noteOn(0x90, 50, 0x60);
        }
         keyPush1 = 1;
      }  
      //KEY 3
      if (digitalRead(switchPin2) == 1 && keyPush2 == 0) {

        for(int i = 0; i < loopMaxOutput; i++)
        { 
            noteOn(0x90, 60, 0x60);
         }
           keyPush2 = 1;
      }
      if(digitalRead(switchPin) == 0 && keyPush == 1) {
         keyPush = 0;
      }        
      if(digitalRead(switchPin1) == 0 && keyPush1 == 1) {
         keyPush1 = 0;
      }        
      if(digitalRead(switchPin2) == 0 && keyPush2 == 1) {
         keyPush2 = 0;
      }
   }
   void noteOn(byte cmd, byte data1, byte  data2) {
   Serial.write(cmd);
   Serial.write(data1);
   Serial.write(data2);
 }

Moderator edit: [code][/code] tags added. (Nick Gammon)

How have you wired up the USB / MIDI interface to the arduino? Have you got a proper interface or is it just the rubbish resistor one? You should send the note only once, don't try and compensate for poor intermittent hardware with software, it won't work. How have you wired up your switches? Are you using external pull up resistors?

You need to add a bit of switch debounce to the sketch. A 20mS delay after a key down or up is detected should do it.

int loopMaxOutput = 50;
 void loop() {
 ...
           for(int i = 0; i < loopMaxOutput; i++)
           {
             noteOn(0x90, 40, 0x60);
           }

Don’t do this. You don’t need to send “note on” 50 times. If it doesn’t work the first time stop and work out why.

Besides, 0x90 is “note on” so sending “noteOn (0x90” seems to me to be redundant. Maybe name the function “midiCommand” or something like that.

I am currently using the resistor method I believe. Here is an image of my current setup. What would you recommend to fix this? http://imgur.com/gg6IS

And thank you for the debounce tip!.

Those micro switches do bounce a lot.

You have 3 wires going to the MIDI socket, that is wrong but might not be doing any harm.

You only have a crappy MIDI interface use a proper one with a transistor that can supply the correct ammount of current. That should improve communications between he arduino and the computer.