#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;
// Define buzzer pin
const int buzzerPin = 6;
// Define main switch and switch pins
const int mainSwitchPin = 4; // Digital input pin for the main switch
const int switchPin = 5; // Digital input pin for the switch
const int ledPin = 7; // Digital output pin for the switch indicator LED
const int waterLevelLedPin = 8; // Digital output pin for the water level indicator LED
// Define the maximum and minimum distances for the tank
const int maxDistance = 70; // 48 cm as the maximum depth
const int minDistance = 25; // 25 cm as the minimum distance
bool buzzerActive = false;
bool introDisplayed = false; // Flag to track if the intro is displayed
void displayIntro() {
// Display the introductory text
lcd.init();
lcd.backlight();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("ANWIN V ANDREWS");
delay(2000); // Show for 2 seconds
lcd.clear();
introDisplayed = true; // Set the flag to indicate intro displayed
}
void setup() {
// Set up the main switch pin as an input
pinMode(mainSwitchPin, INPUT);
// Set up the ultrasonic sensor and alert pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Set up the switch pin as an input
pinMode(switchPin, INPUT);
// Set up the LED pins as outputs
pinMode(ledPin, OUTPUT);
pinMode(waterLevelLedPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read the state of the main switch
int mainSwitchState = digitalRead(mainSwitchPin);
if (mainSwitchState == HIGH) {
// Main switch is off, disconnect everything
introDisplayed = false; // Reset the intro flag
lcd.noBacklight(); // Turn off the LCD backlight
lcd.noDisplay(); // Turn off the LCD display
digitalWrite(ledPin, LOW); // LED off when the main switch is off
digitalWrite(buzzerPin, LOW); // Buzzer off when the main switch is off
buzzerActive = false;
digitalWrite(waterLevelLedPin, LOW); // Turn off the water level indicator LED
} else {
// Main switch is on, run the system
if (!introDisplayed) {
displayIntro(); // Display the intro scene when the main switch is turned on
}
lcd.backlight(); // Turn on the LCD backlight
lcd.display(); // Turn on the LCD display
// Read the state of the switch
int switchState = digitalRead(switchPin);
// Turn off the LED and buzzer when the switch is off
if (switchState == HIGH) {
digitalWrite(ledPin, LOW); // LED off when the switch is off
digitalWrite(buzzerPin, LOW); // Buzzer off when the switch is off
buzzerActive = false;
} else {
digitalWrite(ledPin, HIGH); // LED on when the switch is on
}
// Rest of the code remains the same
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the distance from the sensor
long duration = pulseIn(echoPin, HIGH);
int distance = duration / 58.2; // Convert the time to distance in cm
// Ensure the distance is within the specified range
distance = constrain(distance, minDistance, maxDistance);
// Calculate the water level percentage
int waterLevel = map(distance, minDistance, maxDistance, 100, 0);
waterLevel = constrain(waterLevel, 0, 100);
// Determine the water level status
String waterStatus;
if (waterLevel > 96) {
waterStatus = "Full";
digitalWrite(waterLevelLedPin, HIGH); // Turn on the water level indicator LED
if (switchState == LOW && !buzzerActive) {
// Turn on the buzzer when the switch is on
digitalWrite(buzzerPin, HIGH);
buzzerActive = true;
}
} else if (waterLevel < 30) {
waterStatus = "Low";
digitalWrite(waterLevelLedPin, LOW); // Turn off the water level indicator LED
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
buzzerActive = false;
} else {
waterStatus = "Medium";
digitalWrite(waterLevelLedPin, LOW); // Turn off the water level indicator LED
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
buzzerActive = false;
}
// Display the water level status as numbers and graphical representation
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(waterLevel);
lcd.print(" %");
lcd.setCursor(0, 1);
for (int i = 0; i < waterLevel / 5; i++) {
lcd.print("#");
}
lcd.setCursor(8, 0);
lcd.print(waterStatus);
delay(1000); // Update the display every second
}
}