works: MIDI-IN: code + schematics

Is there any chance to get MIDI data from the USB directly in osx? thanks!

thank you very much for this! this works perfectly...

if you want to make a MIDI-in shield for the arduino based on this schematic you can get layout, parts list etc. there:
http://www.instructables.com/id/Arduino_MIDI_in_shield/

Thanks Carkat, thats the first MidiIn schematic that worked for me! Using a 4N27. :slight_smile:

Firstly, I'm trying to put together my own MIDI instrument, and found this code indispensable -- thanks Kuk!

But, as a programming n00b, I don't really understand the why 'action' is necessary -- would anyone mind explaining that to me, please. :slight_smile:

Also, just to check -- in the initial code, the blink subroutine (was that the correct term?) isn't used is it?

Again, thanks for the very helpful code.

I got a Korg MicroKorg for xmas a while back only to find that it didn't have a sequencer built in :(. So, I've decided I'm going to make an Arduino do some MIDI IN/OUT, and act as a cool sequencer accessory. I'm using a small backlit serial LCD, some multi-color LED bar graph displays, a few HC595's, a large pot, and (possibly) 2 Arduinos. I figure I'll have two Arduinos to make the setup easier to program (master-slave config). I hope to get it to the point where the Arduino(s) will get all key, knob, and other MIDI status messages from the microKorg, and save that info in each step along with the program number (for each unique program sound). It'll then be able to play it back in sequence at a variable tempo. I'll update here if you'd all like! :slight_smile:

Hello!

I'm on a MIDI project and this topic was very useful, thanks!

I'm playing around with this code and don't get it working.
All I want is to have the led on when a note is on and the led off when the note is off (no polyphony).
Here is the code:

//variables setup
byte incomingByte;


int statusLed = 13;   // select the pin for the LED

//setup: declaring iputs and outputs and begin serial
void setup() {
  pinMode(statusLed,OUTPUT);   // declare the LED's pin as output
  Serial.begin(31250);        //start serial with midi baudrate 31250 or 38400 for debugging
  digitalWrite(statusLed,LOW);  
}


void loop () {

 if (Serial.available() > 0) {

    incomingByte = Serial.read();
    // wait for as status-byte, channel 1, note on or off

    if (incomingByte== 144){ // note on
      digitalWrite(statusLed, HIGH);
    }
    
    else if (incomingByte== 128){ // note off
      digitalWrite(statusLed, LOW);
    }
 }

}

The problem is that my led goes on, but never off!
Any idea?

Thanks :slight_smile:

hi tep,
probably your keyboard sends "Note ON w ZERO velocity" instead of "NOTE off". the ones i tested do this.

best, kuk

Thank you kuk, you're probably right.
I tested this

void loop () {
 if (Serial.available() > 0) {

    incomingByte = Serial.read();
    // wait for as status-byte, channel 1, note on or off
    if (incomingByte== 144){ // note on
      digitalWrite(statusLed, HIGH);
      note=incomingByte;
      velocity=incomingByte;
      if (velocity==0){ // note off
      digitalWrite(statusLed, LOW);
      }
    }
     
 }
}

and still no result.
But it's perhaps not the best way to get the velocity ?

:wink: tep

first, your code fragment does not update the value of "incomingByte"... you have to re-read form the serial buffer, like in the first line of your code, to get the next received byte.

plus:
ok, i'm really not into midi right now. but if i remember correctly there was something else to mention:

AFTERTOUCH messages are like NOTE ON (OFF) messages without explictly mentioning which note to play or stop. in that case you have to ON/OFF the last note which was explicitly sent. the code i posted at the beginning of this thread does not reflect this, because my (midiman oxygen) keyboard did not seem to use aftertouch.

keep me/us updated please and good luck.

//kuk

Ok, you're right. I'll look that later.
My project is not pure notes midi I/O, but I want to be sure to understand the mechanism of reading the serial input.
Thanks !

I just got MIDI-IN working with the CNY17-2 optocoupler. I used the MIDI library from the arduino playground to make the led on pin 13 flash on receiving a MIDI message. You can find the modified scheme below.

this will also work without the 100k resistor btw

Can anyone help me with the code for simply reading 3 (20,21,22) program change and 1 control change (36) and simply activate 1 led for each program change and 1 led for the control change?

Can anyone help me with the code for simply reading 3 (20,21,22) program change and 1 control change (36) and simply activate 1 led for each program change and 1 led for the control change?

The following might work for you.

#include <MIDI.h>

byte ledPC = 11;
byte ledCC = 12;
byte channel = 1;

void setup() {
  pinMode(ledPC, OUTPUT);
  pinMode(ledCC, OUTPUT);
  MIDI.begin();
  MIDI.useExternalPowerPin();
}

void loop() {
  if (MIDI.read(channel)) {
    if (MIDI.getType() == PC) {
      byte dat1 = MIDI.getData1();
      if (dat1 > 19 && dat1 < 23) {
        digitalWrite(ledPC, HIGH);
        delay(50);
      }
    }
    else if (MIDI.getType() == CC) {
      if (MIDI.getData1() == 36) {
        digitalWrite(ledCC, HIGH);
        delay(50);
      }
    }
  }
}

Edit: You might want to download the MIDI library from the playground first :wink:

Thank'you very mutch,this night I try all and reply this.

Giorgio

tetracon,
your code seems to work,but I need some things:

I need 3 separate leds for 3 program change,and when is active the program 20 is active the ONLY led1,when active the program 21 is ONLY active the led2,and obviousily the PC22 activate only led3.

When I'm using Control Change,I need to activate it with CC36,and DEACTIVATE with CC36 (or for example CC00)

Is complicated do this?
Thank's in advance,

Giorgio

Giorgio,

You might want to try adapting the code to your needs. If you find it hard to understand you might want to do some more research on programming.
Basically all you need to do is add some vars in the top part for the additional leds.

 if (dat1 > 19 && dat1 < 23) {
        digitalWrite(ledPC, HIGH);
        delay(50);
        digitalWrite(ledPC, LOW); // forgot this last time
      }

This part finds out what the PC value is, you might want to change this to more if statements so that you can turn on or turn off the led of your choice.

I am EXTREMELY frustrated! I have spend my ENTIRE Sunday debugging this, but I can't get it to work. I have all the connections correct, I'm pretty sure. I am using a split-end cable which goes right into the female jack on my microKorg synth. I am using a 4N28 opto-isolator. The 220ohm resistor connects to pin 2 on the other end of the cable (the circular, male metal connector end end). To help myself visualize, when looking at the end of the male MIDI jack, I number the pins:
2. 3. 4.

pin 4 on this cable connects to pin 2 of the opto. I am NOT using pin 3 (which is GND for the MIDI connector). I shouldn't be, correct? Anyways, with or without, it hasn't worked.

I read the details on using a different value resistor on pin 6 of the opto. For some reason, I SOMETIMES will get a '1' returned with Serial.available(), IF i mess with a potentiometer between 100Kohms and 200Kohms (usually ~170 does it), but it isn't reliable at all. I've got this all set up on a breadboard for now.

It is my understanding that this is supposed to connect to the jack on the MIDI device labeled "MIDI OUT", correct? I am also getting a little confused with perspectives here lol (flip-flopping cable/jack pins 4 & 5 to match male/female jacks/sockets). Have I possibly blown out the optoisolator? I don't see what I'm doing wrong! :cry:

FYI: I have been referring to the original schematic on page 1 of this topic.