#include <LiquidCrystal.h>
#define PIN_TRIG 26
#define PIN_ECHO 25
#define LOWLED 18
#define MIDLED 19
#define HIGHLED 21
unsigned int level = 0;
// Initialize the LCD display with the number of columns and rows
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Adjust the pin numbers as per your wiring
void setup() {
pinMode(LOWLED,OUTPUT);
pinMode(MIDLED,OUTPUT);
pinMode(HIGHLED,OUTPUT);
digitalWrite(LOWLED,HIGH);
digitalWrite(MIDLED,HIGH);
digitalWrite(HIGHLED,HIGH);
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
// Initialize the LCD display with the number of columns and rows
lcd.begin(16, 2);
}
void loop() {
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
level = duration / 58;
if (level < 100)
{
digitalWrite(LOWLED, LOW);
digitalWrite(HIGHLED, HIGH);
digitalWrite(MIDLED, HIGH);
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("Low level"); // Display the message
}
else if ((level > 200) && (level < 400))
{
digitalWrite(LOWLED, HIGH);
digitalWrite(HIGHLED, HIGH);
digitalWrite(MIDLED, LOW);
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("Mid level"); // Display the message
}
else if (level >= 400 )
{
digitalWrite(HIGHLED, LOW);
digitalWrite(MIDLED, HIGH);
digitalWrite(LOWLED, HIGH);
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("High level"); // Display the message
}
delay(1000);
}