// Pin assignments
const int trigPin = 24;
const int echoPin = 25;
// Variables to store sensor readings
long duration;
int distance;
void setup() {
// Start serial communication
Serial.begin(9600);
// Set the trigger and echo pin modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Send a pulse to the ultrasonic sensor to trigger the measurement
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the time it takes for the pulse to return
duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in cm)
distance = duration * 0.0344 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Map the distance to the corresponding level percentage
if (distance <= 100) {
Serial.println("Level: 25% (Low)");
} else if (distance <= 200) {
Serial.println("Level: 50%");
} else if (distance <= 300) {
Serial.println("Level: 75%");
} else if (distance <= 400) {
Serial.println("Level: 100% (Full)");
} else {
Serial.println("Out of Range");
}
// Wait for a second before the next reading
delay(1000);
}