Automatic Soap and Hand Sanitizer Dispenser Schemetics * by Vivek Soni (@sonivivek) * https://instagram.com/sonivivek * 12th May 2020

/* Automatic Soap and Hand Sanitizer Dispenser Code
* by Vivek Soni (@sonivivek)
* https://instagram.com/sonivivek
* 12th May 2020
*/

#include<Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // I2C Address. This can vary depending on the hardware you're using. Set accordingly

Servo motor;

int IRSensor=8; // This one is for IR Sensor
int LED=10; // This one is for 4 LED Lights
int piezoPin=11; // This one is for the buzzer

void setup()

// This part of the code will run once and only once at the beginning of you turning ON your device

{

Serial.begin(9600);
motor.attach(9);
pinMode(IRSensor, INPUT);
pinMode(LED, OUTPUT);
}

void loop ()

// This part of code will run repeatedly whenever the conditions are met. In our case, triggering the IR sensor.

{
int statusSensor=digitalRead(IRSensor);
if(statusSensor==0)
{

// The below steps for LCD are actually to maintain the continuity with the end of the last step of this entire loop.
// So that we don't see a jerk on the screen when the IR sensor gets triggered.
// Note that this is the main text that always stays on the screen while the device is idle.

lcd.begin(16,2); // This will initialize the LCD screen
lcd.backlight(); // This will turn the backlight of LCD ON
lcd.setCursor(0,0); // This sets the cursor at line zero, column zero
lcd.print("Stop the Spread"); // This will be printed on the second line (Line one)
lcd.setCursor(1,1); // This sets the cursor at line one, column one
lcd.print("Sanitize Hands"); // This will be printed on the second line (Line one)

// As soon as the sensor detects a hand (or anything for that matter), we want the LED lights pointing at the hand to turn on.


digitalWrite (10, HIGH); // LED is connected to pin 10. Putting that pin on HIGH, turns it ON. Similarly LOW turns it OFF.

// Now we come to the most important part. Putting the servo motor to work when the IR sensor gets triggered.
// But we need to make sure the device NOT AGGRESIVELY SPITTING the sanitizer.
// In order to simulate how the hands push the bottle, we will ask the motor to move with gradually with delay of 1/10 seconds.

motor.write(45); // Turns the motor 45 degree angle
delay (100); // Delay of 0.1 second
motor.write(90); // Turns the motor 45 more degree angle
delay (100); // Delay of 0.1 second
motor.write(135); // Turns the motor 45 more degree angle
delay (100); // Delay of 0.1 second
motor.write(180); // Turns the motor 45 more degree angle

// At this point, the bottle is pressed enough to dispense some hand sanitizer and it has reached to the person's hands.
// Now is the time to ring the buzzer as a gesture to inform the person that the process is done and you may now move your hands.

tone(piezoPin, 50000, 600); // Sharp buzzer tone for 0.6 second

// At the same time, we also need to take advantage of the screen to inform the person about completion of the process and more importantly, thank them. :)

lcd.clear(); // Makes sure that everything printed on the LCD prior to this point gets cleared
lcd.setCursor(3,0); // Sets the cursor at line zero, column three (Because our text is 9 characters long and we want to make sure it gets printed at the centre of the screen)
lcd.print("Thank You"); // This will be printed on the first line (Line zero)
lcd.setCursor(3,1); // Sets the cursor at line one, column three (Because our text is 9 characters long and we want to make sure it gets printed at the centre of the screen)
lcd.print("Stay Safe!"); // This will be printed on the second line (Line one)

// Now that the process is complete, we need to turn of the LED lights. (Another gesture to psychologically inform the person that the process is over) But to make sure it is not very abrupt and that the LED stays on for enough time, we will keep a small delay before it turns off.

delay(1000);
digitalWrite (10, LOW);

// Now we need our motor go back to the original position so that it can perform another cycle of dispensing sanitizer

motor.write(0);

// We need to make sure that the servo doesn't perform another cycle and despense sanitizer again even before the person moves their hand. We will give a 2+1=3 scond delay. Nothing will happen during this time with the servo.

delay(2000);

// interestingly, if someone tries to use the device during this time of delay, it will do nothing and they will think that it doesn't work properly. To make sure that doesn't happen, we will again take advantage of our LED screen to positively communicate the message that the system was idle until now.

lcd.clear(); // Makes sure that everything printed on the LCD prior to this point gets cleared
lcd.setCursor(5,0); // Sets the cursor at line zero, column five (Because our text is 6 characters long and we want to make sure it gets printed at the centre of the screen)
lcd.print("SYSTEM"); // This will be printed on the first line (Line zero)
lcd.setCursor(5,1); // Sets the cursor at line one, column six (Because our text is 5 characters long and we want to make sure it gets printed at the centre of the screen)
lcd.print("READY"); // This will be printed on the second line (Line one)

delay(1000); // This is the amout of time for which we want the SYSTEM READY message to be shown. Hence, a delay before we print something else on the screen.
// Technically, system isn't ready at this point. But 1 sec. is the time it will take human brain to read and analyze the message.
// So, before they could act accordingly, 1 sec will be passed and we will make sure by then the system will actually be ready.

// Now is the time to put the last message on the screen that will always stay there on the screen until the device is sitting idle and sensor is uninterrupted.
// This is the same text that we had put for LCD screen at the beginning of this loop. Connecting the dots. ;)

lcd.clear(); // Makes sure that everything printed on the LCD prior to this point gets cleared
lcd.setCursor(0,0); // This sets the cursor at line zero, column zero
lcd.print("Stop the Spread"); // This will be printed on the first line (Line zero)
lcd.setCursor(0,1); // This sets the cursor at line one, column zero
lcd.print("Sanitize Hands"); // This will be printed on the second line (Line one)

// That is all. Our Hand Sanitizer dispenser with all the fancy LED lights, LCD screen interaction and buzzer sound is ready to go!

}
}