#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); /*the pins that the lcd will use*/
#define PIN_TRIGGER 3
#define PIN_INPUT 2
void setup() {
Serial.begin(9600); /*setting the baud rate to be used (the rate at which the device outputs data)*/
pinMode(PIN_TRIGGER, OUTPUT); /*setting the trigger pin to cause the HC-SR04 to ouput a signal*/
pinMode(PIN_INPUT, INPUT); /*setting the input pin to display when the HC-SR04 detects a echo*/
/*setting up the lcd*/
lcd.setCursor(1,1); /*sets the start point for text to be outputted on the lcd*/
}
void loop() {
/*taking a mesurement*/
digitalWrite(PIN_TRIGGER, HIGH); /*writing a high voltage (5v) to the trigger pin, triggering an output*/
delayMicroseconds(10);
digitalWrite(PIN_TRIGGER, LOW); /*writing a low voltage (0v) to the trigger pin, to stop it outputting*/
/*reading the result of the mesurement*/
int duration_of_pulse = pulseIn(PIN_INPUT, HIGH);
/*converting the duration to distance using convertions on the HC-SR04 data sheet*/
int length_in_cm = duration_of_pulse / 58;
Serial.println(length_in_cm);
lcd.clear();
lcd.print(length_in_cm);
delay(1000);
}