Padding 0s to print in LCD

void writeValue(int val1, int val2 ){
  lcd.setCursor(0,0); /* Set cursor to column 0 row 0 */
  lcd.print("Water Level"); /* Print data on display */
  lcd.setCursor(12,0);   // Set cursor to next row
  lcd.print(String(val1) + "%"); // Print value of water filled in %
  lcd.setCursor(0,1);
  lcd.print("Estd Time");
  lcd.setCursor(10,1);
  lcd.print(String(val2)); // Print value of time remaining in mins
  lcd.setCursor(15,1);
  lcd.print("m");
}

This is the code which I am presently using for printing in LCD display.

The output which gets printed is:

Water Level 40%
Estd Time 6 m

But I want the output to look as:

Water Level 40%
Estd Time 006 m

or

Water Level 40%
Estd Time 026 m

i.e. padding the int val2 with 0s such that there are 3 digits in the display.

Can anybody help me with this?

Can simply do it like this with if else statements.

if (String(value).length() < 3) {
  if (String(value).length() < 2) {
    lcd.print("00"+String(value));
  }
  else {
    lcd.print("0"+String(value));
  }
}
else {
  lcd.print(String(value));
}
2 Likes

Thanks as lot.

No problem. If that is the solution, you can press the solution button on the post to mark it as solved.

How about this? It avoids the use of nasty Strings.

void writeValue(int val1, int val2 )
{
   lcd.setCursor(0, 0); /* Set cursor to column 0 row 0 */
   char buf[16];
   lcd.setCursor(0, 0);
   sprintf(buf, "Water Level %d%% ", val1);
   lcd.print(buf);
   lcd.setCursor(0, 1);
   sprintf(buf, "Estd Time %03dm", val2);
   lcd.print(buf);
}

why do you need these casts to Strings?

on an UNO I would just add zeros if needed.

void writeValue(int val1, int val2 ) {
  lcd.setCursor(0, 0); /* Set cursor to column 0 row 0 */
  lcd.print("Water Level"); /* Print data on display */
  lcd.setCursor(12, 0);  // Set cursor to next row
  // Print value of water filled in %
  if (val1 < 10) lcd.print("00");
  else if (val1 < 100) lcd.print('0');
  lcd.print(val1);
  lcd.print('%');   
  lcd.setCursor(0, 1);
  lcd.print("Estd Time");
  lcd.setCursor(10, 1);
  lcd.print(val2); // Print value of time remaining in mins
  lcd.setCursor(15, 1);
  lcd.print('m');
}

next step: F-Makro for the longer text

void writeValue(int val1, int val2 ) {
  lcd.setCursor(0, 0); /* Set cursor to column 0 row 0 */
  lcd.print(F("Water Level")); /* Print data on display */
  lcd.setCursor(12, 0);  // Set cursor to next row
  // Print value of water filled in %
  if (val1 < 10) lcd.print("00");
  else if (val1 < 100) lcd.print('0');
  lcd.print(val1);
  lcd.print('%');   
  lcd.setCursor(0, 1);
  lcd.print(F("Estd Time"));
  lcd.setCursor(10, 1);
  lcd.print(val2); // Print value of time remaining in mins
  lcd.setCursor(15, 1);
  lcd.print('m');
}

If you want an easy way to use xxprintf() formatting you could add this to your sketch:

#ifndef PPRINTF_BUFSIZE
#define PPRINTF_BUFSIZE 64
#endif
size_t Pprintf(Print &outdev, const char *format, ...)
{
char buf[PPRINTF_BUFSIZE];
	va_list ap;
	va_start(ap, format);
	vsnprintf(buf, sizeof(buf), format, ap);
	va_end(ap);
	return(outdev.write(buf));
}

It adds a Pprintf() function that can do printf()/fprintf() style output that you can direct to the Print class object of your choice.
i.e. you can direct it to your lcd object to send the output to the LCD.

Then you could do the output like this:

void writeValue(int val1, int val2 ){
  lcd.setCursor(0,0); /* Set cursor to column 0 row 0 */
  Pprintf(lcd, "Water Level %02d%%", val1); /* Print data on display */
  lcd.setCursor(0,1);
  Pprintf(lcd, "Estd Time %03d m", val2);  // Print value of time remaining in mins
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.