works: MIDI-IN: code + schematics

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?

Hi all,

I had a few questions.
I'm using some software to send midi data from the pc to a midi kabel.
I'm still confused about the pin's. I don't know if i'm mixing them up.
"putting an LED between pins 4 and 5 of the jack"
seems a good way to figure out which pin is four and which is 5.
what way would the current flow?

Should the led light up like this:
4 Cathode,
5 Anode

or like this:
4 Anode,
5 Cathode??

And what about channels? will this work with all channels? or: How to set channels?

I hope you can help me!

Titus

Hi,
(German? (kabel ;)))
this seems explicit :

The channels are included in the status byte :
176 is CC for channel 1, 177 channel 2 etc
144 note on channel 1, etc
Have a look at the specifications here : http://home.roadrunner.com/~jgglatt/tech/midispec.htm

Hi,
thanks for the fast reply,
dutch :wink:
Actually i'm looking at something like this:
http://highlyliquid.com/midi-accessories/midi-connector-inline/midi-plug-inline.jpg
or
http://highlyliquid.com/midi-accessories/midi-connector-inline/midi-jack-inline.jpg

That's why I didn't understand it.
Could you test my led thing? to see if I can find the pinning out that way!

What whould be note on channel 2? 145?
I think so.
So that's something I can test aswell! from 0x90 to 0x9F

I think I'm going to use Hexa codes, easier to understand some things, like kind of action followed by channel
quote from the link:
"where Middle C is note number 60"
I thought we were listening for 36 as the middle c.
there's another strange thing!!!!

I´m starting to understand midi :smiley:
Actually I'm here to help AndreasViruly with his project :wink:
list with all commands, although we we only need note of and note on:
http://harmony-central.com/MIDI/Doc/table1.html
and all the notes:
http://harmony-central.com/MIDI/Doc/table2.html
http://www.wavosaur.com/download/midi-note-hex.php

The one thing I still don't understand is:
Why is the code referring to middle c as 36(0x24), though midi uses 60(0x3c) as middle c?

Next Friday we will debug the hardware, by controlling the correct pinning.
And the led in stead of arduino rx pin.
If that all works, the software should be piece of cake, considering that I now understand it all, thanks to you!

Titus

actually I edited the code a bit with nice comments, so every one would be able to understand the code!

/* Midi In Basic  0.2 // kuki 8.2007
 *
 * -----------------
 * listen for midi serial data, and light leds for individual notes

 IMPORTANT:
 your arduino might not start if it receives data directly after a reset, because the bootloader thinks you want to uplad a new progam.
 you might need to unplug the midi-hardware until the board is running your program. that is when that statusLed turns on.

#####################################################################################################################################################
SOMETHING ABOUT MIDI MESSAGES
 midi messages start with one status byte followed by 1 _or_ 2 data bytes, depending on the command

 example midi message: 144-36-100
   the status byte "144" tells us what to do. "144" means "note on".
   in this case the second bytes tells us which note to play (36=middle C)
   the third byte is the velocity for that note (that is how powerful the note was struck= 100)
  
 example midi message: 128-36
   this message is a "note off" message (status byte = 128). it is followed by the note (data byte = 36)
   since "note off" messages don't need a velocity value (it's just OFF) there will be no third byte in this case
   NOTE: some midi keyboards will never send a "note off" message, but rather a "note on with zero velocity"
  
 do a web search for midi messages to learn more about aftertouch, poly-pressure, midi time code, midi clock and more interesting things.
#####################################################################################################################################################

HARDWARE NOTE:
The Midi Socket is connected to arduino RX through an opto-isolator to invert the midi signal and seperate the circuits of individual instruments.
connect 8 leds to pin2-pin9 on your arduino.

####################################################################################################################################################


 */

//variables setup

byte incomingByte;
byte note;
byte velocity;


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

int action=2; //0 =note off ; 1=note on ; 2= nada


//setup: declaring iputs and outputs and begin serial
void setup() {
  pinMode(statusLed,OUTPUT);   // declare the LED's pin as output
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  
  //start serial with midi baudrate 31250 or 38400 for debugging
  Serial.begin(31250);        
  digitalWrite(statusLed,HIGH);  
}



void loop () 
{
  if (Serial.available() > 0) 
  {
    // read the incoming byte:
    incomingByte = Serial.read();

    //edit the 144, note ON, and 128, note OFF message if you are
    //not using MIDI channel 0!!!!!!!!!!!!!!!!!!

    if (incomingByte== 144)
    { // note on message starting starting
      action=1;
    }
    else if (incomingByte== 128)
    { // note off message starting
      action=0;
    }
    else if ( (action==0)&&(note==0) )
    { //note off 
      //receive note
      note=incomingByte;
      //play note(OFF)
      playNote(note, 0);
      //reset note+velocity+action
      note=0;
      velocity=0;
      action=2;
    }
    else if ( (action==1)&&(note==0) )
    { // note on
      //receive note
      note=incomingByte;
      //we end here to wait for the velocity
    }
    else if ( (action==1)&&(note!=0) )
    { // note on
      //receive velocity
      velocity=incomingByte;
      //play note(ON, or OFF if velocity==0
      playNote(note, velocity);
      //reset note+velocity+action
      note=0;
      velocity=0;
      action=0;
    }
    else
    {
      //no ON or OFF received. waiting for something to happen
    }
  }
}

