Arduino based hand sanitizer

Given below is the code i am facing issue.I have project submission tomorrow, but my system is not working properly. I have used ultrasonic and PIR sensor with single channel relay and water pump. After sensing the movement the realy keeps turning on and off, making ticking noise and sometimes it goes on for few seconds. The bold text of code I am facing issue with.

code:

/* To allocate the Arduino board's pin to
   trigger and echo pins of Ultrasonic sensor HC SR-04
   define function is used
*/
#define triggerPin 3
#define echoPin 2
/* To perform the simulation of a pump, led is used.
    When the hand will be closer to the sensor,
    the led pin will be high and it will be on for 5 seconds
*/
#define ledPin 12
int sound = 250;
/* I have used also the PIR sensor with Ultrasonic sensor to detect human
    hand better
    for the inpur of PIR sensor, pin 8 is being used
*/
#define pirpin 8

void setup() {
  Serial.begin (9600);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(pirpin, INPUT);

}
void loop() {
  digitalWrite(ledPin, LOW);
  long pulsetime, interval;
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pulsetime = pulseIn(echoPin, HIGH);
  interval = pulsetime * 0.034 / 2; //using the formula of s= v*t
  int sensorVal = digitalRead (pirpin);
  Serial.println(sensorVal);
  if ((sensorVal == HIGH) && (interval < 5))
  {
    Serial.println("Please keep your hands under, for 5 seconds");
    digitalWrite(ledPin, HIGH); // turn the LED on
    delay(15);
    digitalWrite(ledPin, LOW); // turn the LED off
    Serial.println("Your hands are sanitized succesfully");
    delay (15);
  }
  if (sensorVal == LOW || interval > 60 || interval <= 0)
  {
    digitalWrite(ledPin, LOW);
    Serial.println("Keep your hands closer for sanitization");
    Serial.println("Thanks for your patience. Together we will beat Corona");
    delay(500);
  }
}
    Serial.println("Please keep your hands under, for seconds");
    digitalWrite(ledPin, HIGH); // turn the LED on
    delay(15);
    digitalWrite(ledPin, LOW); // turn the LED off
    Serial.println("Your hands are sanitized succesfully");
    delay (15);

Why do you keep turning the output on an off with such a short interval

I have decreased the delay time but pump turns on for few seconds.
Don't know why

Are you sure this is your actual code running on your microcontroller?

The code you have posted has not a single line of code that would turn on/off a pump

If somebody should give you advice you have to post the complete sketch.
From the very first to the very last line

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

in Addition:
a delay(15) means the microcontroller waits for 0,015 seconds.
That is a real fast blinking of your led.

best regards Stefan

pin 12 is controlling the pump through relay

Hello
I think there are more delay() functions in the sketch that block each other.
p.s.
Well I guess a collection of timers and a small FSM will be extremly usefull to reach the o.m. system function.

So why are you turning the pump on and off at 15 millisecond intervals ?

I am really thankful to you.
But if I increase delay time, relay turns the pump on for few seconds.
I just want the pump to on only for a fraction of second.
Please help me to correct that logic.

Why are you turning the pump on an off continually ?
Does it not need to be on for a period after detection and otherwise be off ?

yes when ultrasonic sensor senses the hand in front of it , pump will be on otherwise off.

So the program should do something like this

start of loop()

  if a hand is detected
    start the pump
    save the value of millis() as the start time
  end if

  if millis() minus start time is greater than required period
    stop the pump
  end if

end of loop()

Hi,

You have told us what it is doing wrong.
PLEASE tell us exactly what you want it to do.

Why do you need Ultrasonic AND PIR?

Tom... :grinning: :+1: :coffee: :australia:

Hi,
greetings
I am making a automatic hand sanitizer system.

Hi Aslam,

nice answer. But saying

Hi,
greetings
I am making a automatic hand sanitizer system.
Does not help to proceed in your project.

If you want detailed help you have to provide your new attept to make it work.
Post your full sketch
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE

Just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting
    best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.