#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 16 column and 2 rows
long duration;
int distance;
const int trigPin = 9;
const int echoPin = 10;
const int buz = 8;
void Get_Dis()
{
//Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
return (distance);
}
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buz, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Arduino"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("GetStarted.com"); // print message at (2, 1)
delay(1000); // display the above for two seconds
Get_Dis();
lcd.clear(); // clear display
lcd.setCursor(3, 2); // move cursor to (3, 0)
lcd.println("Distance: ");
lcd.print(distance); // print message at (0, 1)
delay(1000);
if (distance<10 && distance>4)
{
for (int i=440; i<1000; i++)
{
tone(buz, i, 50);
delay(5);
}
for (int i=1000; i>440; i--)
{
tone(buz, i, 50);
delay(5);
}
}
}