int cm = 0;
#include <LiquidCrystal.h>
// initialize the library with the numbers of the
// interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// set the cursor to column 0, line 0
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("Praktik 08");
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
// Print a message to the LCD
lcd.print("Pengukur Jarak");
delay (1000);
// Clear LCD Display
lcd.clear();
}
void loop() {
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 7);
lcd.setCursor(0,0);
lcd.print("Jarak: ");
lcd.setCursor(6,0);
lcd.print(cm);
lcd.setCursor(9,0);
lcd.print("cm");
delay(100); // Wait for 100 millisecond(s)
}
long readUltrasonicDistance(int triggerPin, int
echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10
//microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave
//travel
// time in microseconds
return pulseIn(echoPin, HIGH);
}