#include <Servo.h>
Servo myServo;
const int trigPin = 9;
const int echoPin = 10;
const int pumpPin = 8;
long duration;
int distance;
void setup() {
myServo. attach(6);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pumpPin, OUTPUT);
myServo.write(0); // Servo in default
Serial.begin(9600);
}
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW); delayMicroseconds(2);
// Trigger the sensor by setting the trigPin HIGH for 10 microseconds digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echopin, returns the sound wave travel time in microseconds
duration ;pulseIn(echopin, HIGH);
// Calculate the distance distance duration * 0.034 / 2;
// If distance is less than 10 cm, activate the pump
if (distance < 10) {
digitalWrite(pumpPin, HIGH);
myServo.write(90); // Move the servo to press the pump
delay(1000); // Pump sanitizer for 1 second
digitalWrite(pumpPin, LOW); myServo.write(0); // Move the servo back to default position
}
delay(1000); // Check every 1 second
}