Serial.println() statement ignored unless is followed by a delay()

Hi,
I have this statement as part of a code running on my Electron. Code is compiled locally using the new Particle Workbench.

if(variable == 1) //lowest power consumption, reboots the device from start
      {
        Serial.println("Entering Sleep Mode");
        System.sleep(SLEEP_MODE_SOFTPOWEROFF, seconds, SLEEP_DISABLE_WKP_PIN);
      }

No text is displayed on my serial monitor. Yet adding a 1ms deplay makes the message displayed correctly

if(variable == 1) //lowest power consumption, reboots the device from start
      {
        Serial.println("Entering Sleep Mode");
        delay(1);
        System.sleep(SLEEP_MODE_SOFTPOWEROFF, seconds, SLEEP_DISABLE_WKP_PIN);
      }

Is it due to some asyncronicity in println() or compiler optimization option?

Thanks
Simone

The Serial.println() function returns before all serial data has been written. If it’s followed immediately by a System.reset() or System.sleep() function, the device will reset or sleep before the serial data has had time to be sent.

3 Likes