#include <LiquidCrystal_I2C.h>

#define trigPin 5
#define echoPin 6

LiquidCrystal_I2C lcd(0x27, 16, 2);

float measure_distance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  int duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}

void print_padding(String text, int padding) {
  lcd.print(text);
  for(int i = 0; i < padding-text.length(); i++) {
    lcd.print(" ");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.print("Jarak : ");
  lcd.setCursor(0, 1);
  lcd.print("Benda");
}

void loop() {
  float distance = measure_distance();
  String unit = "cm";
  Serial.println(String(distance) + "cm");
  if(distance >= 100) {
    unit = "m";
  }
  lcd.setCursor(9, 0);
  print_padding((unit.startsWith("cm") ? String(distance, 0) : String(distance / 1000, 2)) + unit, 10);
}
$abcdeabcde151015202530fghijfghij