Harsh Sharma Technicals

Everything About Technology At One Place

Codes And Circuit DiagramsGadgetsLatest

Code and Circuit Diagram for AUTOMATIC HAND SANITIZER DISPENSER

If you want to buy the entire combo including already programmed Arduino board then CLICK HERE 

#include <Servo.h>

const int trigPin = 9;
const int echoPin = 8;
long duration;
int distance;

Servo myServo;
void setup()
{
myServo.attach(7);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
}

void loop() {
distance = calculateDistance();

myServo.write(0);
if ( distance < 5)
{ myServo.attach(7);
myServo.write(160);
delay(500);
myServo.write(0);
delay(1000);

}
else{
myServo.detach();
}
Serial.println(distance);
}
int calculateDistance(){

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
return distance;
}

IF YOU ARE GETTING A STRAY ERROR THEN CLICK HERE 


 

CIRCUIT DIAGRAM



Leave a Reply

Your email address will not be published. Required fields are marked *