user76504: I tried to modify the basic "hello-world" sketch for the LCD that is a part of the examples section in the IDE. I intended to make a countdown timer using the LCD.
The original code simply prints "Hello World" in the first line, and then makes use of the millis() function to print the time elapsed in the next line.
So I tried a simple modification to make it count down from 20. Instead of printing millis()/1000 , I printed 20-millis()/1000, and also added a while() to only print till millis()<=20,000. (so that it does not go beyond 00)
#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  
  lcd.begin(16, 2);
  
  lcd.print("time left");
}

void loop() {
  
  while(millis()<=20000){
    
    lcd.setCursor(0, 1);
    
    lcd.print(20- millis() / 1000);
  }
  

}

It seemed to work till 10. but instead of 9, it printed 90, and likewise 80,70.. for 8,7...etc till 00.
How can I get rid of these extra zeros?

user7508: 
instead of 9, it printed 90

No, it didn't: it printed “9”. However, it did not erase the previous
value before printing “9”. Before printing the 9, the LCD had:
┌────────────────┐
│time left       │
│10              │
└────────────────┘
 ^cursor position

When you printed “9”, that character replaced the “1”, and you end up
seeing “90”.
The fix, as pointed out by Juraj, is to add a space after printing the
numbers.
An alternative is to prepend a space (or a zero), but only when the
number to be displayed is less that 10:
int count = 20 - millis() / 1000;
if (count < 10)
    lcd.print(' ');  // alternatively: '0'
lcd.print(count);


76504: <p>I tried to modify the basic &quot;hello-world&quot; sketch for the LCD that is a part of the examples section in the IDE. I intended to make a countdown timer using the LCD.</p>
<p>The original code simply prints &quot;Hello World&quot; in the first line, and then makes use of the <code>millis()</code> function to print the time elapsed in the next line.</p>
<p>So I tried a simple modification to make it count <em>down</em> from 20. Instead of printing <code>millis()/1000</code> , I printed <code>20-millis()/1000</code>, and also added a <code>while()</code> to only print till <code>millis()&lt;=20,000</code>. (so that it does not go beyond <code>00)</code></p>
<pre><code>#include &lt;LiquidCrystal.h&gt;


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  
  lcd.begin(16, 2);
  
  lcd.print(&quot;time left&quot;);
}

void loop() {
  
  while(millis()&lt;=20000){
    
    lcd.setCursor(0, 1);
    
    lcd.print(20- millis() / 1000);
  }
  

}
</code></pre>
<p>It seemed to work till <code>10</code>. but instead of <code>9</code>, it printed <code>90</code>, and likewise <code>80,70..</code> for <code>8,7...</code>etc till <code>00</code>.</p>
<p><strong>How can I get rid of these extra zeros?</strong></p>
 7508: <blockquote>
<p>instead of <code>9</code>, it printed <code>90</code></p>
</blockquote>
<p>No, it didn't: it printed “9”. However, it did not <em>erase</em> the previous
value before printing “9”. Before printing the 9, the LCD had:</p>
<pre><code>┌────────────────┐
│time left       │
│10              │
└────────────────┘
 ^cursor position
</code></pre>
<p>When you printed “9”, that character replaced the “1”, and you end up
seeing “90”.</p>
<p>The fix, as pointed out by Juraj, is to add a space after printing the
numbers.</p>
<p>An alternative is to prepend a space (or a zero), but only when the
number to be displayed is less that 10:</p>
<pre class="lang-cpp prettyprint-override"><code>int count = 20 - millis() / 1000;
if (count &lt; 10)
    lcd.print(' ');  // alternatively: '0'
lcd.print(count);
</code></pre>
