MIDI on Arduino Micro doesn't work...

Hey guys,

I'm trying to use an Arduino Micro to send out Midi notes but it doesn't work. I tried that on an Arduino Uno and it worked fine...
Here's the code I'm using:

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPinPanic = 4;
const int LedPin1 = 5;
const int LedPin2 = 6;
const int LedPinPanic = 7;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonStatePanic = 0;

int buttonLast1 = 0;
int buttonLast2 = 0;
int buttonLastPanic = 0;


// Midi Note (0x00 = C0, 0x01 = C#0)
int note = 0x00;

void setup() {
  Serial.begin(31250);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPinPanic, INPUT);
  pinMode(LedPin1, OUTPUT);
  pinMode(LedPin2, OUTPUT);
  pinMode(LedPinPanic, OUTPUT);
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonStatePanic = digitalRead(buttonPinPanic);

  if (buttonState1 != buttonLast1){
    if (buttonState1 == HIGH) {
      note = 0x3A;
      noteOn(0x90, note, 0x45);
      digitalWrite (5, HIGH);
      delay(100);
      digitalWrite (5, LOW);
    }
    else {
      note = 0x3A;
      noteOn(0x90, note, 0x00);
      delay(10);
    }
  }
  
   if (buttonState2 != buttonLast2){
    if (buttonState2 == HIGH) {
      note = 0x10;
      noteOn(0x90, note, 0x45);
      digitalWrite (6, HIGH);
      delay(100);
      digitalWrite (6, LOW);
    }
    else {
      note = 0x10;
      noteOn(0x90, note, 0x00);
      delay(10);
    }
  }
  if (buttonStatePanic != buttonLastPanic){
    if (buttonStatePanic == HIGH) {
      note = 0x20;
      noteOn(0x90, note, 0x45);
      digitalWrite (7, HIGH);
      delay(100);
      digitalWrite (7, LOW);
    }
    else {
      note = 0x20;
      noteOn(0x90, note, 0x00);
      delay(10);
    }
  }
  buttonLast1 = buttonState1;
  buttonLast2 = buttonState2;
  buttonLastPanic = buttonStatePanic;
}
 

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

I'm using two buttons for Midi notes and one as a Midi Panic Button. Each button also has an LED which will turn on for 100ms when I push it.

After a bit of research I found out that I have to use the Serial1 protocoll for midi on the Micro/Leonardo, so I changed the code to this:

/
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPinPanic = 4;
const int LedPin1 = 5;
const int LedPin2 = 6;
const int LedPinPanic = 7;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonStatePanic = 0;

int buttonLast1 = 0;
int buttonLast2 = 0;
int buttonLastPanic = 0;


// Midi Note (0x00 = C0, 0x01 = C#0)
int note = 0x00;

void setup() {
  Serial1.begin(31250);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPinPanic, INPUT);
  pinMode(LedPin1, OUTPUT);
  pinMode(LedPin2, OUTPUT);
  pinMode(LedPinPanic, OUTPUT);
}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonStatePanic = digitalRead(buttonPinPanic);

  if (buttonState1 != buttonLast1){
    if (buttonState1 == HIGH) {
      note = 0x3A;
      noteOn(0x90, note, 0x45);
      digitalWrite (5, HIGH);
      delay(100);
      digitalWrite (5, LOW);
    }
    else {
      note = 0x3A;
      noteOn(0x90, note, 0x00);
      delay(10);
    }
  }
  
   if (buttonState2 != buttonLast2){
    if (buttonState2 == HIGH) {
      note = 0x10;
      noteOn(0x90, note, 0x45);
      digitalWrite (6, HIGH);
      delay(100);
      digitalWrite (6, LOW);
    }
    else {
      note = 0x10;
      noteOn(0x90, note, 0x00);
      delay(10);
    }
  }
  if (buttonStatePanic != buttonLastPanic){
    if (buttonStatePanic == HIGH) {
      note = 0x20;
      noteOn(0x90, note, 0x45);
      digitalWrite (7, HIGH);
      delay(100);
      digitalWrite (7, LOW);
    }
    else {
      note = 0x20;
      noteOn(0x90, note, 0x00);
      delay(10);
    }
  }
  buttonLast1 = buttonState1;
  buttonLast2 = buttonState2;
  buttonLastPanic = buttonStatePanic;
}
 

void noteOn(int cmd, int pitch, int velocity) {
  Serial1.write(cmd);
  Serial1.write(pitch);
  Serial1.write(velocity);
}

But it is still not working... Can anybody help me?

Cheers
Malte

On the Leonardo examples they put the following code in setup():

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

Perhaps, even though you don't use Serial, if you put that at the top of setup() it will help. It's one of the other few differences between Leonardo and UNO.

Thanks John!
Sorry for the late reply, had a lot of other stuff to do.
I tried it, but it still doesn't work. I can't even get the MIDI example to work with the micro...
Any other ideas?

show us your schematic

On the Leonardo and Micro you need to use Serial1 (hardware serial) for MIDI I/O. That goes through the D0,D1 serial pins. "Serial" is the built in USB-to-Serial.