Problems with Serial Monitor and Sleep functions

Hi,

Problem: Garbage coming out of serial monitor when sleep function included in code.

Details of problem:

When the Sleep function is included in the sketch, even before the sleep function is called, I start recieving rubbish through the Serial monitor. If I comment the sleep function out then I get the correct result through the serial monitor.
In my sketch below, I print out variables z, xinc and yinc to the serial monitor before the first sleep function is called. The output I get through the serial monitor can be seen in the attached image “Output from Serial Monitor with Sleep function”.
The output I get through the Serial monitor when I comment out the sleep function is attached in the image “Serial output with no Sleep function”.

A few notes:
i. I’m using an arduino nano
ii. I’m using an ADS1115 16 bit ADC
iii.I’m using an Adafruit SD Card reader
iv. The z, xinc and yinc variables are correctly saved on the SD card even when garbage is coming out of the serial monitor
v. When the sleep function is called, the arduino’s Tx led stays lit up for 8 seconds.
vi. When powering this via battery and having no connection to the computer, the Tx led behaves the same (i.e. stays lit up for 8 seconds).

The code I am using is:

#include <LowPower.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <SPI.h>
#include <SD.h>
int pinCS = 10;

int16_t adcx1;
int16_t adcx2;
int16_t adcy1;
int16_t adcy2;
float xinc;
float yinc;
int z;
File file;
Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */

void setup(void) 
{
Serial.begin(19200);
pinMode(pinCS, OUTPUT);
z=1;
delay(2000);
  ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 0.1875mV (default)
}

void loop(void) 
{
delay(2000);
    ReadADC();
    delay(100);
    SDSave();
    delay(100);
Serial.print(z);Serial.print(",");Serial.print(xinc);Serial.print(",");Serial.println(yinc);
z = z + 1;

Sleep();
}

void ReadADC(void) {
      ads.begin();
      delay(2000);

      adcx1 = ads.readADC_SingleEnded(1);
      delay(50);
      adcx2 = ads.readADC_SingleEnded(1);
      delay(50);
      
xinc = float(adcx1)+float(adcx2);
xinc=xinc/2;
delay(50);
      adcy1 = ads.readADC_SingleEnded(2);
      delay(50);
      adcy2 = ads.readADC_SingleEnded(2);
      delay(50);
      
      yinc = float(adcy1)+float(adcy2);
      yinc=yinc/2;
}

void SDSave(void) {
  SD.begin();
  delay(1000);

  delay(50);
        if (!SD.begin(pinCS)){
              file = SD.open("DATA.csv", FILE_WRITE);file.print(z);file.print(",");file.print(xinc);
              file.print(",");file.print(yinc);file.println(",");file.close();
              delay(50);
        }
}


void Sleep(void) {
        
         LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
         LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); 
        
}

Does anyone have some insight to share relating to this problem?
Thanks.

Serial.print() returns almost immediately, the bytes are just copied to an internal buffer. If you call Sleep() directly after several Serial.print() calls the data will still be in the buffer when the processor goes to sleep and you don't see any real data on the serial interface. Insert a Serial.flush() before the Sleep() call and you will get normal output.

Excellent, "Serial.flush();" worked perfectly. Thanks.

Just out of curiosity I tried a delay function in place of the Serial.flush() (Now that you've identified the problem area) and that worked also.

Just out of curiosity I tried a delay function in place of the Serial.flush() (Now that you've identified the problem area) and that worked also.

Yes, that works too but Serial.flush() blocks only for as long as it's necessary while delay() might block for much to long, so using much more energy.