While loops prevent uploading code

Hello all,
I’m facing a problem with uploading new code to my hardware whenever I have a while loop in my code, both a forever while loop, as well as a conditional one.

Board: Sodaq One V3
OS: Windows 10
IDE: Arduino 1.8.13

problem explained
The problem is that whenever and wherever I use a while loop in my code, I am unable to upload code again to the board. I keep hard resetting the board, again and again, and only by chance, I’d be able to program the board again.

What happens is that Arduino IDE tries to upload the code after compiling, and it just does not. It says uploading…, but nothing happens; it freezes. Then I press the reset button, and it throws an error: an error occurred while uploading the code, I try to upload again, and it says that the COM port is busy; I don’t understand why a while loop causes all this pain!

For example, I had a forever while loop in my main loop (the void setup(), void loop(){} I mean ), to prevent sending Lora packets from one point forward, which I then switched to the alternative, introducing some big delays to prevent reprogramming problem.

So, to sum up, my question is, if there is a fundamental reason why a while loop prevents reprogramming the board, and if so, why and how to solve this.

I have already removed all while loop codes I had, so I have only an old example code.

void loop() {
  uint8_t packet = 0x0;
  String Payload = "";
  
  sendPacket(String(packet)); // also when commented out
  receivePacket(Payload); // also when commented out
 
  while(1){
    // something 
    delay(60000);
  }
}

Thank you for your time.

Hi Deniz,

If you fail to program your board, please remember that you can force bootloader mode by double tapping the reset button. This will help you if your device hard faults or just gets stuck.

As to why you can’t program, I’d first advise you to enable “verbose output during: upload”, check your settings. Does it still happen if you remove the while(1)?
while(1); or while(1){} should also the same thing here for you.

Please share your .ino as well, it could be that something breaks in your initialization that just happens to show up in your loop.

Regards,
Thom

Thanks, Tom, for your fast reply.

I actually tried the bootloader menu. But I guess I am not doing it right. Because when on the bootloader menu, Arduino IDE does not upload the code either. it says that “there is no board connected (or found) on the port.”

For the second part, no everything goes fine when I don’t have a while loop. So, I have already removed all while loops from my code. And tbh, since I only have one board, I am afraid to try and upload a code with a while loop in it (to make sure it gets stuck and share the code here.)

Hi Deniz,

I can assure you that while loops are perfectly fine and I cannot see anything wrong with your code (as long as the 2 functions are commented out, I can’t tell if they’re OK or not).

If the device is in bootloader mode it will appear as a different device on a different port, you’ll have to change this in your IDE to match it.

Regards,
Thom