works: MIDI-IN: code + schematics

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!

Hello,

I have been trying a lot of solutions appearing on this post but I haven't found my problem. I'm quite sure that I have made right the circuit. But I have a old Kawai with midi out and I can't control the channel...

But i don't continue from serial.available... this may work with a wrong channel or i'm wrong?

thanks a lot!

with midi out and I can't control the channel.

You can only read a channel with a MIDI out on your keyboard, that is you can only respond to notes. You can't control anything on your keyboard with only a MIDI out.

Sorry i explained wrong.

I don't want to control a midi keyboard, but i read that the messages must be in channel 1 and in my keyboard i don't know in which channel is sending messages...

So, my question is, if i'm sending messages in other channels my arduino must detect at least the serial.available... i'm right?

thanks!

Hello there,

I have a big problem. I made the whole schematic, exactely as described in the post above. But it still fails to work.

I want to make about 36 LED's, corresponding to the midi output of my electronical keyboard.
This means that by playing the middle C, the first LED switches on. By pressing the C#, the second, and so on.

When letting the tone go, the LED should switch off.

This is a very important project for my school. Could somebody help me out?

I tried all the comments posted on this forum.
This is the code I used to debug the system.
Even this didn't work.

Another question I have is the following:
How can I tell the difference between pin number 4 and 5.
When looking at the front side of the plug, is it a fact that the left pin is always number 5?

I hope to receive a usefull reply!

Andreas Viruly from the Netherlands

byte incomingByte;
byte note=0;
byte velocity;
int statusLed = 13;   // select the pin for the LED
int action=2; //0 =note off ; 1=note on ; 2= nada

void setup() 
{
  pinMode(statusLed,OUTPUT);   // declare the LED's pin as output
  Serial.begin(31250);       
  digitalWrite(statusLed,HIGH); 
}

void loop ()
{
  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read();

    if (incomingByte== 144)
    { // note on 
      action=1;
    }
    else if (incomingByte== 128)
    { // note off 
      action=0;
    }
    else if ( (action==0)&&(note==0) )
    { // if we received a "note off", we wait for which note (databyte)
      note=incomingByte;
      playNote();
      note=0;
      velocity=0;
      action=2;
    }
    else if ( (action==1)&&(note==0) )
    { // if we received a "note on", we wait for the note (databyte)
      note=incomingByte;
    }
    else if ( (action==1)&&(note!=0) )
    { // ...and then the velocity
      playNote();
      note=0;
      velocity=0;
      action=0;
    }
    else
    {
      //nada
    }
  }
}

void playNote()
{
  digitalWrite(statusLed, LOW);
  delay(1000);
  digitalWrite(statusLed, HIGH);
}

i'm also having trouble with this. I've confirmed that midi is coming through the jack by removing the opto-isolator and putting an LED between pins 4 and 5 of the jack. I've confirmed that something is coming through the opto-isolator disconnecting RX and instead putting an LED between +5 and pin 5 of the opto-isolator. It flashes when I hit keys. Ive tried 38400 baud as well. The Arduino is just not receiving the Serial in RX. I've at least narrowed it down, can anyone provide any tips or things to try?

I'm using a 4n25. The Arduino is still able to recieve Serial over USB.

I've also tried a different schematic from Tim Igoe's Physical Computing that uses a 6N138 optocoupler and a 7404. I thought this might be a more tested and authoratative source. I got the same results: the LED tests show that everything is working, or at least signals are being passed, all the way up to Arduino's RX pin. At that point the Arduino seems not to recognize the signal.

If anyone has had success with any other serial input to the RX pin, I'd love to know about it.

Hi AndreasViruly, could you test this :

//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){ // 144 is note on ch1
      digitalWrite(statusLed, HIGH);
      delay(20);
      digitalWrite(statusLed, LOW);
      delay(1);
    }
     
 }
}

(I used this at the beginning of this project for testing the midi in.

Note that often keyboard don't use note off but note on with 0 velocity.

Mattgilbertnet, could you draw a quick shematic of your setup based on this tread?