How can I get midi to work with the Arduino Leonardo

Hello everyone!

I’ve been struggling with this for a couple of months now, and I’m wondering what could be the problem, and how could I solve it. I have the newest midi library!

#include <MIDI.h>
int potPin = A0;

int analogValue = 0;
int lastAnalogValue = 128;

void setup() {
  // put your setup code here, to run once:
  MIDI.begin(4);
      // 115200 hairless MIDI
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  int analogValue = analogRead(A0)/8;
  
  if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) {
        // value changed?
        if (analogValue != lastAnalogValue) {
          // send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
          // more info: http://arduinomidilib.sourceforge.net/a00001.html
          MIDI.sendControlChange(1, analogValue, 1);
          lastAnalogValue = analogValue;
        }
      }
}

and the error that i get is:

Arduino: 1.5.8 (Windows 7), Board: "Arduino Leonardo"

sketch_dec09b.ino: In function 'void setup()':
sketch_dec09b.ino:9:3: error: 'MIDI' was not declared in this scope
sketch_dec09b.ino: In function 'void loop()':
sketch_dec09b.ino:23:11: error: 'MIDI' was not declared in this scope
Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

What could be the problem? Please help!

Thank you again!
-Valentin

I have the newest midi library!

From where? installed where?

http://arduinomidilib.sourceforge.net/a00001.html

If this is where you got the library, you need to create an instance of the class.

I got it from here

"The default MIDI object has been removed, so you will need to create it using one of the following macros:
For straightforward compatibility with sketches written with v4.1 and older:

MIDI_CREATE_DEFAULT_INSTANCE();
.
.
.

(You probably want to use Serial1 on the Leonardo since Serial goes only to USB.)

Thanks John! But I would like some more help, because I don’t get any midi data, and I’ve changed my code a little

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

int potPin = A0;

int analogValue = 0;
int lastAnalogValue = 128;

void setup() {
  // put your setup code here, to run once:
  MIDI.begin(1);
      // 115200 hairless MIDI
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  int analogValue = analogRead(A0)/8;
  
  if ((analogValue-lastAnalogValue) > 1 || (analogValue-lastAnalogValue) < -1) {
        // value changed?
        if (analogValue != lastAnalogValue) {
          // send serial value (ControlNumber 1, ControlValue = analogValue, Channel 1)
          // more info: http://arduinomidilib.sourceforge.net/a00001.html
          MIDI.sendControlChange(1, analogValue, 1);
          lastAnalogValue = analogValue;
        }
      }
}

How is your MIDI network connected to your Arduino? What are you using to receive the control messages you are sending?

johnwasser:
How is your MIDI network connected to your Arduino? What are you using to receive the control messages you are sending?

Before writing your own code, get one of the examples supplied with the MIDI library (like MIDI_Basic_IO) to work with your MIDI hardware.

Hello again John! I am using hairless-midiserial to convert serial messages and loopMIDI to make a virtual MIDI port(I have also tried this with LoopBe1). I have it set up like this:
I have used this as a reference!