user67906: I want to print a single line including the variable "val" only once rather than looping or repeating of the same line and within it the val will change according to the reading. Is it possible? If so, how?

#define trigPin 5
#define echoPin 6

float distance(uint8_t T, uint8_t H);

void setup()
{
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Serial.begin(9600);
    Serial.print("The object is: ");
}

void loop()
{
    float val = distance(32, 68);
    Serial.print(val);
    Serial.println(" cm away");
    delay(500);
}


user7508: 
within it [the already printed line] the val will change according to
the reading

This “within it” makes little sense. “Printing” is just sending bytes
through the serial port, and you don't choose where these bytes are
going: you just send them one at a time. The program you use as a
“serial port monitor” then displays those bytes in whatever manner it
sees fit. You cannot control from the Arduino where things get
displayed.
Or maybe you can...
If your monitor program can emulate an old school terminal, then
it can understand special control sequences, including escape codes,
that instruct it to perform specific actions, such as moving the cursor
or erasing part of the screen. Suitable terminal emulators include
screen on Linux and MacOS, and Putty on Windows. You may find
something appropriate for your mobile platform. Notably, the Arduino
serial monitor is not a terminal emulator, and doesn't interpret those
control sequences.
Assuming you are using a suitable terminal emulator, the simplest
solution to your problem is to just repeat the whole sentence “The
object is: xx.xx cm away” for every measurement, but do so by
overwriting the current line instead of moving to the next one. You thus
replace Serial.println() by Serial.print(), and precede each
sentence by a carriage return character (ASCII CR = 0x0d = '\r'),
which is interpreted as the command “move the cursor to the start of the
line”:
void loop() {
    float val = distance(32, 68);
    Serial.print("\rThe object is: ");  // note the '\r'
    Serial.print(val);
    Serial.print(" cm away");  // no println()
    delay(500);
}

There is a caveat here. If you overwrite a sentence (say “The object is:
10.07 cm away”) by a shorter one (“The object is: 9.92 cm away”, one
character less), the previous sentence will not be completely
overwritten, so you may end up with “awayy” showing on the display. This
can be overcome by ensuring a fixed-length formatting of the numbers
displayed, by padding with spaces, or by sending an “erase current line”
command to the terminal.
As a side note, what Serial.println() does is print what you
requested, followed by a CR character, and then a line feed character
(ASCII LF = 0x0a = '\n'). The line feed is interpreted by the
terminal as the command “move the cursor to the next line, without
changing its horizontal position”.
If you find it annoying to repeatedly print “The object is:”, you can
instead use more advanced control sequences. For instance:

“<ESC>[16G” means “move the cursor to column 16”
“<ESC>[K” means “clear from the cursor to the end of the line”

where <ESC> is the escape character:
void loop() {
    float val = distance(32, 68);
    Serial.print("\x1b[16G\x1b[K");  // "\x1b" is the escape character
    Serial.print(val);
    Serial.print(" cm away");
    delay(500);
}


67906: <p>I want to print a single line including the variable &quot;val&quot; only once rather than looping or repeating of the same line and within it the val will change according to the reading. Is it possible? If so, how?</p>
<p><img src="https://i.stack.imgur.com/FQGjo.jpg" alt="enter image description here" /></p>
<pre class="lang-cpp prettyprint-override"><code>#define trigPin 5
#define echoPin 6

float distance(uint8_t T, uint8_t H);

void setup()
{
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Serial.begin(9600);
    Serial.print(&quot;The object is: &quot;);
}

void loop()
{
    float val = distance(32, 68);
    Serial.print(val);
    Serial.println(&quot; cm away&quot;);
    delay(500);
}
</code></pre>
 7508: <blockquote>
<p>within it [the already printed line] the val will change according to
the reading</p>
</blockquote>
<p>This “within it” makes little sense. “Printing” is just sending bytes
through the serial port, and you don't choose where these bytes are
going: you just send them one at a time. The program you use as a
“serial port monitor” then displays those bytes in whatever manner it
sees fit. You cannot control from the Arduino where things get
displayed.</p>
<p>Or maybe you can...</p>
<p>If your monitor program can emulate an <a href="https://en.wikipedia.org/wiki/Computer_terminal" rel="nofollow noreferrer">old school terminal</a>, then
it can understand special control sequences, including <a href="https://en.wikipedia.org/wiki/ANSI_escape_code" rel="nofollow noreferrer">escape codes</a>,
that instruct it to perform specific actions, such as moving the cursor
or erasing part of the screen. Suitable terminal emulators include
<a href="https://www.gnu.org/software/screen/" rel="nofollow noreferrer">screen</a> on Linux and MacOS, and <a href="https://www.putty.org/" rel="nofollow noreferrer">Putty</a> on Windows. You may find
something appropriate for your mobile platform. Notably, the Arduino
serial monitor is <em>not</em> a terminal emulator, and doesn't interpret those
control sequences.</p>
<p>Assuming you are using a suitable terminal emulator, the simplest
solution to your problem is to just repeat the whole sentence “The
object is: xx.xx cm away” for every measurement, but do so by
overwriting the current line instead of moving to the next one. You thus
replace <code>Serial.println()</code> by <code>Serial.print()</code>, and precede each
sentence by a carriage return character (ASCII CR = <code>0x0d</code> = <code>'\r'</code>),
which is interpreted as the command “move the cursor to the start of the
line”:</p>
<pre class="lang-cpp prettyprint-override"><code>void loop() {
    float val = distance(32, 68);
    Serial.print(&quot;\rThe object is: &quot;);  // note the '\r'
    Serial.print(val);
    Serial.print(&quot; cm away&quot;);  // no println()
    delay(500);
}
</code></pre>
<p>There is a caveat here. If you overwrite a sentence (say “The object is:
10.07 cm away”) by a shorter one (“The object is: 9.92 cm away”, one
character less), the previous sentence will not be completely
overwritten, so you may end up with “awayy” showing on the display. This
can be overcome by ensuring a fixed-length formatting of the numbers
displayed, by padding with spaces, or by sending an “erase current line”
command to the terminal.</p>
<p>As a side note, what <code>Serial.println()</code> does is print what you
requested, followed by a CR character, and then a line feed character
(ASCII LF = <code>0x0a</code> = <code>'\n'</code>). The line feed is interpreted by the
terminal as the command “move the cursor to the next line, without
changing its horizontal position”.</p>
<p>If you find it annoying to repeatedly print “The object is:”, you can
instead use more advanced control sequences. For instance:</p>
<ul>
<li>“&lt;ESC&gt;[16G” means “move the cursor to column 16”</li>
<li>“&lt;ESC&gt;[K” means “clear from the cursor to the end of the line”</li>
</ul>
<p>where &lt;ESC&gt; is the escape character:</p>
<pre class="lang-cpp prettyprint-override"><code>void loop() {
    float val = distance(32, 68);
    Serial.print(&quot;\x1b[16G\x1b[K&quot;);  // &quot;\x1b&quot; is the escape character
    Serial.print(val);
    Serial.print(&quot; cm away&quot;);
    delay(500);
}
</code></pre>
