Using while loops prevent uploading code

Hello all,
I’m facing a problem with uploading newer code to my hardware whenever I have a while loop in my code. First, I will explain the general version of my problem, and then, I will explain my code’s aim and my problem in detail. So I apologize for the long post beforehand. Please bear with me.

Board: Sodaq One
microcontroller: ATSAMD21G18
OS: Windows 10
IDE: Arduino 1.8.13

Main problem
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, going to the bootloader menu, by double-pressing the reset button, 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. 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 go to the bootloader menu and try to upload again; I get the error that there is no board available on the COM port. These errors are actually expected if I am not mistaken, but I don’t understand why a while loop causes all this pain!

Long version and case-specific
I am using Sodaq One board, which has an “ATSAMD21G18, 32-Bit ARM Cortex M0+” microcontroller, Lora, and GPS antennas. My goal is to send some sensor data using Lora communication. I have already broken down the program into smaller chunks, the Lora communication, accessing the sensor data, printing it, and so on.

The part that has been really difficult for me is where I want to use a Lora payload protocol. It involves using the thingML library. In a nutshell, I create some objects with properties such as name, unit, value and render them to Cbor HEX value, which I will send using my Lora communication.

One problem I face is that the rendered Cbor data is a char array of type stream, and the packet sending function from RN2483 library is of type uint8_t array. So I use a bunch of helper functions to bridge the gap (since this is my doing, probably if something goes wrong, it would be here).

In this specific case, I have two functions to convert the stream to hex data, fromStreamtoHEX, a for loop going through the array, and streamtoHEX, a while loop doing the same thing. When I use the streamtoHEX function, I cannot upload code again to my board, as I have explained above. This problem has happened before many times. For example, I had a 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 this pain of not being able to upload code.

I have attached my code files to this post (only the thingsML and helper functions since this is a stand-alone program for testing my code).

Thank you for your time. :slight_smile:

__test.ino (437 Bytes)

helper.ino (1.83 KB)

thingsML.ino (1.16 KB)

this is a pretty specific board so may be best to ask the question on their forum?

my suggestion: try to double press the reset button 1 second apart (not 3 times or it will go back to normal reboot) to manually put the device in bootloader mode but also then double check the COM port in the IDE as it will likely change. You need the correct port to upload

J-M-L:
this is a pretty specific board so may be best to ask the question on their forum?

my suggestion: try to double press the reset button 1 second apart (not 3 times or it will go back to normal reboot) to manually put the device in bootloader mode but also then double check the COM port in the IDE as it will likely change. You need the correct port to upload

Thanks a lot for your reply. I did not know that COM port changes on bootloader mode! I will also post it in the Sodaq forum as you suggested.
Thanks.