#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pins for JSN-SR04T Ultrasonic Sensors and motor
#define TRIG_PIN_SUMP 5
#define ECHO_PIN_SUMP 18
#define TRIG_PIN_TANK 19
#define ECHO_PIN_TANK 4
#define MOTOR_PIN 15
// Define pins for mode switch and manual control
#define MODE_SWITCH_PIN 13
#define MANUAL_CONTROL_PIN 12
// Constants for distance thresholds (in cm)
const int MIN_SUMP_LEVEL = 90; // Example threshold for sump low level
const int MAX_SUMP_LEVEL = 10;
const int MAX_TANK_LEVEL = 10; // Example threshold for tank full level
const int MIN_TANK_LEVEL = 90;
// Create an LCD object with the I2C address 0x27 and a 16x2 display size
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(115200);
// Initialize sensor pins
pinMode(TRIG_PIN_SUMP, OUTPUT);
pinMode(ECHO_PIN_SUMP, INPUT);
pinMode(TRIG_PIN_TANK, OUTPUT);
pinMode(ECHO_PIN_TANK, INPUT);
// Initialize motor control pin
pinMode(MOTOR_PIN, OUTPUT);
digitalWrite(MOTOR_PIN, LOW); // Motor off initially
// Initialize mode switch and manual control pins
pinMode(MODE_SWITCH_PIN, INPUT_PULLUP);
pinMode(MANUAL_CONTROL_PIN, INPUT_PULLUP);
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the backlight of the LCD
}
void loop() {
bool autoMode = digitalRead(MODE_SWITCH_PIN) == LOW; // Auto mode if switch is LOW
bool manualControl = digitalRead(MANUAL_CONTROL_PIN) == LOW; // Manual control if button is pressed
int sumpLevel = getDistance(TRIG_PIN_SUMP, ECHO_PIN_SUMP);
int tankLevel = getDistance(TRIG_PIN_TANK, ECHO_PIN_TANK);
if (autoMode) {
// Automatic mode logic
if (sumpLevel < MIN_SUMP_LEVEL && tankLevel > MAX_TANK_LEVEL) {
digitalWrite(MOTOR_PIN, HIGH); // Turn on the motor
} else {
digitalWrite(MOTOR_PIN, LOW); // Turn off the motor
}
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Auto Mode");
lcd.setCursor(0, 1);
lcd.print("Sump Level : ");
sumpLevel = map((int)sumpLevel ,MIN_SUMP_LEVEL, MAX_SUMP_LEVEL, 0, 100);
lcd.print(sumpLevel);
lcd.print(" % ");
lcd.setCursor(0, 2);
lcd.print("Tank Level : ");
tankLevel = map((int)tankLevel ,MIN_TANK_LEVEL, MAX_TANK_LEVEL, 0, 100);
lcd.print(tankLevel);
lcd.print(" %");
} else if (manualControl) {
// Manual control logic
digitalWrite(MOTOR_PIN, HIGH); // Turn on the motor
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Manual Mode");
lcd.setCursor(0, 1);
lcd.print("Motor ON");
} else {
// Ensure motor is off in manual mode if button is not pressed
digitalWrite(MOTOR_PIN, LOW);
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Manual Mode");
lcd.setCursor(0, 1);
lcd.print("Motor OFF");
}
delay(1000); // Delay for stability
}
int getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2; // Convert to cm
return distance;
}