#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int trigPin = 9;
const int echoPin = 10;
const int relayPin = 2;
const int mutePin = 3; // Define the pin for the mute switch
const int buzzerPin = 4; // Define the pin for the buzzer
const int RDP = 13;
const int darkYellowLEDPin = 6;
const int yellowLEDPin = 5;
const int greenLEDPin = 8;
const int blueLEDPin = 7;
bool mute = false;
bool lastButtonState = HIGH; // Previous state of the button
unsigned long lastDebounceTime = 0; // Last time the output pin was toggled
unsigned long debounceDelay = 50; // The debounce time; increase if the output flickers
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(mutePin, INPUT_PULLUP); // Set the mute switch pin as input with pull-up resistor
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
pinMode(RDP, OUTPUT);
pinMode(darkYellowLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
Serial.begin(9600);
if (!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Display1 allocation failed"));
Serial.println(F("Hello World 1"));
for(;;);
}
display1.display();
delay(2000);
display1.clearDisplay();
}
void loop() {
long duration;
int distance;
int percent;
// Check the state of the mute switch with debounce
int reading = digitalRead(mutePin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed
if (reading == LOW && !mute) {
mute = true; // Set the mute state
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
noTone(buzzerPin); // Ensure no tone is playing
}
}
lastButtonState = reading;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
percent = 100 - map(distance, 0, 400, 0, 100); // Mapping distance to percentage (0-100%)
display1.clearDisplay();
// Print water level percentage
display1.setTextSize(1);
display1.setTextColor(SSD1306_WHITE);
display1.setCursor(9.5, 9);
display1.print("WATER LEVEL \n");
display1.print("--------------");
display1.print("\n");
//SCREEN_WIDTH 128 SCREEN_HEIGHT 64
// Draw a simple bar graph representing the water level
int barHeight = map(percent, 0, 100, 0, SCREEN_HEIGHT - 15); // Adjusted height
int barWidth = 35;
int barX = 90;
int barY = 10; // Adjusted starting point
// Calculate top of the bar
int barTop = barY + (SCREEN_HEIGHT - 14.1 - barHeight);
// Ensure bar height does not exceed screen height
if (barHeight > (SCREEN_HEIGHT - 14.1)) {
barHeight = SCREEN_HEIGHT - 14.1;
}
// Draw outer border rectangle
display1.drawRect(barX - 1, barY - 1, barWidth + 2, SCREEN_HEIGHT - 16 + 2, SSD1306_WHITE);
// Draw filled inner rectangle representing the bar
display1.fillRect(barX, barTop, barWidth, barHeight,SSD1306_WHITE);
// Draw the dashed border box for percentage
int boxWidth = 80;
int boxHeight = 36;
int boxX = 2;
int boxY = SCREEN_HEIGHT - boxHeight - 5;
// Draw top and bottom borders with dashes
for (int i = boxX; i < boxX + boxWidth; i += 4) {
display1.drawPixel(i, boxY, SSD1306_WHITE);
display1.drawPixel(i + 1, boxY, SSD1306_WHITE);
display1.drawPixel(i, boxY + boxHeight, SSD1306_WHITE);
display1.drawPixel(i + 1, boxY + boxHeight, SSD1306_WHITE);
}
// Draw left and right borders
for (int i = boxY; i < boxY + boxHeight; i += 4) {
display1.drawPixel(boxX, i, SSD1306_WHITE);
display1.drawPixel(boxX, i + 1, SSD1306_WHITE);
display1.drawPixel(boxX + boxWidth, i, SSD1306_WHITE);
display1.drawPixel(boxX + boxWidth, i + 1, SSD1306_WHITE);
}
// Display the percentage inside the box
display1.setCursor(boxX + 7, boxY + 8);
display1.setTextSize(3);
display1.print(percent);
display1.print("%");
// Auto-Mode
unsigned int frequencies[] = {500, 400, 500, 400, 500, 400, 500, 400, 500, 400, 500, 400, 500, 400, 500, 400};
unsigned int frequencies1[]={1000,2000,1000,2000,1000,2000,1000,2000,1000,2000,1000,2000,1000,2000,1000,2000};
if (percent >= 95) {
if (!mute) {
digitalWrite(buzzerPin, HIGH);
for (int i = 0; i < 16; i++) {
tone(buzzerPin, frequencies1[i], 250);
delay(100);
}
}
}
else if (percent <= 12) {
if (!mute) {
digitalWrite(buzzerPin, HIGH);
// Turn on the buzzer if not muted
for (int i = 0; i < 16; i++) {
tone(buzzerPin, frequencies[i], 250);
delay(100);
}
}
}
else {
mute = false; // Reset the mute state when outside the range
noTone(buzzerPin);
digitalWrite(buzzerPin, LOW); // Ensure the buzzer is off
}
if (percent <= 15) {
digitalWrite(relayPin, HIGH);
if (percent>=15 && percent<=90) {
digitalWrite(relayPin, HIGH);
}
}
if (percent > 90) {
digitalWrite(relayPin, LOW);
}
display1.display();
delay(1000);
// Update LED states progressively
digitalWrite(RDP, percent > 0);
digitalWrite(darkYellowLEDPin, percent > 25);
digitalWrite(yellowLEDPin, percent > 48);
digitalWrite(greenLEDPin, percent > 68);
digitalWrite(blueLEDPin, percent > 90);
}