Mega2560 Stalls before completing Upload

Hi Folks,
My Mega2560 is frustrating me totally at the moment. When loading a program, the IDE appears to negotiate the upload and then the progress bar halts about 4/5's of the way across and stops there. The Serial LED's continue to flash but nothing more happens, the IDE just waits and waits.
The program is about 27kb and using a simple memory determining tool

// free RAM check for debugging.
int availableMemory() {
    // Use 8192 with Mega 2560 8k SRAM
    int size = 8192;
    byte *buf;
    while ((buf = (byte *) malloc(--size)) == NULL);
        free(buf);
    return size;
}

it says I have 4040bytes of memory free, the program then connects an XMem memory expansion board

void setup() {
  Serial.begin(115200); //Serial interface for Debugging
  Serial.println("?? MIDI ?? Begins.. ");
  Serial2.begin(31250); //set up Serial Interface 2 for Midi (Mega Pin16 Tx2)
  Serial.println(availableMemory());  //returns 4040
  xmem::begin(true); // use the memory in bank zero, with LowMem Stack,Heap etc
  xmem::setMemoryBank(0, true); //manually select memory bank 0.
  midiFile = reinterpret_cast<byte *>(0x2200);  
  Serial.println(availableMemory()); //returns 8191

that increases SRAM to 64kb and accordingly the checking the memory available after this says 8191bytes are free (it is a simple tool after all).
However if I add this routine

void updateNo(int nos, int line){ //Write a number into the LCD screen in ASCII from 15-18 characters across
    if(nos<0 || nos >999){//it won't render Negatives or numbers >999
    lcdLine("Out of Range!!!!", YELLOW, 16);
  }
}

near the end of the program with a simple IF statement in it, the IDE upload just stalls. (I plan to extend the routine to turn a int variable into a sequence of ASCII Numerals on an LCD screen. Little point at the moment.)

When I remove the above routine, and maybe add other routines, eg the memory checker above, it does not miss a beat.

My first thought was it was running out of memory somehow, but I think I have covered that. My second thought was a "loose cannon" pointer somewhere blasting holes in the data structures, but the program does not actually begin to run so while I can't say definitely haven't got bytes corrupting the program, that does not seem to be a problem either (as yet). The program minus the additional code above appears to behave as predicted and quite stable. Grrrrr!

If anyone has some helpful suggestions how I can debug this, I would be very appreciative.

Cheers, Rob

You need to be less flamboyant with your punctuation.

    lcdLine("Out of Range!!!!", YELLOW, 16);

Some bootloaders have a problem with !!! in the code.

Thank you Nick, for clear and and, dare I say as requested, helpful advice to solve a real brain twister..
It now compiles and uploads. I can't remember this arising in the Driver's Test, :slight_smile:
So onto the next challenges. Why oh why, did Google fail me on this?
Never mind,

Thanks so much Nick,

Rob

PS Thank goodness I left the passion in on the posted example :~

robwlakes:
Why oh why, did Google fail me on this?

Despite being a very old issue, this is a tough one to get through Google's filters, which ignore punctuation in search queries.

U R 2 Kind James,
My simple trust in the old programming adage, "you can put what ever you like between the quotes", has been broken?
However I would have been very unlikely to even have begun a Google search on ' Arduino +"!!!!" ' problems, but I am surprised I could not trigger a helpful page with all the searches I tried. Thank heavens for the Forum and you guys behind it! The whole thing has shaken me a bit that a literal like that could interfere with what should be a well delimited "binary" upload.

I am hoping that this discussion will maybe turn up quicker on Google (and hence the Forum) for other people with a similar emphatic outlook as myself :relaxed:

Thanks again AC (Arduino Community),

Rob

Googled "arduino mega 2560 upload stalls"
First hit: Can't upload to Arduino Mega 2560 - #6 by system - Troubleshooting - Arduino Forum

Problem Solved Nick, and solution verified. Now making good progress again, thank you for clear, concise and useful assistance, Rob