#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 display(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
bool mute = false;
bool isButtonPressed= false;
bool isBeepOn = false;
unsigned long buttonPressTime = 0; // Last time the output pin was toggled
unsigned long lastBeepTime = 0; // 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
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
long duration;
int distance;
int percent;
// Check the state of the mute switch with debounce
int buttonState = digitalRead(mutePin);
unsigned long currentTime = millis();
if (buttonState == LOW && !isButtonPressed) {
isButtonPressed = true;
buttonPressTime = currentTime;
noTone(buzzerPin);
}
if(isButtonPressed){
if(currentTime-buttonPressTime < 10000){
noTone(buzzerPin);
}
else{
isButtonPressed = false;
}
}
else{
if(currentTime-lastBeepTime >= 500){
if(isBeepOn){
noTone(buzzerPin);
}
else{
tone(buzzerPin,1000);
}
isBeepOn = !isBeepOn;
lastBeepTime = currentTime;
}
}
delay(10);
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%)
display.clearDisplay();
// Print water level percentage
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5, 9);
display.print("WATER LEVEL:\n");
display.print("-------------");
display.print("\n");
// Draw a simple bar graph representing the water level
int barHeight = map(percent, 0, 100, 0, SCREEN_HEIGHT - 25); // Adjusted height
int barWidth = 35;
int barX = 90;
int barY = 10; // Adjusted starting point
// Calculate top of the bar
int barTop = barY + (SCREEN_HEIGHT - 25 - barHeight);
// Ensure bar height does not exceed screen height
if (barHeight > (SCREEN_HEIGHT - 25)) {
barHeight = SCREEN_HEIGHT - 25;
}
// Draw outer border rectangle
display.drawRect(barX - 1, barY - 1, barWidth + 2, SCREEN_HEIGHT - 25 + 2, SSD1306_WHITE);
// Draw filled inner rectangle representing the bar
display.fillRect(barX, barTop, barWidth, barHeight, SSD1306_WHITE);
// Draw the dashed border box for percentage
int boxWidth = 70;
int boxHeight = 36;
int boxX = 4;
int boxY = SCREEN_HEIGHT - boxHeight - 5;
// Draw top and bottom borders with dashes
for (int i = boxX; i < boxX + boxWidth; i += 4) {
display.drawPixel(i, boxY, SSD1306_WHITE);
display.drawPixel(i + 1, boxY, SSD1306_WHITE);
display.drawPixel(i, boxY + boxHeight, SSD1306_WHITE);
display.drawPixel(i + 1, boxY + boxHeight, SSD1306_WHITE);
}
// Draw left and right borders
for (int i = boxY; i < boxY + boxHeight; i += 4) {
display.drawPixel(boxX, i, SSD1306_WHITE);
display.drawPixel(boxX, i + 1, SSD1306_WHITE);
display.drawPixel(boxX + boxWidth, i, SSD1306_WHITE);
display.drawPixel(boxX + boxWidth, i + 1, SSD1306_WHITE);
}
// Display the percentage inside the box
display.setCursor(boxX + 18, boxY + 9);
display.setTextSize(2);
display.print(percent);
display.print("%");
if (percent >= 93 ) {
digitalWrite(relayPin, HIGH);
if (!mute) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer if not muted
tone(buzzerPin, 1000);
}
}
else if(percent <= 15){
digitalWrite(relayPin, HIGH);
if (!mute) {
digitalWrite(buzzerPin, HIGH);
// Turn on the buzzer if not muted
tone(buzzerPin, 800);
}
}
else {
mute = false; // Reset the mute state when outside the range
noTone(buzzerPin);
digitalWrite(relayPin, LOW);
digitalWrite(buzzerPin, LOW); // Ensure the buzzer is off
}
display.display();
delay(1000);
}