Serial.println

Hello everyone. Does any of you what does this mean?

Serial.println("Out of range")

and this?

Serial.println("cm")

Out of range = A value that falls outside a specified minimum and maximum cm = centimeters

Serial.println("Out of range")Prints the text "Out of range" to the serial interface followed by a linefeed.

I guess that you can work out what the second statement does.

If you are asking when and why the messages are printed then we will need to see the complete program.

RT(F)M

Thank you so much. I would like to know, if possible, why do the Serial.println(“Out of range”) and the Serial.println(“cm”) happen. Here I post the full code

#define trigPin 12
#define echoPin 6
#define led 8
#define rele 10

void setup() {
 
  Serial.begin (9600); 
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(rele, OUTPUT);  

}

void loop() {
 
  long duration, distance; 
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW); 
   duration = pulseIn(echoPin, HIGH); 
   distance = (duration/2) / 29.1; 
  Serial.println(distance); 
     if(distance > 40)  { 
      digitalWrite(led, HIGH); 
      digitalWrite(rele, LOW); 
     }
     else { 
      digitalWrite(led, LOW); 
      digitalWrite(rele, HIGH); 
     }
     if (distance >= 200 || distance <=0)  { 
      Serial.println("Out of range"); 
     }
     else { 
      Serial.print(distance);
      Serial.println("cm"); 
}

I would like to know, if possible, why do the Serial.println("Out of range") and the Serial.println("cm") happen.

Not sure what you mean but....

The "Out of Range" prints when the distance is either over 200 or under or equals 0. The || means "or"

The distance and the "cm" print otherwise, because of the word else.

If that doesn't answer the question, sorry....

Ku99: Thank you so much. I would like to know, if possible, why do the Serial.println("Out of range") and the Serial.println("cm") happen.

They happen because the programmer wanted them to happen. As they are just printed reports, they don't affect the operation of the program.

Therefore, they aren't needed?

Ku99: Therefore, they aren't needed?

They, and the entire if-else structure around them are not needed, if all you want to do is flash the LED.

Ku99: Therefore, they aren't needed?

How can we say? It's not our program, it's yours: only you know what you want it to do.

Ku99: Therefore, they aren't needed?

why don't you just comment that code and try to run the code..