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.
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');
}
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
}