I am wroking on controlling MIDI equipment with my arduino. I was using the UNO but it was frustrating to debug because I was sending serial info to MIDI using 31250 baud and could not see what I was doing on the serial monitor. I since bought a MEGA 2560 and it has 4 serial outputs. I thought I could maybe send MIDI data at 31250 Baud on one serial channel and send Serial.prints at 9600 baud on another so that I could see my debug code in the serial monitor screen.... no uck so far... Is this even possible or does anyone have any tips on how to debug when tying up my serial port with MIDI data???
Sounds like the perfect use for the MEGA. What are the symptoms? It would likely help if you post your code.
fxmech:
I am wroking on controlling MIDI equipment with my arduino. I was using the UNO but it was frustrating to debug because I was sending serial info to MIDI using 31250 baud and could not see what I was doing on the serial monitor. I since bought a MEGA 2560 and it has 4 serial outputs. I thought I could maybe send MIDI data at 31250 Baud on one serial channel and send Serial.prints at 9600 baud on another so that I could see my debug code in the serial monitor screen.... no uck so far... Is this even possible or does anyone have any tips on how to debug when tying up my serial port with MIDI data???
What you are considering is perfectly doable, and make for a useful troubleshooting tool. What you really need to make it easier is to have a standalone USB TTL serial converter cable wired to one of the additional mega serial ports. Then use a PC terminal program (there are many good free ones avalible) to monitor your debug messages. That way you can leave the arduino IDE for editing and uploading sketches to the board but have another open serial terminal program monitoring your debug serial port.
Here is one USB serial TTL level cable that works well:
http://www.ebay.com/itm/370532286388?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
You just need to attach the send, receive and ground pins to the arduino.
Lefty
Here is the code so far... Its a work in process but the intent is to play MIDI notes with two IR sensors detecting hand positions. My sensor detect code seemed fine when just sending out serial print data to the serial monitor, but when I would add code to switch over to MIDI output, the results are not correct since my notes dont shut off when I remove my hand from the sensor. This s why I wanted to be able to output numbers to the serial onitor simultaneously with the midi serial output. Hope you can help.
int sensor1 = 1;
int note1 = 1;
int prevnote1 = 1;
int sensor2 = 1;
int note2 = 1;
int prevnote2 = 1;
int velocity1 = 127;
int velocity2 = 127;
void setup()
{
Serial2.begin(31250); //Initialize MIDI serial communication
Serial1.begin(9600); //Serial Monitor baud for debugging
Serial1.print("hi");
}
void loop()
{ Serial1.print("hi");
// Check both sensors and filter results
//filter by averaging then canstraining output (1-500)
sensor1= (analogRead(A0)+analogRead(A0)+analogRead(A0)+analogRead(A0))/4;
//constrain(sensor1, 1, 500);
if (sensor1 < 20 || sensor1 >500)
{velocity1 =0;
playMidiNote(prevnote1,velocity1);
}
else
{ note1= whichNote(sensor1);
velocity1 =127;
playMidiNote(note1,velocity1);
prevnote1 = note1;
}
sensor2= (analogRead(A1)+analogRead(A1)+analogRead(A1)+analogRead(A1))/4;
//constrain(sensor2, 1, 500);
if (sensor2 < 20 || sensor2 >500)
{velocity2 =0;
playMidiNote(prevnote2,velocity2);
}
else
{ note2= whichNote(sensor2);
velocity2 =127;
playMidiNote(note2,velocity2);
prevnote2 = note2;
}
//Debug Code (chaqnge baud rate to 9600 for serial monitor)
Serial1.print ("Sensor1: ");
Serial1.println (sensor1);
Serial1.print ("Note1: ");
Serial1.println (note1);
Serial1.println ();
Serial1.print ("Sensor2: ");
Serial1.println (sensor2);
Serial1.print ("Note2: ");
Serial1.println (note2);
Serial1.println ();
delay(1500);
}
int whichNote(int sensor)
{ // takes any sensor value and outputs a note value
int note;
if (sensor >= 20 && sensor < 80)
{note = 64;}
if (sensor >= 95 && sensor < 160)
{note = 62;}
if (sensor >= 175 && sensor < 240)
{note = 60;}
if (sensor >= 225 && sensor < 320)
{note = 59;}
if (sensor >= 305 && sensor < 400)
{note = 56;}
if (sensor >= 385 && sensor <= 500)
{note = 54;}
return (note);
}
void playMidiNote(byte note, int velocity) // subroutine for playing note triggered by RH
{ //Plays a midi note on ch
Serial2.write(0xC3); //code to call change of sound
Serial2.write(30); //code to set new sound to sound #41
Serial2.write(0x93);// code to call up note change
Serial2.write(note);// specifies which note
Serial2.write(velocity); //sets volume for the note 0-127
}
Thanks for the link... I ordered some just now, but if there is a way to do it without the board it will get me through while I wait for the ebay package to arrive from china...
Yes, you have 4 independent serial streams.
Serial0 (if they are numbered 0,1,2,3)
is the one that goes to PC monitor via the USB connection.
Is that what you are monitoring?
fxmech:
Thanks for the link... I ordered some just now, but if there is a way to do it without the board it will get me through while I wait for the ebay package to arrive from china...
Only the first serial port is hardwired to the Arduino on board USB serial converter chip, so the arduino IDE can only monitor the first serial port from your board, so if your application is using that port for midi, etc then you can't use another serial port through the existing USB connection, if that makes sense. Now if you move all the midi code to use one of the other serial ports then that would free up the first serial port for debug messages.
Lefty
Thanks for the help.
I just finished debugging my code and its working great now. I did not realize that there was a serial.print in MEGA. I tried serial1.print serial2.print, etc so I cold not get the monitor to work, but when I tried serial0.print and it errored out, I then got rid of the zero and it started working...
BTW here is the corrected code:
int sensor1 = 1;
int note1 = 30;
int prevnote1 = 30;
int sensor2 = 1;
int note2 = 30;
int prevnote2 = 30;
int velocity1 = 127;
int velocity2 = 127;
int sound2 = 49;
int sound1 = 52;
void setup()
{
Serial2.begin(31250); //Initialize MIDI serial communication
Serial.begin(9600); //Serial Monitor baud for debugging
}
void loop()
{ //Serial.println("LOOPAROUND");
// Check both sensors and filter results
//filter by averaging then canstraining output (1-500)
sensor1= (analogRead(A0)+analogRead(A0)+analogRead(A0)+analogRead(A0))/4;
sensor1=max(sensor1, 19);
Serial.print("Sensor1= ");
Serial.println(sensor1);
if (sensor1 < 20 || sensor1 >500 || note1==1)
{velocity1 =0;
playMidiNote(prevnote1+12,velocity1,1,sound1);
playMidiNote(note1+12,velocity1,1,sound1);
note1= whichNote(sensor1);
/Serial.println("S1 OUT OF RANGE");
Serial.print("prevnote1= ");
Serial.println(prevnote1);
Serial.print("note1= ");
Serial.println(note1);
Serial.println();
Serial.println();/
//delay(20);
}
else
{ if (note1 != 1)
{
note1= whichNote(sensor1);
if (note1 != prevnote1)
{
velocity1 =127;
playMidiNote(note1+12,velocity1,1, sound1);
playMidiNote(prevnote1+12,0,1,sound1);
/* Serial.println("NOTE1 <> 1 WHICHNOTE ");
Serial.print("prevnote1= ");
Serial.println(prevnote1);
Serial.print("note1= ");
Serial.println(note1);
Serial.println();
Serial.println();*/
prevnote1 = note1;}
// delay(20);
} else playMidiNote(prevnote1+12,0,1,sound1);
}
sensor2= (analogRead(A1)+analogRead(A1)+analogRead(A1)+analogRead(A1))/4;
//Serial.print("Sensor2= ");
//Serial.println(sensor2);
sensor2=max(sensor2, 19);
if (sensor2 < 20 || sensor2 >500 || note2==1)
{velocity2 =0;
playMidiNote(prevnote2-12,velocity2,2,sound2);
playMidiNote(note2-12,velocity2,2,sound2);
note2= whichNote(sensor2);
/Serial.println("S2 OUT OF RANGE");
Serial.print("prevnote2= ");
Serial.println(prevnote2);
Serial.print("note2= ");
Serial.println(note2);
Serial.println();
Serial.println();/
//delay(5);
}
else
{ if (note2 != 1)
{
note2= whichNote(sensor2);
if (note2 != prevnote2)
{
velocity2 =127;
playMidiNote(note2-12,velocity2,2, sound2);
playMidiNote(prevnote2-12,0,2,sound2);
/Serial.println("NOTE2 <> 1 WHICHNOTE ");
Serial.print("prevnote2= ");
Serial.println(prevnote2);
Serial.print("note2= ");
Serial.println(note2);
Serial.println();
Serial.println();/
prevnote2 = note2;}
//delay(5);
} else playMidiNote(prevnote2-12,0,2,sound2);
}
//Debug Code (chaqnge baud rate to 9600 for serial monitor)
/* Serial.print ("Sensor1: ");
Serial.println (sensor1);
Serial.print ("Note1: ");
Serial.println (note1);
Serial.println ();
Serial.print ("Sensor2: ");
Serial.println (sensor2);
Serial.print ("Note2: ");
Serial.println (note2);
Serial.println ();*/
//delay(1000);
}
int whichNote(int sensor)
{ // takes any sensor value and outputs a note value
int note=1;
if (sensor >= 20 && sensor < 170)
{note = 67;}
if (sensor >= 170 && sensor < 210)
{note = 65;}
if (sensor >= 210 && sensor < 250)
{note = 64;}
if (sensor >= 250 && sensor < 290)
{note = 62;}
if (sensor >= 290 && sensor < 330)
{note = 60;}
if (sensor >= 330 && sensor < 370)
{note = 59;}
if (sensor >= 370 && sensor < 410)
{note = 57;}
if (sensor >= 410 && sensor < 450)
{note = 56;}
if (sensor >= 450 && sensor < 500)
{note = 53;}
return (note);
}
void playMidiNote(byte note, int velocity, int ch, int snd) // subroutine for playing note triggered by RH
{ //Plays a midi note on ch
Serial2.write(192+ch); //code to call change of sound
Serial2.write(snd); //code to set new sound to sound #41
Serial2.write(144+ch);// code to call up note change
Serial2.write(note);// specifies which note
Serial2.write(velocity); //sets volume for the note 0-127
delay(5);
}
THANKS FOR ALL THE HELP!!!
Glad to help.