int trig = 18;
int echo = 19;
int relay = 25;
int ledgreen=16;
long duration ;
int distance ;
int waterLevelThreshold= 250;
int drashbinThreshold= 100; // Water level threshold in cm (adjust based on your tank size)
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set sensor pins as output/input
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(relay, OUTPUT);
// Turn off the relay (pump off) initially
digitalWrite(relay, HIGH);
}
void loop() {
// Trigger the ultrasonic sensor
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Read the echo signal
duration = pulseIn(echo, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
// Print the distance for debugging
Serial.print("Distance: ");
Serial.print(distance );
Serial.println(" cm");
// Control the relay based on water level
if (distance < waterLevelThreshold) {
// If the water level is low, turn on the relay (pump on)
digitalWrite(relay, HIGH);
Serial.println("Pump ON");
} else {
digitalWrite(relay, LOW);
Serial.println("Pump OFF");
if (distance <drashbinThreshold){
digitalWrite(ledgreen, HIGH);
Serial.println("drashbin opened");
}else {
digitalWrite(ledgreen, LOW);
Serial.println("drashbin closed");
}
}
// Wait for a second before next reading
delay(1000);
}
// put your main code here, to run repeatedly:// this speeds up the simulation