#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the LCD I2C address, number of columns and rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the pin for reading the water level sensor
int readPin = A0;
// Variable to store the read value from the sensor
int readValue;
// Define the pins for the buzzer, relay, and speaker
const int buzzerPin = 13;
const int relayPin = 9;
const int speakerPin = 10; // Add the speaker pin
// Flag to indicate if the "Water is full" message has been played
bool messagePlayed = false;
// Variable to store the start time of the buzzer sound
unsigned long buzzerStartTime = 0;
// Variable to count the number of times the buzzer has played
int buzzerCount = 0;
void setup() {
// Initialize the LCD and turn on the backlight
lcd.begin(16, 2);
lcd.backlight();
// Set the readPin as an input, and the buzzerPin, relayPin, and speakerPin as outputs
pinMode(readPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(speakerPin, OUTPUT); // Set the speaker pin as an output
// Initialize serial communication at 9600 baud
Serial.begin(9600);
}
void loop() {
// Clear the LCD screen
lcd.clear();
// Read the value from the water level sensor
readValue = analogRead(readPin);
// Map the read value to a percentage (0-100)
int waterLevelPercentage = map(readValue, 0, 1023, 0, 100);
// Display the water level percentage on the LCD
lcd.print("Water Level: ");
lcd.print(waterLevelPercentage);
lcd.print("%");
// Set the cursor to the second line of the LCD
lcd.setCursor(0, 1);
// Check the water level and display the corresponding message
if (waterLevelPercentage < 25) {
lcd.print("Low");
digitalWrite(relayPin, HIGH); // Turn on the relay (e.g. to fill the tank)
} else if (waterLevelPercentage < 99) {
lcd.print("Medium");
digitalWrite(relayPin, HIGH); // Keep the relay on
} else {
lcd.print("Full");
lcd.setCursor(0, 1);
lcd.print("Full"); // Display "The water is full" message
// Play the "Water is full" message only once
if (!messagePlayed) {
playVoiceMessage(speakerPin, "The water is full"); // Play the voice message
messagePlayed = true;
}
// Start playing the buzzer sound every 1 second, and play only 10 times
if (buzzerCount < 5) {
tone(buzzerPin, 2000, 400); // Play the buzzer sound for 100 ms
buzzerCount++;
delay(500); // Wait for 500 millisecond
} else {
noTone(buzzerPin); // Stop the buzzer sound
}
digitalWrite(relayPin, LOW); // Turn off the relay (e.g. to stop filling the tank)
}
// Wait for 1 second before taking the next reading
delay(1000);
}
// Function to play a voice message through the speaker
void playVoiceMessage(int speakerPin, String message) {
// Code to play the voice message through the speaker
// This will depend on the specific speaker and voice module being used
// For example, you could use a library like the "VarioSpeak" library
}