Program for Hand sanitizer dispenser using Arduino and Servo

Arduino and servo motor

Arduino and servo motor

#include<Servo.h>
Servo servo_tap,servo_dispensor;
int angle = 0;
int VCC =13;
int Trig = 12;
int Echo = 11;
int GND = 10;
int buzzer_vcc = 2;
int buzzer_gnd = 5;
float duration,distance =30;
boolean flag = false;
//================================================
void setup()
{
servo_tap.attach(4);
servo_dispensor.attach(6);
Serial.begin(115200);
pinMode(VCC,OUTPUT);
pinMode(Trig,OUTPUT);
pinMode(Echo,INPUT);
pinMode(GND,OUTPUT);
pinMode(buzzer_vcc,OUTPUT);
pinMode(buzzer_gnd,OUTPUT);
//=================================================
digitalWrite(VCC,HIGH);
digitalWrite(GND,LOW);
digitalWrite(buzzer_gnd,LOW);
}
//==================================================
void detect_human()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
duration = pulseIn(Echo, HIGH);
distance = (duration*.0343)/2;
delay(1000);
Serial.println(distance);
}
void beep_for_sanitizer()
{
digitalWrite(buzzer_vcc,HIGH);
delay(2000);
digitalWrite(buzzer_vcc,LOW);
}
void dispense_sanitizer()
{
for (angle = 90; angle >= 0; angle -= 1) {
servo_dispensor.write(angle);
delay(10);
}

for (angle = 0; angle <= 90; angle += 1) {
// in steps of 1 degree
servo_dispensor.write(angle);
delay(10);
}
}
void beep_for_tap_open()
{
digitalWrite(buzzer_vcc,HIGH);
delay(1000);
digitalWrite(buzzer_vcc,LOW);
delay(1000);

digitalWrite(buzzer_vcc,HIGH);
delay(800);
digitalWrite(buzzer_vcc,LOW);
delay(800);

digitalWrite(buzzer_vcc,HIGH);
delay(600);
digitalWrite(buzzer_vcc,LOW);
delay(600);

digitalWrite(buzzer_vcc,HIGH);
delay(100);
digitalWrite(buzzer_vcc,LOW);
delay(100);
}
void open_tap()
{
for (angle = 90; angle >= 0; angle -= 1) {
servo_tap.write(angle);
delay(20);
}
}
void close_tap()
{
for (angle = 0; angle <= 90; angle += 1) {
servo_tap.write(angle);
delay(20);
}
}
void flow()
{
flag = true;
detect_human(); //here we call this function
if(distance <= 6 && flag == true)
{
Serial.println("Human detected");
flag = false;
Serial.println("BEEP....");
beep_for_sanitizer();
delay(1000);
Serial.println("Dispense_sanitizer....");
dispense_sanitizer();
dispense_sanitizer();//it will tap the bottle 2 times
open_tap();
delay(3000);
close_tap();
Serial.println("WASH YOUR HAND FOR 20 SEC");
delay(20000);
Serial.println("TAP OPENING");
delay(3000);
Serial.println("Beep_for_Tap_open");
beep_for_tap_open();
open_tap();
Serial.println("TAP CLOSING");
delay(15000);
close_tap();
}
else
{

}
}
void loop()
{
flow();

}