Strange output behaviour after increasing serial buffer size

Hey everyone,

hoping someone can offer some guidance on a strange issue with an Arduino mega2560:

In the code that I am working on currently I am sending a lot of midi information using serial writes and I am sending the serial writes too quickly, filling the buffer and causing pauses before the buffer is emptied.

Unfortunately I can't make the midi messages smaller and I need to send them this quickly for my project so I tried increasing the buffer. Increasing the buffer size from 64 to 256 has solved this issue, allowing me to send all midi info quickly enough for my purpose and without mini-pauses from maxing out the buffer but it has caused a strange side effect -

In the project, pins 6, 7, 8, 9, 16 & 17 are configured as outputs for LEDs (other outputs are connected to LEDs but only these 6 are effected) - with the buffer size set to 256 these LEDs are blinking every time I call a serial write.

The exact same code works perfectly fine without blinking these LEDs on serial writes when the buffer is set to 64 (but of course i get the problem the mini-pauses from filling the buffer too quickly), with the buffer set to 256 i get no pauses but these pins blinking.

My first thought was to add

Serial1.end(); 
Serial2.end(); 
Serial3.end();

in setup, as I am only using the Serial0 RX and TX for midi, and I know pins 16 & 17 are for Serial 2 so I thought the serial writes may be causing those to blink but no difference when disabling the other serial ports.

I tried changing the buffer size to 128 and i get shorter pauses and no blinking but the pauses are still no good.

I am changing the buffer size by creating new folders in the 'cores' folder, named arduino_256_serialbuff and arduino_128_serialbuff, these folders are copies of the original arduino folder with only the buffer size in the HardwareSerial.cpp file changed. Here I have changed

#if (RAMEND < 1000)
  #define SERIAL_BUFFER_SIZE 16
#else
  #define SERIAL_BUFFER_SIZE 64
#endif

to

#if (RAMEND < 1000)
  #define SERIAL_BUFFER_SIZE 16
#else
  #define SERIAL_BUFFER_SIZE 256
#endif

and in the 128 folder to

#if (RAMEND < 1000)
  #define SERIAL_BUFFER_SIZE 16
#else
  #define SERIAL_BUFFER_SIZE 128
#endif

then in the boards.txt I added 2 new boards so that i can switch between the buffer sizes from the IDE Boards menu like this -

##############################################################

mega2560.name=Arduino Mega 2560 or Mega ADK

mega2560.upload.protocol=wiring
mega2560.upload.maximum_size=258048
mega2560.upload.speed=115200

mega2560.bootloader.low_fuses=0xFF
mega2560.bootloader.high_fuses=0xD8
mega2560.bootloader.extended_fuses=0xFD
mega2560.bootloader.path=stk500v2
mega2560.bootloader.file=stk500boot_v2_mega2560.hex
mega2560.bootloader.unlock_bits=0x3F
mega2560.bootloader.lock_bits=0x0F

mega2560.build.mcu=atmega2560
mega2560.build.f_cpu=16000000L
mega2560.build.core=arduino
mega2560.build.variant=mega

##############################################################

newmega2560.name=Arduino Mega 2560 (256 Serial Buffer)

newmega2560.upload.protocol=wiring
newmega2560.upload.maximum_size=258048
newmega2560.upload.speed=115200

newmega2560.bootloader.low_fuses=0xFF
newmega2560.bootloader.high_fuses=0xD8
newmega2560.bootloader.extended_fuses=0xFD
newmega2560.bootloader.path=stk500v2
newmega2560.bootloader.file=stk500boot_v2_mega2560.hex
newmega2560.bootloader.unlock_bits=0x3F
newmega2560.bootloader.lock_bits=0x0F

newmega2560.build.mcu=atmega2560
newmega2560.build.f_cpu=16000000L
newmega2560.build.core=arduino_256_serialbuff
newmega2560.build.variant=mega

##############################################################

secondnewmega2560.name=Arduino Mega 2560 (128 Serial Buffer)

secondnewmega2560.upload.protocol=wiring
secondnewmega2560.upload.maximum_size=258048
secondnewmega2560.upload.speed=115200

secondnewmega2560.bootloader.low_fuses=0xFF
secondnewmega2560.bootloader.high_fuses=0xD8
secondnewmega2560.bootloader.extended_fuses=0xFD
secondnewmega2560.bootloader.path=stk500v2
secondnewmega2560.bootloader.file=stk500boot_v2_mega2560.hex
secondnewmega2560.bootloader.unlock_bits=0x3F
secondnewmega2560.bootloader.lock_bits=0x0F

secondnewmega2560.build.mcu=atmega2560
secondnewmega2560.build.f_cpu=16000000L
secondnewmega2560.build.core=arduino_128_serialbuff
secondnewmega2560.build.variant=mega

##############################################################

Has anyone encountered something similar with serial writes affecting these pins on a mega board? The pins 6,7,8,9 on the mega don't seem to have any special function other than PWM so I can't understand it, I have checked all hardware many times and everything is perfect, as I say the exact same code works perfectly with the smaller buffer size and doesnt blink the leds,

I mean if the code is the same, why would changing the buffer size affect non-serial related outputs?Any help would be very much appreciated!