Arduino m0, printing to serial monitor possibly preventing reprogramming?

Hello.

About a week ago I encountered an issue where my m0 suddenly stopped being able to be programmed. I was programming and reprogramming it easily and consistently with no changes to any external connections. Then I uploaded code that printed to the serial monitor and read the ADC in a loop. This code worked fine. The issue was that when trying to upload new code to my m0, the Arduino IDE would essentially timeout on the first attempt to upload in a given IDE session.

The message window would print the following (I enabled the verbose compiler setting) :

-It prints that it is forcing reset with 1200bps open/close on COM7
-It prints many duplicates (20 total lines) of the following line:
PORTS {COM7, } / {COM7, } => {}
-It says it is uploading to selected port COM7.
-It displays in orange the avrdude: Version 6.3 etc.
-The orange avrdude indented text stops after the line:
Overriding Baud Rate : 57600
-Then It hangs for maybe 30 seconds and then prints (also in orange):
An error occurred while uploading the sketch

Subsequent attempts without closing the window quickly deliver errors claiming that the Port is busy, and that there was an "Error touching serial port 'COM7'". Again, this was last week.

To troubleshoot and try to fix this I tried to upload to the board using a different USB port, a different USB cable, a different COM port, after restarting the IDE, and after restarting my computer. I also tried to update my IDE and board drivers but my computer claimed they are up to date.

I was suspicious that the issue was caused by the very quick serial monitor program uploaded to the board. I drafted up a forum post on the issue but decided to plug in the board for one more try before I posted it, and the board worked. I swore never to use the serial monitor in that way ever again and deleted my post.

In the present, I was reading values through the analog input pin on my m0 and trying to pass them to a DAC I have connected via SPI. When the chain didn't immediately work upon programming the board, I added code to print the read ADC value to the serial monitor. I figured out what my issue was and went to upload the changes to the board but an identical issue to the one I faced last week occurred. Again, restarting the board, changing USB ports, USB cords, and restarting my computer haven't allowed me to reprogram the board. Unfortunately my desperate pre-forum-post attempt didn't work this time.

ULTIMATELY, The m0 board has the ON, L, and TX LED's illuminated solidly with no visual flashing/flickering on any of them. The board currently is running a program that reads an ADC value, prints it to the serial monitor, and then does some math on the read value. It performs these operations in a loop. I connect the board to my computer via USB and my Windows 7 PC recognizes the connected device at COM7, and the Arduino IDE allows me to select that COM port under the Port: section of the tools dropdown. However I am unable to upload a new sketch, not even an example program. I am under the impression this is because I am talking over the serial connection using the code:SerialUSB.print(val); in a loop with very little else going on.


Does anyone know how I could make my board stop running this program? Under the large assumption that my issue is traffic on the USB, I believe that somehow stopping the currently loaded program would fix my problem. I haven't been able to find any convincing or understandable methods for pausing or stopping the program besides the 1200bps signal that the IDE is already trying. I don't know if this is actually a fix or not, my real question is how do I program my board again, and prevent this from happening in the future.


I attached what is very close to the code that was running on the board, I don't have the exact version that was uploaded saved. I reverted the few small fixes I made so that I think this is what is running on the board currently (I removed a bunch of the less helpful comments I had in the file for this post as they add a lot of bulk and aren't too helpful to anyone but myself). I think (for lack of anything else to point a finger at) lines 79 & 80 are the root of the issue, as they might have been the only change between that sketch and the previous. If the sketch on the board is irrelevant and I'm looking down the wrong path I apologize for having gone into so much detail on the sketch and errors etc. but I believe the sketch is all that changed from when the board was programmable to now.

Thank you for any help you can offer.

Best,

KQ

simplified_ADC_DAC_sketch.ino (3.57 KB)