Pro-Micro stuck on upload

I have read multiple posts on how to reset the sparkfun pro-micro when it gets stuck during upload, and this seems to work to get it back to a state where I can upload new code again - However, I am here to ask if it is possible that it is something in my code that is causing this issue?
The reason I suspect it is my code is because I have tried:

  • Different pro micro
  • Different USB cable
  • Downloaded latest IDE
  • Downloaded latest AVR boards package
  • Tried reverting back to an older AVR boards package as suggested in a different post

Not sure if this is a coincidence, but when I upload a simple blink sketch I don’t have any trouble, but as soon as I upload my code (below), it gets stuck on uploading…
I can get it to eventually upload after many attempts using the reset during upload trick, but it seems to be unstable. I say unstable because sometimes it will print out the values requested, and other times it will print nothing…

    int clampVal = 0;

    void setup() {
      Serial.begin(115200);
    }


    void ClampReading()
    {
        const int numRead = 400; //amount of calibration values we will average
        double vals[numRead];      // the readings from the analog input
        int readIndex2 = 0;                 // the index of the current reading
        double total2 = 0;                  // the running total
        int calibration2 = 0;               // calibration incrementer

        for (int thisReading2 = 0; thisReading2 < numRead; thisReading2++) {
        vals[thisReading2] = 0;
        }
        while(calibration2<numRead)
        {
          int rawClamp = analogRead(A0);
          total2 = total2 - vals[readIndex2];
          vals[readIndex2] = rawClamp;
          total2 = total2 + vals[readIndex2];
          readIndex2 = readIndex2 + 1;
          if (readIndex2 >= numRead) {
            readIndex2 = 0;
         }
          clampVal = total2 / numRead;
          calibration2++;
        }
          
    }  


    void loop() {
     ClampReading();
     Serial.println(clampVal);
    }

Once I reset and upload an EMPTY sketch, I can upload the empty sketch with no problems. I did it 5 times in a row with no issues. Then tried to upload the code above once, and it worked…but then tried to upload second time, and stuck in uploading again… it has to be the code, but I can’t narrow down what it is.

Was answered here: https://arduino.stackexchange.com/questions/70525/sparkfun-pro-micro-stuck-on-uploading/70534#70534

Short answer is that I was printing too fast and needed a delay with the promicro of at least 500ms between prints. This fast printing was not allowing the tx/rx lines to be open to flashing new program.

I’m not familiar with the Pro Micro; I’m using Leonardos and have not observed the behaviour.

Anyway, you can try to use Serial.availableForWrite() to check if there is space in the buffer to send the data. An int can be maximum 5 digits plus a sign.

// hang here till there is space for 6 characters
while(Serial.availableForWrite()<6);
// print
Serial.println(clampVal);

Notes:
1)
A standard analogRead takes around 100 microseconds.
2)
Serial communication over native USB is not influenced by baudrate; you can e.g. set the baudrate in serial monitor to anything without modifying the code and you should get output.
3)
Your code might be missing a while(!Serial) in setup().