void Blink()
{
  //test routine
  digitalWrite(statusLed, HIGH);
  delay(1000);
  digitalWrite(statusLed, LOW);
  delay(1000);
}


void playNote(byte note, byte velocity)
{
  //in case the keyboard doesn't use "turn note OFF" commands,
  //but rather sends a "note ON, with 0 velocity" command
  //this check does figures that out
  
  int value;
  if (velocity >10)
  {
    value=HIGH;
  }
  else{
    value=LOW;
  }

  //Although this piece of code is still strange in my opinion
  //regarding that notes form 36 to 44 aren't isn't the octave
  //of the middle c
  
  //this code shifts the notes to the correct pins
  //you can empliment more code here to turn on more
  //led(led driver/multiplexing/charlieplexing/etc...
  if(note>=36 && note<44)
  {
    byte myPin=note-34; // to get a pinnumber between 2 and 9
    digitalWrite(myPin, value);
  }

}

Hi,

http://highlyliquid.com/midi-accessories/midi-connector-inline/midi-jack-inline.jpg will be exactly the same than the connector I showed, and the other one will be in mirror.

I'm sorry, I can't really test the led thing right now, I have currently a problem with my hardware. :-/ I did this two times, in one case a consolidated the soldering on the veroboard and it doesn't work since :o The other case, I went from breadboard to veroboard and now...
So I have to debug this, but it doesn't seem so easy... But I could try the LED to help me !

Middle C is just 60 because MIDI begins at the -1 octava.

Note on 0x90 to 0x9F
Note off 0x80 to 0x8F but often note on with 0 velocity is used instead of note off!

The one thing I still don't understand is:
Why is the code referring to middle c as 36(0x24), though midi uses 60(0x3c) as middle c?

Which code are you talking about?

Oh yes, ok. The 36 is definitely an error!

this code:

SOMETHING ABOUT MIDI MESSAGES
 midi messages start with one status byte followed by 1 _or_ 2 data bytes, depending on the command

 example midi message: 144-36-100
   the status byte "144" tells us what to do. "144" means "note on".
   in this case the second bytes tells us which note to play (36=middle C)
   the third byte is the velocity for that note (that is how powerful the note was struck= 100)

Well, that was what I wanted to hear!!!!!!!
"http://highlyliquid.com/midi-accessories/midi-connector-inline/midi-jack-inline.jpg is the same, and the other is MIRRORED"

So we will try that :smiley:

Good luck debugging your things. If i can help(new to midi, but debugged quite a lot of other things. Robotics....

EDIT:
You saw the 36.
Strange that NO ONE mentioned it :open_mouth:
it is a darn error, I think it is THE error why people weren't getting their stuff to work!

How would this be wired with a 4 pin optocoupler?

Hi every one,

Today I debugging Andreas his schematics etc.
We made good progress, but got stuck later on.
As mattgilbernet said:

'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?

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.

We tried sending serial data from the pc to the arduino, and that worked. Arduino was able to receive that.

So the big question is:

WHY is arduino not receiving the serial data from my midi signal?
(p.s. the code never gets past the "if (Serial.available() > 0)" part)

THANKS

Titus

EDIT:
@sciguy

take a look at the schematic of the optocouper's inside on page 1
Your optocoupler has no pin 3 and no pin 6 is my guess
so you'd only need to skip the 100K Ohm resistor to ground.
The rest is the same
GL Titus

Hello everyone,

As Titus stated in the last post; we still have some problems with the RX of my Arduino.
I have an Arduino Duemilanove.
I use this with a M-Audio Uno Usb to midi device.
Also I downloaded MIDI OX to check whether my outputdevice works.
This works.

Everything works accept the incoming signal through the RX.
What are we missing? Are we using the RX pin correctly?
Can we use the usb-port as the poweradapter at the same time of the RX use? The usb is not connected to the PC, only to a AC adapter.

Thanks in advance,

Andreas

(to titus)

Okay, thanks.

I hope to sometime make an animusic-ish thing.

AndreasViruly, Titus, what's the voltage coming on pin RX?

Tep, do you mean with the arduino connected, or without the arduino connected?
BTW, do you need to declare the Rx pin as an input, to lower the internal pull up/down resistor?

Could the voltage be to low for the Arduino to notice?
In that case you'd suggest lowering the 3k3? or making it bigger?

Andreas, could you measure the Voltage over the rx pin?

You don't really need the arduino to be connected.
Since it's standart serial communication, nothing to declare concerning the arduino pin.

Let see! You're perhaps having the same problem than me...
Just measure the idle and the busy voltage of the pin!
(Btw, it's perhaps easier if you can send something continuously like MTC, MMC, etc.)