!Serial Monitor causes lagging

I want to control a LED and also print control value to serial, but if I close the serial monitor there's a lagging. I tried with if (serial), but it didn't work. Any suggestions?

      
     ledValue=map(fadeValue,0,1023,0,255);
    analogWrite(ledPin,ledValue );
  if (Serial) { 
    Serial.print("Värde: ");
    Serial.print(ledValue);  
    Serial.print(" "); 
    Serial.println(fadeValue); 
    }
    delay (50); ```

Hello iderik
Does the compiler spent some warings?
If not than configure this inside the IDE.

grafik

Have a nice day and enjoy coding in C++.

I followed your complier setup and got no errors anyway. The compiler says OK. When I run the program everything is fine and the LED brightness is controlled smoothly, also when i open the serial monitor. The lagging starts when close the monitor. If I open the monitor again, everything's fine.

Please post all your code... very hard to debug with just a snippet.

Hello iderik
In this case publish the current sketch to see what happens or not.
Have a nice day and enjoy coding in C++.

p.s. red_car was faster

int sensorPin = A0; 
int fadeValue = 0 ;
int ledValue = 0 ;
void setup() {
  // put your setup code here, to run once:
    // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:      fadeValue = analogRead(sensorPin);
     fadeValue= analogRead(sensorPin);
     ledValue=map(fadeValue,0,1023,0,255);
    analogWrite(ledPin,ledValue );
  if (Serial) { 
    Serial.print("Värde: ");
    Serial.print(ledValue);  
    Serial.print(" "); 
    Serial.println(fadeValue); 
    }
    delay (50);```

}

Which board are you using?

Arduino Micro.
The if(Serial) {} command makes no difference.

Hello iderik
Try

if (Serial.available())

Have a nice day and enjoy coding in C++.

For writing?

I haven't seen if (Serial) used like this before.

If you remove this if statement what happens?

Hello red_car
I assume that the values should only be output when an input is made.
Have a nice day and enjoy coding in C++.

No I don't think so. Serial.available() would be used in that case as per your previous post.

if(Serial) is used to check the serial port is open... typically after Serial.begin()

The Arduino Micro uses a ATMega32U4 processor which has a native USB interface. In contrast to e.g. a UNO the processor recognizes, when the USB connection gets broken ( closing the serial monitor ) and gets stuck with a timeout.

Hello red_car
Many thanks for the advice.
Have a nice day and enjoy coding in C++.

That explains why, thank you!

The solution is to use Serial.availableForWrite() and check if there is enough space to print what you want to print. See Serial.print on Leonardo very slow after disconnection from serial - #7 by sterretje and Serial.print on Leonardo very slow after disconnection from serial - #10 by sterretje for two examples.

if(Serial) and the variations only check if a connection has been established, not if the connection disappears. It's mostly important if you don't want to 'loose' the first print statements. To my knowledge, printing / writing is basically ignored if the connection was not yet established; the problem comes when it disappears.

Thanks, good to know :sunglasses:

Excellent! Works perfect.

void loop() {
  // put your main code here, to run repeatedly:      fadeValue = analogRead(sensorPin);
     fadeValue= analogRead(sensorPin);
     ledValue=map(fadeValue,0,1023,0,255);
    analogWrite(ledPin,ledValue );
  if (Serial.availableForWrite()) { 
    Serial.print("Värde: ");
    Serial.print(ledValue);  
    Serial.print(" "); 
    Serial.println(fadeValue); 
  }
    
    delay (50);

}

I suspect that you actually have to test for the size that you want to send; if you want to send e.g. 10 characters but there is only place for 1, you might still run into the problem.