works: MIDI-IN: code + schematics

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.

quackmaster7000, I didn't follow everything, but don't stay frustated!
How to number de pins:
Myself I made a modification to the schematic that is posted by kuk:

I didn't connect the 100kOhm resistor to anything and it worked, perhaps your solution too?
I connected the 4th pin of the DIN connector to the 220Ohm (and the 5th is the other).

Good luck! :wink:

great instructable ! i am using your code now to drive relays via transistors so that i can program beats using old radios as the sound source (turned on and off by eight relays) I have come across an issue though that i dont understand at all,

When i plug the midi input straight into the midi out of my korg ms2000 everything works fine and dandy, however if i control the arduino with my edirol ua25 sound cards midi out the arduino will not handle two simultaneous notes. If i dont send a note of for the first note, i have to wait approx 1 sec until i can press a new note that it will handle, this happens with my edirol PCR-500 midi keyboard too. What is going on ? :-?

HOORAY! It works! ;DI finally got it to work lol. I DIDN'T need the 100Kohm resistor, and I had a few things mixed up it seems. Now I just have to debug all these MIDI messages, and figure out a way to receive, store, and retrieve them quickly and efficiently...

been trying to get this schematic alongside a few other MIDI IN ones to work but so far no results. Apologies if this is a stupid question but am I right in thinking that with this schematic pins 6 and 4 go to GND? and pin 5 goes first to PIN 0 and then to +5v (via 3.3k resistor)?

I have been trying to find a suitable solution and have also come across this in the process:

http://www.spikenzielabs.com/SpikenzieLabs/Serial_MIDI.html

which is a Serial to Midi converter - haven't fully tested yet but could be a solution also - however this requires the MIDI data to be sent over USB and that also means a computer, I am interested in that to an extent but I would also like to be able to build standalone units that can be controlled by simple (and cheap) midi keyboards.

Kazimier,

It's not very clear what you wish to do with this, but from what I have gotten out of it, you want to make a MIDI Controller/Sampler that will receive MIDI messages from a MIDI keyboard and then produce sound assigned to each key?

I didn't use the 100K resistor; I didn't even connect pin 6 to anything and it works. If you have trouble reading schematics, google "schematic symbols"; there's a huge wealth of information out there on that.

appologies for not being very clear - I want to build a device which would allow me to control a number of LED's connected to the arduino board through a MIDI controller, so essentially MIDI going into the board and then outputting serial data to control the individual I/O.

I will give it a go today without pin 6 connected, I guess the tricky thing I am finding and I am sure this is the same for many of us here, is troubleshooting, i.e. finding ways to troubleshoot the problem and narrow down the possible causes of it when nothing stands out.

Kazimier,

You may want to start a new thread for that topic.

As for myself, I am trying to figure out how to deal with serial data. I have found out that MIDI data is EXTREMELY time-critical. How can I deal with simultaneously getting MIDI data, sending MIDI data, and communicating to a serial LCD?? These all require serial (to be exact, I'd need 2 dedicated Tx lines, and 1 Rx line). I do not want to spend $65 on an arduino mega; they're really cool, but they are just too expensive. I've heard something about 'serial multiplexers' which I THINK give you more serial I/O, but there seems to be not very much documentation on using them with the arduino, OR Is it possible to build a switching circuit using high-speed transistors that can give me more serial I/O?? :-/

Have a look at the "Dual Core Arduino" in the exhibition. I think you can perhaps easily build such one yourself on a stripboard?

I was able to get the code from the original (first) post to work (I got the hardware to work with no problem). It works wonderfully when I hook up the arduino to the computer via USB.

Do I have to hook the arduino up to the computer via USB for this code to work?

I want to be able to create an arduino project that will take in MIDI via a 5pin DIN and respond by doing something (such as turn on an LED connected to one of the digital pins) - without needing to be hooked to a computer via a USB cable . . . I want it to be self contained running off a battery or external DC power. What changes do I need to make to the code in order to make this work? Is is possible?

FYI - I had nothing hooked to the arduino when loading the program and nothing hooked to the arduino as it boot up after unhooking the USB and changing to external power (changing the power selection jumper). The LED on pin13 never lights up when I change to external power (no USB) - indicating that the program does not work properly when no USB is used.

I'm sure there is a simple answer to this - I am quite new to programing. I appreciate any help you have as I am working on an installation piece for next Fall.

Thank you!