Loading...
Pages: [1]   Go Down
Author Topic: MIDI Piano  (Read 726 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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)
« Last Edit: April 22, 2012, 04:07:00 pm by Nick Gammon » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 277
Posts: 25493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 277
Posts: 25493
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Print
 
Jump to: