#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LiquidCrystal_I2C.h>
// OLED water level display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// LCD for temperature
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address 0x27
// Ultrasonic pins
const int trigPin = 5;
const int echoPin = 18;
// Tank dimensions
const int tankHeight = 400; // cm
// LED pins
int ledPins[] = {13, 12, 14, 27}; // Green, Yellow, Red, White
// Buzzer pin
const int buzzerPin = 26;
// NTC temperature sensor pin
const int tempPin = 34;
void setup() {
Serial.begin(115200);
// LEDs
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Buzzer
pinMode(buzzerPin, OUTPUT);
// Ultrasonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED failed");
for (;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// LCD init
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp Display Ready");
delay(1000);
lcd.clear();
}
void loop() {
// Ultrasonic distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2; // cm
// Water level
int waterLevel = tankHeight - distance;
if (waterLevel < 0) waterLevel = 0;
if (waterLevel > tankHeight) waterLevel = tankHeight;
int percentage = map(waterLevel, 0, tankHeight, 0, 100);
int adcValue = analogRead(tempPin);
float temperature = (1.0 / ((log(1.0 / (4095.0 / adcValue - 1.0)) / 3950.0) + (1.0 / (25.0 + 273.15)))) - 273.15;
for (int i = 0; i < 4; i++) digitalWrite(ledPins[i], LOW);
if (percentage > 0 && percentage <= 25) {
digitalWrite(ledPins[0], HIGH);
} else if (percentage > 25 && percentage <= 50) {
digitalWrite(ledPins[1], HIGH);
} else if (percentage > 50 && percentage <= 75) {
digitalWrite(ledPins[2], HIGH);
} else if (percentage > 75 && percentage < 100) {
digitalWrite(ledPins[3], HIGH);
} else if (percentage >= 100) {
bool blinkState = (millis() / 500) % 2;
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], blinkState);
}
digitalWrite(buzzerPin, blinkState);
} else {
digitalWrite(buzzerPin, LOW);
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Water Level Monitor");
display.setTextSize(2);
display.setCursor(0, 20);
display.print("Level: ");
display.print(percentage);
display.println("%");
display.display();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature, 1);
lcd.print(" C");
//if you ever read this then well all the sentences given below are taken from Differnt Songs
//just an Easter Egg
lcd.setCursor(0, 1);
if (temperature < 10) {
lcd.print("Life's too Short");
} else if (temperature <= 15) {
lcd.print("This is all a situation");
} else if (temperature <= 28) {
lcd.print("The Best that Never was");
} else if (temperature <= 36){
lcd.print("No Rival")
} else if (temperature <= 46){
lcd.print("Everybody want some but can't handle when I brought 'em")
} else if (temperature <= 58 ){
lcd.print("Sheesh")
} else if(temperature <=70 ){
lcd.print("Too Hot, Too Hot")
} else {
lcd.print("Life's too short")
}
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Water: ");
Serial.print(percentage);
Serial.print("% | Temp: ");
Serial.print(temperature);
Serial.println(" C");
delay(1000);
}