MIDI INPUT AND READING BYTES

Hi there!

i'm trying to interface my arduino following a threat from the past forum ( http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1187962258/)
i'm trying to receive midi data from my arduino, and i think i made it, but when i try to read the bytes from my RX port, i get nothing.....well i get something but not what i want.
how can i serial.print the byte information to debugging ?

like this: Serial.println(incomingByte +" : incomingByte"); ?

i think not because i'm not receiving anything:

i'm a noob but i was trying to read from some websites, that's basically 0/1 information travelling in a group of 8, but can i serial.print it as we do with vars?
do i have to make a for to read it?

this is the code i'm using:

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(38400);        
  digitalWrite(statusLed,HIGH);  
}
 
//loop: wait for serial data, and interpret the message
void loop () {
  if (Serial.available() > 0) {
        // read the incoming byte: 
    incomingByte = Serial.read();
    
Serial.println(incomingByte   +"  : 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;
      Serial.print(note   +"  : note");
      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
    }
  }
}

void blink(){
  digitalWrite(statusLed, HIGH);
  delay(100);
  digitalWrite(statusLed, LOW);
  delay(100);
}


void playNote(byte note, byte velocity){
  int value=LOW;
  if (velocity >10){
      value=HIGH;
  }else{
   value=LOW;
  }

 //since we don't want to "play" all notes we wait for a note between 36 & 44
 if(note>=36 && note<44){
   byte myPin=note-34; // to get a pinnumber between 2 and 9
   digitalWrite(myPin, value);
 }

}

thanks for all your help,

Rodrigo

Serial.print() and Serial.println() can be used to print one thing at a time. That can be a constant (string) or a value.

Serial.print("I read a ");
Serial.println(incomingByte);

my problem is not how to visualize a variable with serial.print, cause i was doing
Serial.println(incomingByte +" : incomingByte");

and your proposition is the same :
Serial.print("I read a ");
Serial.println(incomingByte);

my problem is that i have the impression that i can not read the bytes, theres always strage symbols or caracters in the serial.print,
like $, é,£, etc

thanks anyway.

First off, you can not use Serial.print() the way that you are trying to. There is no operator that can add a byte and an array of characters.

Second, what difference does it make what you print after reading a byte, if there are no bytes to read?

The fact that you are getting strange output means that there WAS something to read, and that you are not printing it out correctly. So, just for giggles, try it my way.

hi again,

yes, you are right is better, i learnt actionscript and not C++, so i was doing it wrong.
but still, i don't get it, when i try to read my incommingByte it says things like this:

do you have a clue, why?
how can i control de midi notes if the are not numbers? how i can use my "if condition"?

void loop () {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    Serial.print("I read a ");
Serial.println(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
    }
  }
}

thanks a lot for your time and help!

There are several Serial.print() methods, each taking a different type of variable as input, and each processing that data in slightly different ways.

There is also an optional second argument for Serial.print() that influences how it outputs data.

Since Serial.read() returns an int, and since an int is bigger than a byte, you really have incomingByte defined incorrectly. The correct type for incomingByte is int, not byte.

If you change incomingByte to int, test that it really is byte size (i.e. is positive and less than 255), and store the value in (or cast it to) a char, Serial.print() will display the value differently.

You could also use Serial.print(incomingByte, DEC); to display the byte as a number rather than a character. Since the data that you are trying to receive is MIDI data (numbers), rather than text (characters), this last option is probably more appropriate for you.

i just want to thank you, PaulS, for all your active answers.
i found the solution, using the Arduino Midi Library, it reads the bytes for me :wink:

R