#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <Wire.h> // Include the Wire library for I2C communication
#include <Adafruit_SSD1306.h> // Include the Adafruit SSD1306 library for OLED display
#include <DHT.h> // Include the DHT library for DHT22 sensor
#include <NewPing.h> // Include the NewPing library for ultrasonic sensor
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET); // Define OLED display object
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); // Define DHT sensor object
#define TRIGGER_PIN 9
#define ECHO_PIN 10
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Define ultrasonic sensor object
long duration, inches;
int set_val, percentage;
bool state, pump;
void setup() {
Serial.begin(9600);
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Set up the text size and color
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
// Initialize the DHT sensor
dht.begin();
// Initialize the water level monitoring system
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(12, OUTPUT);
lcd.begin(16, 2);
lcd.print("WATER LEVEL:");
lcd.setCursor(0, 1);
lcd.print("PUMP:OFF MANUAL");
set_val = EEPROM.read(0);
if(set_val > 150) set_val = 150;
}
void loop() {
// Read temperature and humidity from DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature and humidity on OLED display
display.clearDisplay();
display.setCursor(0,0);
display.print("Temp: ");
display.println(temperature);
display.print("Humidity: ");
display.println(humidity);
display.display();
// Read water level from ultrasonic sensor
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
inches = microsecondsToInches(duration);
// Calculate percentage of water level
percentage = (set_val - inches) * 100 / set_val;
// Display water level percentage on LCD
lcd.setCursor(12, 0);
if(percentage < 0) percentage = 0;
lcd.print(percentage);
lcd.print("% ");
// Check for leaks or abnormal water flow
if(inches == 0 || inches >= MAX_DISTANCE) {
// Leak or abnormal flow detected, activate alarm and shut off pump
digitalWrite(12, LOW); // Turn off pump
tone(13, 1000); // Activate buzzer
} else {
// Normal water level, deactivate alarm and pump as necessary
noTone(13); // Turn off buzzer
if(percentage < 30 && digitalRead(11)) {
pump = true;
} else {
pump = false;
}
digitalWrite(12, pump ? HIGH : LOW); // Control the pump
}
// Save new set_val if manual switch pressed
if(digitalRead(10) == LOW && !state) {
state = true;
set_val = inches;
EEPROM.write(0, set_val);
} else if(digitalRead(10) == HIGH) {
state = false;
}
// Enter low-power mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}