Serial.println() problem

Hello,

I have the following configuration;
Uno with xbee shield hooked up to a gas sensor (Dräger 0-20 ma) via an ads1115.
Another xbee is connected to a pc via an usb xbee adapter.
When i use Serial.print(waarde) the values show up on the pc.
When i use Serial.println(waarde) nothing shows up.

I can’t figure out what the problem is.


#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <Time.h>

Adafruit_ADS1115 ads;

const int numReadings = 10;
int readings[numReadings];        
int index = 0;                    
float total = 0;                  
float average = 0;                              
float waarde2;
String unitId = "Desin2";         // desinfectie sensor 2 hoog
long waarde;
unsigned long previousMillis = 0;
const long interval = 4000;
long calibratie = 5333;  //   nulpunt sensor, range = 5
String inputString = "";         // inkomende data
boolean stringComplete = false;  // data compleet
boolean ZendPPM = true;

void setup() {

  Serial.begin(9600);
  inputString.reserve(200);
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
  ads.begin();
  ads.setGain(GAIN_TWOTHIRDS);       // gain one = 6.144 V

}

void loop() {
  concentratie();
  communicatie();
  tijd();
}

void communicatie()
{
  if (stringComplete) {

    if (inputString.substring(0, 4) == "ZeId") {
      Serial.println(unitId);
    }

    if (inputString.substring(0, 4) == "StDc") { // verstuur geen ppm
      ZendPPM = false;
      Serial.println("StDcOk");
    }
    if (inputString.substring(0, 4) == "ZeDc") { //verstuur ppm
      ZendPPM = true;
      Serial.println("ZeDcOk");
    }
    if (inputString.substring(0, 4) == "NulC") {
      String NulPunt = (inputString.substring(4, 8));
      long Tussenstap = NulPunt.toInt();
      calibratie = Tussenstap;

    }
    inputString = "";
    stringComplete = false;
  }
}

void concentratie()
{
  int16_t adc0, adc1, adc2, adc3;
  adc0 = ads.readADC_SingleEnded(0);


  total = total - readings[index];
  readings[index] = adc0;
  total = total + readings[index];
  index = index + 1;
  if (index >= numReadings) {
    index = 0;
  }
  average = total / numReadings;
  waarde = map(average, calibratie, 26665, 0, 50);
  delay(1);                               
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void tijd() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ZendPPM == true) {
      Serial.print("DataDc");
      Serial.println(waarde);
    }
  }

}

Which one of all your “if” conditions should be doing the println()? Add a print() some identifier to see if any of them are becoming active.
Paul

It is the last one. Serial.println(waarde);
The rest is working fine.

If i program to use Serial.print(waarde) twice and for the rest of the time use Serial.println(waarde) there is no problem.
But i don’t now why.

IF you mean this one, add a Serial.print(“ZeDcOK”); Ahead and following the one that doesn’t work.
Paul

No, it is this last one.
If i program this to print twice without linefeed and the rest of the time with linefeed it works.

Sorry, cannot give any more advice that what I have done. I am not surprised that you are having problems because of all the “String” manipulation that is being done. Perhaps that is resulting in program code corruption. Can you reprogram to use “string” instead of “String”? A “string” will be NULL terminated instead of CRLF, and will not be using the Arduino stack, etc. behind you back.
Paul

Try this:

if (ZendPPM == true) {
Serial.print(“DataDc”);
Serial.print(waarde);
Serial.println();
}

If this works then we are truly baffled.

This sketch worked for me (no problems with your Strings, no need to down grade to c-strings )

Output

11:31:40.423 -> DataDc-12
11:31:44.427 -> DataDc-12
11:31:48.427 -> DataDc-12
11:31:52.435 -> DataDc-12
11:31:56.408 -> DataDc-12
11:32:00.436 -> DataDc-12
//https://forum.arduino.cc/t/serial-println-problem/858891

#include <Wire.h>
//#include <Adafruit_ADS1015.h>
//#include <Time.h>

//Adafruit_ADS1115 ads;

const int numReadings = 10;
int readings[numReadings];        
int index = 0;                    
float total = 0;                  
float average = 0;                              
float waarde2;
String unitId = "Desin2";         // desinfectie sensor 2 hoog
long waarde;
unsigned long previousMillis = 0;
const long interval = 4000;
long calibratie = 5333;  //   nulpunt sensor, range = 5
String inputString = "";         // inkomende data
boolean stringComplete = false;  // data compleet
boolean ZendPPM = true;

void setup() {

  Serial.begin(9600);
  inputString.reserve(200);
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
//  ads.begin();
//  ads.setGain(GAIN_TWOTHIRDS);       // gain one = 6.144 V

}

void loop() {
  concentratie();
  communicatie();
  tijd();
}

void communicatie()
{
  if (stringComplete) {

    if (inputString.substring(0, 4) == "ZeId") {
      Serial.println(unitId);
    }

    if (inputString.substring(0, 4) == "StDc") { // verstuur geen ppm
      ZendPPM = false;
      Serial.println("StDcOk");
    }
    if (inputString.substring(0, 4) == "ZeDc") { //verstuur ppm
      ZendPPM = true;
      Serial.println("ZeDcOk");
    }
    if (inputString.substring(0, 4) == "NulC") {
      String NulPunt = (inputString.substring(4, 8));
      long Tussenstap = NulPunt.toInt();
      calibratie = Tussenstap;

    }
    inputString = "";
    stringComplete = false;
  }
}

void concentratie()
{
  int16_t adc0, adc1, adc2, adc3;
  adc0 = 55; //ads.readADC_SingleEnded(0);


  total = total - readings[index];
  readings[index] = adc0;
  total = total + readings[index];
  index = index + 1;
  if (index >= numReadings) {
    index = 0;
  }
  average = total / numReadings;
  waarde = map(average, calibratie, 26665, 0, 50);
  delay(1);                               
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void tijd() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ZendPPM == true) {
      Serial.print("DataDc");
      Serial.println(waarde);
    }
  }

}

What board are you actually using for this?

Uno (no xbee shield though)

This doesn’t work.

I hooked up the xbee without the shield and then it worked.
But i have concerns about the 5V tolerance of the xbee. Have to use a level shifter

For now i use this;

if (ZendPPM == true) {
if (Q <= 2){
Serial.print(“DataDc”);
Serial.print(waarde);
Q++;
}
else {
Serial.print(“DataDc”);
Serial.println(waarde);
}

And it works.

Thanks all.

Your serialEvent () function only checks for the presence of the ‘\n’ byte. Check the commands you are sending from the port monitor of PC (Line Ending Parameter). They can contain ‘\n’, ‘\n\r’, or nothing at all. If you send, for example, a string with the line ending Both NL & CR, in the input buffer of your Arduino Serial a ‘\r’ is left from the previous command, you can get a similar effect.
In such case I use Only New Line setting, or I also check for presence of ‘\ r’ in the input buffer of Serial and discard it.