/* Main.ino file generated by New Project wizard
Created: Sat May 4 2024
Processor: Arduino Uno
Compiler: Arduino AVR (Proteus)
*/
#include <NewPing.h>
#include <toneAC.h>
#define debounceDelay 250 ////milliseconds
#define manualOnTime 5000 /////milliseconds
#define manualSoapOnTime 3000 //30Seconds
#define manualWaterOnTime 30000 //3 Secondes
#define serialUpdateTime 100 /////milliseconds
const int manualPin = 2;
const int Tpin = 4;
const int Epin = 5;
const int waterLEDPin = 7;
const int soapLEDPin = 8;
// BUZZER PIN IS 9(+) & 10(-)
const int waterPump = 11; // water pump
const int soapPump = 12; //sopa pump
const int maxDis = 100;
int dis = 0;
boolean firstRun = true;
boolean pumpStatus = false, blinkStatus = false;
// boolean ledState = LOW; // ledState used to set the LED
boolean manualState = false, handState = false;
unsigned long previousMillis = 0, currentMillis = 0; // will store last time LED was updated
unsigned long delayMillis = 0; // will store last time Serial was updated
unsigned long manualMillis = 0; // will store last time manual Button was updated
NewPing hand(Tpin, Epin, maxDis);
void setup () {
// TODO: put your setup code here, to run once:
Serial.begin(115200);
pinMode(Tpin, OUTPUT);
pinMode(Epin, INPUT);
pinMode(manualPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(manualPin), manualControl, CHANGE);
pinMode(waterLEDPin, OUTPUT);
pinMode(soapLEDPin, OUTPUT);
pinMode(waterPump, OUTPUT);
pinMode(soapPump, OUTPUT);
digitalWrite(waterPump, HIGH);
digitalWrite(soapPump, HIGH);
Serial.println("\n\n *** Automatic Hand Gel Dispenser ***\n");
delay(1000);
}
void loop() {
static int counter = 0;
// TODO: put your main code here, to run repeatedly:
dis = hand.ping_cm();
currentMillis = millis();
if (currentMillis - delayMillis >= serialUpdateTime) { // check if 100 milli second has passed
delayMillis = currentMillis; // update the delayMillis
Serial.print("Distance : ");
Serial.print(dis);
Serial.print("\tcm");
if (manualState) {
if (!firstRun) {
Serial.print(" : Manual Soap : ");
Serial.print((( manualSoapOnTime - (currentMillis - manualMillis) ) / 1000) + 1);
} else {
Serial.print(" : Manual Water : ");
Serial.print((( manualWaterOnTime - (currentMillis - manualMillis) ) / 1000) + 1);
}
Serial.print(" sec");
}
Serial.println();
}
if (!firstRun) {
if ((currentMillis - manualMillis >= manualSoapOnTime) && manualState) { // check if 5 second has passed
//manualMillis = currentMillis; // update the manualMillis
manualState = false;
Serial.println("Manual Timer Passed...");
}
} else {
if ((currentMillis - manualMillis >= manualWaterOnTime) && manualState) { // check if 5 second has passed
//manualMillis = currentMillis; // update the manualMillis
manualState = false;
Serial.println("Manual Timer Passed...");
}
}
//check if hand exist
if (0 < dis && dis < 10 && dis != 0) {
handState = true;
} else {
handState = false;
}
if (handState) counter++;
else counter = 0;
//Perform Actions
beep(2); // loop buzzer
if ((handState && counter > 5) || manualState) {
pumpControl(HIGH);
//reset manual timer when hand exist
if (handState && manualState) {
manualState = false;
Serial.println("Manual timer RESETED / HAND EXIST ..");
}
} else {
pumpControl(LOW);
}
}
void pumpControl(boolean turnOn) {
if (turnOn) {
if (!pumpStatus) {
// Dispense Hand Gel
if (firstRun) {
digitalWrite(waterPump, HIGH);
digitalWrite(soapPump, LOW);
digitalWrite(waterLEDPin, LOW);
digitalWrite(soapLEDPin, HIGH); // set the LED
Serial.println("Water Pump : OFF || Soap Pump : ON ");
Serial.print("Distance : ");
Serial.print(dis);
Serial.println("\tcm");
beep(1);
firstRun = false;
}
else {
digitalWrite(waterPump, LOW);
digitalWrite(soapPump, HIGH);
digitalWrite(waterLEDPin, HIGH);
digitalWrite(soapLEDPin, LOW); // set the LED
Serial.println("Water Pump : ON || Soap Pump : OFF ");
Serial.print("Distance : ");
Serial.print(dis);
Serial.println("\tcm");
beep(1);
firstRun = true;
}
pumpStatus = true;
}
} else {
if (pumpStatus) {
// Dispense Hand Gel
digitalWrite(waterPump, HIGH);
digitalWrite(soapPump, HIGH);
digitalWrite(waterLEDPin, LOW);
digitalWrite(soapLEDPin, LOW); // set the LED
Serial.println("Water Pump : OFF || Soap Pump : OFF ");
beep(0);
pumpStatus = false;
}
}
}
void manualControl() {
/// Manual Button Pressed
if ( digitalRead(manualPin) == LOW ) {
if (!manualState && (currentMillis - manualMillis >= debounceDelay)) { //debounce
manualState = true;
manualMillis = currentMillis; // update the manualMillis
Serial.println("Manual Button Pressed...");
} else if (currentMillis - manualMillis >= debounceDelay) {
manualMillis = currentMillis; // update the manualMillis
manualState = false;
Serial.println("Manual Button RESETED...");
}
}
}
void beep(int isOn) {
if (isOn == 1) blinkStatus = true;
else if (isOn == 0) blinkStatus = false;
if(blinkStatus && isOn == 2){
if (currentMillis - previousMillis >= 1000) { // check if 1 second has passed
previousMillis = currentMillis; // update the previousMillis
if (handState) toneAC(262);
else if (manualState) toneAC(440);
else toneAC(440);
delay(500);
toneAC();
}
}
else if (isOn == 0) {
toneAC();
}
}
// void led(int isOn) {
// if (isOn == 1) blinkStatus = true;
// else if (isOn == 0) blinkStatus = false;
// //else
// if (blinkStatus && isOn == 2) {
// if (currentMillis - previousMillis >= 1000) { // check if 1 second has passed
// previousMillis = currentMillis; // update the previousMillis
// ledState = !ledState; // toggle the ledState
// if (firstRun) digitalWrite(soapLEDPin, ledState); // set the LED'
// else digitalWrite(waterLEDPin, ledState);
// //digitalWrite(buzz,HIGH);
// if (handState) toneAC(262);
// else if (manualState && !handState) toneAC(440);
// delay(500);
// //digitalWrite(buzz,LOW);
// toneAC();
// }
// }
// else if (isOn == 0) {
// digitalWrite(waterLEDPin, LOW); // turn off the LED
// digitalWrite(soapLEDPin, LOW);
// toneAC();
// }
// }
// void ledSet(boolean isON){
// if(isON){
// if(firstRun) {
// digitalWrite(soapLEDPin, HIGH); // set the LED'
// digitalWrite(waterLEDPin, LOW);
// }
// else {
// digitalWrite(soapLEDPin, LOW);
// digitalWrite(waterLEDPin, HIGH);
// }
// }
// else {
// digitalWrite(waterLEDPin, LOW); // turn off the LED
// digitalWrite(soapLEDPin, LOW);
// }
// }