Serial Values

Hey there,
I added a MIDI Input to my Sequencer like this : http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1187962258/

now When I Serial.print(incomingByte, DEC);

i get stuff like this: this : à sÿÄ@ààòààdÿàòàüÔü qÿôýàòô

Do you have an idea where the problem is?

-Flo

Incorrect line-speed?

There is no option to set the serial monitor to a baudrate of 31250.

Mystery solved.

yeah = )
now I see four values for each midi message. often it is 0000 ...

thats nothing to go on. mh.

if midis baudrate is 31250 how can i see the midi messages on the pc, where i a have baudrate oder 38400.

or does a higher baudrate not matter and the problem is somewhere else?

What is the TX Pin good for?

If you are on windows you could use putty.exe as serial monitor.
FYI, putty can also communicate at higher speeds than 115200. I succesfully communicated the Arduino at 230400 and 345600 baud. This latter is approx 40 char per millisecond,

Better option is to use a second Arduino that talks 31250 on hardwareserial and 115200 on a software serial

Be aware that the serial monitor is not able to interpret MIDI bytes,
So if you have an Arduino to spare go for option 2 and build us a MIDI sniffer and “thou will gather eternal f(l)ame” :wink:

here a 0.1 MIDI sniffer sketch

// 
//    FILE: MIDIsniffer.pde
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: get insight in MIDI comamnds
//
// HISTORY: 
// 0.1.00 - 2011-07-16  initial version, rob tillaart
//                      hex dump only
//
// 
// Released to the public domain
//

#include <NewSoftSerial.h>

#define MIDI_BAUD 31250
#define MONITOR_BAUD 115200

NewSoftSerial Monitor(4, 5);

void setup()
{
  Serial.begin(MIDI_BAUD);
  Monitor.begin(MONITOR_BAUD);  
  Monitor.println("MIDI sniffer 0.1.0");
}

int i = 0;

void loop()
{
  if (Serial.available() > 0)
  {
    int m = Serial.read();
    Monitor.print(m, HEX);
    Monitor.print("\t");
    i++;
    if (i == 10) 
    {
      i = 0;
      Monitor.println();
    }
  }
}

There exist midi2text tools (and vice versa) that may be used to extend above sketch

succes
Rob

thanks, i just used putty and got the same for values in a row.

Is it perhaps, that i do nothing with the tx pin?

i forgot to say that i use another optocoupler than the guy used in his little tutorial.
i use this one http://www.produktinfo.conrad.com/datenblaetter/175000-199999/187038-da-01-en-PC817.pdf

it has no base connector . could this be a problem?

furthermore i use a 2,2k instead of a 3,3k resitor.

Beware that MIDI messages are BYTES (0..255), not ASCII text, that's why I proposed to make HEX of them in the MIDI sniffer ....

yo, but the given example works should work...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1187962258/

perhaps my optocoupler is too slow...

perhaps my optocoupler is too slow

It would be a very slow opto device indeed if it couldn't deal with MIDI rates.

Shit. I really dont know what the problem is, and if there is actually a Problem.

    }
    
    if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
   Serial.print("incomingByte");
    // wait for as status-byte, channel 1, note on or off
    if (incomingByte== 144){ // note on message starting starting
      action=1;  
    }else if (incomingByte== 128){ // note off message starting
      action=0;
    }else if (incomingByte== 208){ // aftertouch message starting
       //not implemented yet
    }else if (incomingByte== 160){ // polypressure message starting
       //not implemented yet
    }else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
      note=incomingByte;
      playNote(note, 0);
      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
      velocity=incomingByte;
      playNote(note, velocity);
      note=0;
      velocity=0;
      action=0;
    }else{
      //nada
    }
  }

this is the code and i always get 4 integers on the serial monitor, when i send a midi note to it.
perhaps it is because the arduino is also connected to my pc serially. could this be a problem?

Think the code is buggy ...

}else if (incomingByte== 160){ // polypressure message starting
       //not implemented yet
    }else if ( (action==0)&&(note==0) )

after reading a byte from the line you set some flags which are tested in the same if then else ladder as the incoming bytes. Think you have to remove the 2nd else in above snippet.

  • read values & set flags
  • take action upon flags.

General remarks:

  • use of proper indentation makes the flow more clear
  • Serial.print("incomingByte"); is informative, but why not printing the incoming byte itself too?

its so strange, when i do stuff on my midi gear, like editing a note and change the menu, then there comes 144 as byte to the midi in. sometimes it works after hitting a key of the keyboard very often.
thats even still working when i write incomingByte = Serial.read()- '0';

@robtillaart, i ll take a closer look at what you said, when actually anything is working

EDIT:
this now comes in the Serial.Monitor (i played one note) :

-48-48-48-48-48-48206 (if initiliaze the variable "incomingByte" not as byte but as int

if i set it to byte again and "Serial.print(incomingByte, DEC) i get this for a note :
208208208208208206

if i do not write incomingByte = Serial.read()- '0'; but
incomingByte = Serial.read();
i get this as decimal serial print for a note:
00254254000254

if i set it to byte again and "Serial.print(incomingByte, DEC) i get this for a note :
208208208208208206

that looks good as 208 is the aftertouch , see your code .

}else if (incomingByte== 208){ // aftertouch message starting
       //not implemented yet

So that seems to be the proper way to interpret the data…


Stil I don’t understand your code. I would expect something like this below (not tested).

void interpretMIDI()
{
  while (1)  // do it forever
  {
    while (Serial.available() == 0); wait for start of MIDI message

    incomingByte = Serial.read();
    // Serial.print("input: ");
    // Serial.println(incomingByte, DEC);

    switch(incomingByte)
    {
      case 128: // note off message
        while (Serial.available() < 1);  // wait until whole message in buffer  - assumption you need 1 other byte
        note = Serial.Read();               // read the note to stop
        playNote(note, 0);        
      break;

      case 144 : // note on message 
        while (Serial.available() < 2);  // wait until whole message in buffer - assumption you need 2 other bytes
        note = Serial.Read();
        velocity = Serial.Read();
        playNote(note, velocity);
      break;

      case 160: // polypressure message starting 
        // read the rest of the command
        // and action 
        //not implemented yet
        break;

      case 208: // aftertouch message starting 
        // read the rest of the command
        // and action 
        //not implemented yet
        break;

      default:
        // Serial.print("unknown code : ");
        // Serial.println(incomingByte, DEC);

    } // end of switch 
  }  // end of while
}

ok, i set it up like this again, but if I just dont get an 144 for note on...
just 208 and for example 220

perhaps it is just not right circuit for this optocoupler :
i just found this :
http://old.8052.com/forum/read.phtml?id=118042

i resoldered with trimmers instead of the fixed resistors. now i can only get 0 or 255 ... ****

Edit: Moderator. Removed offensive language.

sorry for the bad word.
The midi in works now, i dont really know why . Mh. it probably was something concerning the variable resistors i added.

Thank you for your help !