Is it possible for data to go out of the Serial port without explicitly being sent by your code?
Or is it possible to accidentally interfere with the Serial buffer through addressing out of bounds memory in an array etc in code?
I have an Arduino based midi instrument which generates midi commands when I press buttons. I've been updating the code for a few days now without testing which was a bad idea!
When I did test it I encountered a problem that while it worked in general sometimes it would turn on a whole bunch of notes at once. I've been using this code professionally for many years now with no problems. Something I did in the last few days programming caused this problem!! When I revert back to old code this bug doesn't manifest itself!
I have essentially three references to the offending serial port in the code. To try to figure out what is going on I attempted to print to the console "Serial" ANY data sent out on "Serial3".(Arduino mega)
void noteOn(byte channel, byte note, byte velocity) {
Serial.println();
midiMsg((0x90 | 0), note, velocity);
}
void midiMsg(byte cmd, byte data1, byte data2) {
Serial.print(cmd, HEX);
Serial.print(data1, HEX);
Serial.print(data2, HEX);
Serial3.write(cmd);
Serial3.write(data1);
Serial3.write(data2);
}
void setup() {
Serial3.begin(31250);// midi
Serial.begin(250000);// console
}
So I play the instrument for a few minutes and suddenly a bunch of notes are turned on. But looking through the console output shows no extraneous "note on" commands being sent.
I attached a jumper wire from Serial3"TX" to Serial2 "RX" and added code to print this data back to the main Serial console in the loop().
if(Serial2.available()){
Serial.print(Serial2.read(), HEX);
}
I can play for a few minutes and the output on the console matches the input. i.e. I get two identical midi messages (the one I intend to send out matches the one actually sent out), but when eventually the random bunch of notes gets turned on I can see there are extra commands being sent out that don't match what I'm trying to send out! I don't have access to the device to post the actual midi message here unfortunately but from what I've heard it's always an ascending sequence of notes (i.e. the note numbers in the command are going up).
To reiterate: the above functions contain the ONLY reference to Serial3 in my entire code! (9,000 lines!) Somehow more data is being sent out from outside this function. I don't think it's interference in the real world (as I said I've used this system for many years and it's been extremely reliable). I Don't think it's a problem with that specific serial port as I have tried a different serial port and got the same results. I also reduced the size of my RAM usage down to just 50% in case it was a stability issue but that made no difference.
I would really appreciate any help/suggestions on this as I'm getting sick of trying to debug and I can't use the new code professionally as it loudly turns on a lot of notes that I can't turn of without pressing the "panic"(all notes off) button on the synthesizer!
Many thanks in advance.