Serial.println produces unwanted SOH+D

Hello everyone,

I found little bit strange behaviour by Serial.println. I am developing small measurement device, which is sending test status and values on UART. In one moment I am sending this:

if((testStage == 0) && (digitalRead(testValveOut) == LOW)) { //Run-up not finished yet => 
      digitalWrite(testValveOut, HIGH);                                           //filling-up
      Serial.println(":test:filling_started");  
      }

but on serial monitor I get this:

:test:fil Dng_started

in middle of text are replaced two characters by (SOH) and D. (SOH - start of heading 01 ASCII) Everything else before and after is OK.

I tried to change baudrate. To add some delay. To split the text in two parts and send it separate and lot of other changes.

What finally helped is to send once, on start of program something like that:

Serial.println(":test:entering");

With this it works ok. So far I can test at home.

But I don't like miracles like that. I would like to understand this.

Have anybody simmilar experience? Or do you know the reason?

Thanks for the hints

Jenik5

You are printing from inside an interrupt routine. If you would prefer more than an inspired guess, post all of your code (in code tags).

Pete

Exactly. Post the code, but my guess is that testValveOut is either 0 or 1 and that you should choose another pin.

Hi,

important part of code is below.

@el_supremo: No, I am printing from loop(). There are no interrupts in use.

@6v6gt: No, it is 7, see below. But even if it is 0 or 1, it should have no influence. It is Arduino Micro. Pins 0 and 1 are used by Serial1. Or am I wrong?

