works: MIDI-IN: code + schematics

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);
  }

}