#include <Ultrasonic.h>
const int trigPin = A0; // Trigger pin of the ultrasonic sensor
const int echoPin = A1; // Echo pin of the ultrasonic sensor
const int relayPin = 9; // Relay pin connected to Arduino
Ultrasonic ultrasonic(trigPin, echoPin);
int targetWaterLevel = 10; // Adjust this value based on your setup (in centimeters)
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int distance = ultrasonic.read(); // Read distance from sensor
Serial.print("Distance: ");
Serial.println(distance);
if (distance <= targetWaterLevel) {
digitalWrite(relayPin, LOW); // Close the tap
} else {
digitalWrite(relayPin, HIGH); // Open the tap
}
delay(1000); // Adjust delay as needed
}