const int testValveOut = 7;        // testing valve output pin
void loop() {

  bool readyForTest = false;
  bool greenLEDIsOn = false;
  
  int startProbeLastState = HIGH;
  int errorLevel = 0;
  int testStage = 0;
  unsigned long testStartTime = 0;
  unsigned long runUpFinishedTime = 0;
  unsigned long greenLedBlinkTimer = 0;
  if(firstTime){
    Serial.println("Nexans autoelectric GmbH. Seal test device.");
    Serial.println("Hardware version: 1.0.0.0, 01.01.2017");
    Serial.println("Firmware version: 1.0.0.0, 01.01.2017");
    Serial.print("Serial number: ");
    Serial.println(configuration.serialNumber);
    Serial.println(":test:entering");
    firstTime = false;
  }
  while (test){
    
    if(Serial.available()>0) {
      test = false;
      Serial.println("going_off_line");
      break;
      } 
      
    if(!readyForTest && (testStartTime == 0)) {
      Serial.println(":test:ready");
      readyForTest = true;
      }       
      
    if(digitalRead(failIn) == LOW) analogWrite(redLedOut,configuration.brightRed);
    else analogWrite(redLedOut,0);
     
    if((digitalRead(releaseButtonIn) == LOW) ||    //Release button pushed or
       (digitalRead(startProbeIn) == HIGH)) {      //presence probe not activated?
      digitalWrite(presenceOut, LOW);             //Switch all off and init 
      digitalWrite(cpaTestResultOut, LOW);
      digitalWrite(sealTestResultOut, LOW);
      digitalWrite(catchValveOut, LOW);            
      digitalWrite(testValveOut, LOW);
      analogWrite(greenLedOut, 0);
      greenLEDIsOn = false;
      analogWrite(yellowLedOut, 0);
      analogWrite(blueLedOut, 0);
      analogWrite(cpaLightSourceOut,0); 
      startProbeLastState = HIGH;
      errorLevel = 0;                        //Init test result.
      testStage = 0;
      testStartTime = 0;
      runUpFinishedTime = 0;                 //Init run-up phase
      continue;   
      }     
      
    digitalWrite(catchValveOut, HIGH);                     //Catch it
    digitalWrite(presenceOut, HIGH);                       //Activate output for tester "connector in adapter"
    analogWrite(yellowLedOut,configuration.brightYellow);  //Set yellow LED on.   
    analogWrite(cpaLightSourceOut,configuration.CPAtestSourcePower);    
    
    if((configuration.CPASensor1Min < analogRead(cpaInPin1)) //Test CPA
     &&(configuration.CPASensor1Max > analogRead(cpaInPin1))
     &&(configuration.CPASensor2Min < analogRead(cpaInPin2))
     &&(configuration.CPASensor2Max > analogRead(cpaInPin2))) {
      analogWrite(blueLedOut,configuration.brightBlue);
      digitalWrite(cpaTestResultOut, HIGH);
      }
    else {
      analogWrite(blueLedOut,0);    
      digitalWrite(cpaTestResultOut, LOW);
      }
    
    if(errorLevel < 0) {                         //on OK
      digitalWrite(sealTestResultOut, LOW);
      analogWrite(greenLedOut, configuration.brightGreen);
      continue;
      }
      
    if(errorLevel > 0 ) {                        //on NOK  
      digitalWrite(sealTestResultOut, HIGH);
      if(greenLedBlinkTimer > millis()) continue; //nothing to change yet.
      if(greenLEDIsOn) {                          //toggle LED
        analogWrite(greenLedOut, 0); 
        greenLEDIsOn = false;
        }
      else {
        analogWrite(greenLedOut, configuration.brightGreen); 
        greenLEDIsOn = true;
        }  
      if (errorLevel == 1) greenLedBlinkTimer = millis() + 200;  //fast blinking (minimum pressure reached too fast)
      if (errorLevel == 2) greenLedBlinkTimer = millis() + 400;  //slower blinking (minimum pressure not reached)       
      if (errorLevel == 3) greenLedBlinkTimer = millis() + 800; //slow blinking (error in drop-out phase)
      continue;
      }
      
    if(startProbeLastState == HIGH) {            //Just inserted?
      startProbeLastState = LOW;                 //Actualize old probe status.                       
      testStartTime = lasttime = millis();       //Store time of start of test
      readyForTest = false;
      Serial.println(":test:start");
      }
      
    if (testStartTime == 0) continue;            //Test not started yet
    
    if((testStartTime + configuration.startUpDelay) > millis()) continue; //Start-up delay not gone yet
       
    pressure = getPressure(false);   
       
    if((pressure > configuration.testPressure)&&(runUpFinishedTime == 0)) {     //Minimum pressure just reached.  
      digitalWrite(testValveOut, LOW);                            //Close filling valve.       
      runUpFinishedTime = millis();                               //Store run-up phase finished time
      Serial.println(":test:level_reached");
      if((testStartTime + configuration.startUpDelay + configuration.runUpMinTime) > runUpFinishedTime) { //Minimum pressure reached too fast
        errorLevel = 1;                                           //Set error level to 1 (minimum pressure reached too fast)
        Serial.println(":test:failed:level_reached_too_fast");
        continue;
        }
             
      if(measPoints[0].time == 0){                                //Just run-up phase is tested.
        errorLevel = -1;                                          //Test passed.
        Serial.println(":test:passed");
        continue;
        }
        testStage = 1;                                              //Test stage 1
      }           
        
    if((testStage == 0) && (digitalRead(testValveOut) == LOW)) { //Run-up not finished yet => filling-up
      digitalWrite(testValveOut, HIGH);
      Serial.println(":test:filling_started");  
      }

    if((lasttime + 10) < millis()) {
       lasttime = millis();
       Serial.print(":test:pressure:");
       Serial.println(pressure);
       } 
        
    if(((testStartTime + configuration.startUpDelay + configuration.runUpTimeout) < millis())&&(runUpFinishedTime == 0)){  //Time for reach minimum pressure is gone
      digitalWrite(testValveOut, LOW); //Close filling valve.
      errorLevel = 2;                   //Set error level to 2 (minimum pressure not reached)
      Serial.println(":test:failed:level_not_reached");
      continue;
      }                
       
    if(testStage == 0) continue; //Minimum pressure not reached yet.
    
    if((runUpFinishedTime + measPoints[testStage-1].time) > millis()) continue; //It is not time for measure yet   
    
    if((pressure < measPoints[testStage-1].minPressure) || (pressure > measPoints[testStage-1].maxPressure)){ //Test stage failed
      Serial.print(":test:stage_");
      Serial.print(testStage);
      Serial.println("_failed");
      Serial.println(":test:failed");
      errorLevel = 3;
      continue;  
      }
    else {
      Serial.print(":test:stage_");
      Serial.print(testStage);
      Serial.println("_passed");
      }
    testStage++; //next test stage
    
    if((testStage > 8) || (measPoints[testStage-1].time == 0)){  //All phases passed
      errorLevel = -1;                                           //Test passed.
      Serial.println(":test:passed");
      continue;
      }     
    }

If you can’t be bothered to post all your code I’m NOT going to help you.

Mark

PS And anyway as you don’t follow instrictions there would be no point!

M

Jenik5: Or do you know the reason?

Yes. You appear to be allergic to the... https://www.google.com/search?q=arduino+f+macro

Which, as you have discovered, has a rather nasty symptom.

Allergy cured, symptoms disappeared :-).

Thanks a lot…

You are welcome.