#include<LiquidCrystal_I2C.h>
class UltrasonicSensor {
private:
int trigPin;
int echoPin;
long duration;
int distance;
public:
// Constructor
UltrasonicSensor(int trigPin, int echoPin) : trigPin(trigPin), echoPin(echoPin) {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
// Function to trigger the sensor and calculate distance
void measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
}
// Function to get the measured distance
int getDistance() {
return distance;
}
};
// Define Pins
const int TRIGGPIN1 = 4;
const int TRIGGPIN2 = 19;
const int ECHOPIN1 = 5;
const int ECHOPIN2 = 18;
const int LED1 = 13;
const int LED2 = 12;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Create instances of the UltrasonicSensor class
UltrasonicSensor sensor1(TRIGGPIN1, ECHOPIN1);
UltrasonicSensor sensor2(TRIGGPIN2, ECHOPIN2);
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight(); // Turn on the blacklight
intro();
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.println("Test Sequence Initiating");
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED1, LOW);
delay(500);
digitalWrite(LED2, LOW);
delay(500);
lcd.clear();
}
void loop() {
// Measure distance for each sensor
sensor1.measureDistance();
sensor2.measureDistance();
// Get the measured distance for each sensor
int distance1 = sensor1.getDistance();
int distance2 = sensor2.getDistance();
// Print distances
Serial.print("Distance1: ");
Serial.print(distance1);
Serial.println(" cm");
Serial.print("Distance2: ");
Serial.print(distance2);
Serial.println(" cm\n");
//Print distances in LCD
distance();
lcd.setCursor(11, 0);
lcd.print(distance1);
lcd.setCursor(11, 1);
lcd.print(distance2);
// Control LEDs based on distance
digitalWrite(LED1, distance1 < 100 ? HIGH : LOW);
digitalWrite(LED2, distance2 < 100 ? HIGH : LOW);
delay(1000); // Adjust delay as needed
}
void intro() {
//Introductary Screen
lcd.println("WATER MONITORING");
lcd.setCursor(0, 1);
lcd.println("SYSTEM:1.0");
}
void distance() {
//Distance Mesurement screen
lcd.setCursor(0, 0);
lcd.print("Distance 1: cm");
lcd.setCursor(0, 1);
lcd.print("Distance 2: cm");
}