Weird serial behavior. Serial.Println() doesn't work in setup(), only loop()

So I'm using a BLE Sense; and I've having COM issues I never experienced on my Uno.

Anytime I upload a sketch, the required COM port seems to change between 4 and 5 at random. On my Uno the COM port was always fixed to 3; and I never had these issues.

COM 4 is the regular port, and COM 5 is the bootloader port.

Additionally, it seems Serial.Println() only prints to the serial monitor in the loop() function.

Edit: Upon further testing this seems to be an issue with the serial window refusing to connect upon hitting the reset button; I am currently getting around this by closing the serial window, entering bootloader mode, then exiting bootloader mode and opening a new serial window.

I have placed code in the setup() function, opened the monitor, hit the reset button and nothing happens.

If I put Serial.begin() in setup() and Serial.println() in loop() it works; but for whatever reason println() won't work in setup().

void setup() {
  // put your setup code here, to run once:
  int i;
  Serial.begin(9600);
  for(i = 0; i < 10; ++i)
  {
    Serial.println("test");
    delay(1000);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

The above code works fine on a Uno; but not on my BLE Sense.

I'm at a loss here; can anyone advise on how to fix the COM port randomly changing and how to get Serial.Println() to work in the setup?

sunmisola:
Anytime I upload a sketch, the required COM port seems to change between 4 and 5 at random. On my Uno the COM port was always fixed to 3; and I never had these issues.

This is the expected behavior. Many of the new Arduino's handle the connection to the PC directly (they execute USB firmware code and have USB peripherals inside). Older Arduino which do not have the USB capability use separate chips to handle USB.

For some reason, the firmware uses two different virtual COM ports (one for download and one for the serial monitor). Only one is active at a time.

sunmisola:
I'm at a loss here; can anyone advise on how to fix the COM port randomly changing

You cannot. After the sketch is uploaded the Arduino needs to be reset which disconnects the USB connection. During boot the USB is initialized and restarted.

Many sketches use the following code for the Serial. This will ensure the sketch will wait until you connect the Serial Monitor. You will have to disable the second line of code when you want your sketch to run powered by a battery without the Serial monitor. I have used timeouts in some sketches so I can do both. After a fixed amount of time waiting for the Serial Monitor to connect the sketch continuous without Serial.

Serial.begin( 9600 );
  while ( !Serial );

Thanks for the response; it's good to know this is expected behavior, it's just taking some getting used to coming from an Uno.

Seems my initial claim about it having to do with the setup() and loop() functions is incorrect; it's all just a matter of what COM port is active when you open the serial monitor.

Took some getting used to, but once you know how it behaves I guess it isn't all that bad.