Sending Midi Out

I’m really struggling guys… I “think” I’ve got my code down. I’m trying to send midi notes via buttons, but with a midi jack attached to a midi to usb device, I get nothing even though my TX led is lighting up on my arduino… Can someone take a look at my code and tell me if there’s something wrong with it?

#include <SoftwareSerial.h>
SoftwareSerial midi(0,1); ///RX TX

int recButton = 2;
int stopButton = 3;
int modeButton = 4; 
int track1Button = 5;
int track2Button = 6;
int track3Button = 7;
int track4Button = 8;
int undoButton = 9; 
int clearButton = 10;
int mode1LED = 11;
int mode2LED = 12;
int logoLED = 13;

int toggle1 = 0;
int toggle2 = 0;
int toggle3 = 0;
int toggle4 = 0;
int toggle5 = 0;
int toggle6 = 0;
int toggle7 = 0;
int toggle8 = 0;
int toggle9 = 0;

int mode = 0;

void setup(){
  pinMode (logoLED, OUTPUT);
  digitalWrite(logoLED, HIGH);
  pinMode (clearButton, INPUT_PULLUP);
  pinMode (undoButton, INPUT_PULLUP);
  pinMode (recButton, INPUT_PULLUP);
  pinMode (stopButton, INPUT_PULLUP);
  pinMode (track1Button, INPUT_PULLUP);
  pinMode (track2Button, INPUT_PULLUP);
  pinMode (track3Button, INPUT_PULLUP);
  pinMode (track4Button, INPUT_PULLUP);
  pinMode (modeButton, INPUT_PULLUP);
  pinMode (undoButton, INPUT_PULLUP);
  pinMode (mode1LED, OUTPUT);
  pinMode (mode2LED, OUTPUT);
  digitalWrite(mode1LED, HIGH);
  digitalWrite(mode2LED, LOW);
  delay(250);
  digitalWrite(mode1LED, LOW);
  digitalWrite(mode2LED, HIGH);
  delay(250);
  digitalWrite(mode1LED, HIGH);
  digitalWrite(mode2LED, LOW);
  delay(250);
  digitalWrite(mode1LED, LOW);
  digitalWrite(mode2LED, HIGH);
  delay(250);
  digitalWrite(mode1LED, HIGH);
  digitalWrite(mode2LED, LOW);
  delay(250);
  digitalWrite(mode1LED, LOW);
  digitalWrite(mode2LED, LOW);
  delay(250);

  midi.begin(31250);
}

void loop(){
//record button
    if(digitalRead(recButton) == LOW && toggle1 == 0){
    toggle1 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x10);
      midi.write(0x45);
      //delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x35);
      midi.write(0x45);
      // delay(300);
    }
  }
  
  if(digitalRead(recButton) == HIGH && toggle1 == 1){
   toggle1 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x10);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x35);
      midi.write((byte)0x00);
      delay(300);
    }
  }

//stop button
  if(digitalRead(stopButton) == LOW && toggle2 == 0){
    toggle2 = 1; 
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x07);
      midi.write(0x45); 
      //delay(300); 
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x34);
      midi.write(0x45); 
      //delay(300); 
    }
  }


  if(digitalRead(stopButton) == HIGH && toggle2 == 1){
    toggle2 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x07);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x34);
      midi.write((byte)0x00);
      delay(300);

    }
  }
  
//mode button
  if (digitalRead(modeButton) == LOW && toggle3 == 0){
    toggle3 = 1; 
    if(mode == 0){
      
      midi.write(0x90);
      midi.write(0x17);
      midi.write(0x01);
      // delay(300);
    }

    if(mode == 1){

      midi.write(0x90);
      midi.write(0x30);
      midi.write(0x01);
      //  delay(300);
    }
  } 

  if (digitalRead(modeButton) == HIGH && toggle3 == 1){
    toggle3 = 0;
    if(mode == 0){
      mode = 1;
      midi.write(0x90);
      midi.write(0x17);
      midi.write((byte)0x00);  
      digitalWrite(mode1LED, HIGH);
      digitalWrite(mode2LED, LOW);
      delay(300);
    }
    else if(mode == 1){
      mode = 0;
      midi.write(0x90);
      midi.write(0x30);
      midi.write((byte)0x00);  
      digitalWrite(mode1LED, LOW);
      digitalWrite(mode2LED, HIGH);
      delay(300);
    }
  }
  
//track 1 button
  if (digitalRead(track1Button) == LOW && toggle4 == 0){
    toggle4 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x14);
      midi.write(0x01);
      // delay(300);
    } 
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x33);
      midi.write(0x01);
      //  delay(300);
    }
  } 

  if (digitalRead(track1Button) == HIGH && toggle4 == 1){
    toggle4 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x14);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x33);
      midi.write((byte)0x00); 
      delay(300);
    }
  }
  
//track 2 button
  if (digitalRead(track2Button) == LOW && toggle5 == 0){
    toggle5 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x15);
      midi.write(0x01);
      // delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x32);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(track2Button) == HIGH && toggle5 == 1){
    toggle5 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x15);
      midi.write((byte)0x00);  
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x32);
      midi.write((byte)0x00);  
      delay(300);
    }
  }
  
//track 3 button
  if (digitalRead(track3Button) == LOW && toggle6 == 0){
    toggle6 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x16);
      midi.write(0x01);
      //  delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x31);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(track3Button) == HIGH && toggle6 == 1){
    toggle6 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x16);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x31);
      midi.write((byte)0x00); 
      delay(300);
    }
  }
  
//track 4 button
    if (digitalRead(track4Button) == LOW && toggle7 == 0){
    toggle7 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x18);
      midi.write(0x01);
      //  delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x29);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(track4Button) == HIGH && toggle7 == 1){
    toggle7 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x18);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x29);
      midi.write((byte)0x00); 
      delay(300);
    }
  }
  
//undo button
  if (digitalRead(undoButton) == LOW && toggle8 == 0){
    toggle8 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x41);
      midi.write(0x01);
      // delay(300);
    }

    if(mode == 1){
      midi.write(0x90);
      midi.write(0x36);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(undoButton) == HIGH && toggle8 == 1){
    toggle8 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x41);
      midi.write((byte)0x00);  
      delay(300);
    }

    if(mode == 1){
      midi.write(0x90);
      midi.write(0x36);
      midi.write((byte)0x00);  
      delay(300);
    }
  }
  
//clear button
  if(digitalRead(clearButton) == LOW && toggle9 == 0){
    toggle9 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x01);
      midi.write(0x45);
      //delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x37);
      midi.write(0x45);
      //delay(300);  
    }
  }

  if(digitalRead(clearButton) == HIGH && toggle9 == 1){ 
    toggle9 = 0;

    if(mode == 0){
      midi.write(0x90);
      midi.write(0x01);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x37);
      midi.write((byte)0x00);
      delay(300);
    }
  }
}

MIDI requires a specific hardware interface. You can't just wire the Arduino's pins direct to a MIDI device.

Can you show a schematic of what you have? It's difficult to understand from that short description what is connected to what.

At the present time I don't have a way to show a schematic, but I'll describe it the best I can. I'm using an Arduino Mega 2560. I have a midi jack connected as follows: Pin 4 = 5v (with a 330 ohm resistor), Pin 2 = Ground, and Pin 5 = Digital Pin 1 on my Arduino (labeled as the TX Pin). That jack has a midi cable plugged into it which is running to a USB Midi Interface. That interface is confirmed as working by other devices. I also have a button wired to a breadboard going to Digital Pin 2 (and ground) which SHOULD be sending a midi note on press, but this doesn't seem to be working. The TX LED on my Arduino lights up when I press the button, but my computer is not recognizing the input. I'm wondering if it's a problem with my Code? I did not use the Arduino Midi Library as the code was adapted from Ed Sonnit's code for his looper which used SoftwareSerial instead. I'm still new to this so I don't know if I'm headed in the right direction.

Hey mate,

I'm brand new here too, however, my current project uses MIDI and I have it correctly controlling both stage keyboard and synth.

When trying to troubleshoot an issue, it's normally easier to take a minimalistic approach and reduce the number of variables that may be causing issues. Have you tried making a new sketch that simply outputs a single MIDI on signal?

For example (untested):

void setup() {
    Serial.begin(31250);

    sendMIDI(0x90, 0x37, 0x7F);
}

void sendMIDI(int cmd, int pitch, int velocity)
{
    Serial.write(cmd);
    Serial.write(pitch);
    Serial.write(velocity);
}

(I think creating a sendMIDI() function in your case would be beneficial as well, as you appear to be repeating that code a lot)

Also, is there any reason you are using SoftwareSerial? It was my understanding that that library is for using other pins for serial, however, since you're using the dedicated serial pin you may as well use regular Serial functionality.

aaronpmusic: At the present time I don't have a way to show a schematic,

A pencil sketch photographed with a phone is better than a wordy description.

You can try Fritzing but please only show the schematic view, not the breadboard view.