Auto Hand Sanitizer Dispenser - Code Help

Hello everyone.

I have made an automatic hand sanitizer dispenser that detects hand or object at a certain distance near the nozzle and dispenses it automatically for use at my Emergency Department as a sort of non-contact hand sanitation for people coming in and going out. I have not written the code and it is by someone else as I have nil experience in coding. The problem with the existing code is it dispenses liquid through a submersible pump continuously until the hand is removed which is causing some waste of sanitizer liquid. We can educate people to slowly lower their hand instead of removing their hand from the nozzle but as we receive new patients and attendants everyday, educating each and everyone would be difficult.
I have tried to edit the code myself but with the new code now the pump is automatically being triggered every 4 seconds without any hand near the nozzle .
I am using Arduino Nano, an IR obstacle sensor and a submersible pump.
Here I have attached both the codes.
I am looking for help in modifying the code in such a way that once triggered the pump works for 3 or 4 seconds and shuts off automatically until the next trigger.
Thank you all.

“New” code:

#define IRsensor 2
#define DCwater_pump 8
void setup()
{
  pinMode(IRsensor, INPUT);
  pinMode(DCwater_pump, OUTPUT);
  Serial.begin(9600);
}
int readPin = 0;
void loop()
{
  readPin = digitalRead(IRsensor);
  if (readPin == HIGH)
  {
    digitalWrite(DCwater_pump, HIGH);
    Serial.println("DC Pump is ON Now!!");
    delay(500);
  }
  else
  {
    digitalWrite(DCwater_pump, LOW);
    Serial.println("DC Pump is OFF Now!!");
    delay(500);


  }
}

“Modified” code

#define IRsensor 2
#define DCwater_pump 8
void setup()
{
  pinMode(IRsensor, INPUT);
  pinMode(DCwater_pump, OUTPUT);
  Serial.begin(9600);
}
int readPin = 0;
void loop()
{
  readPin = digitalRead(IRsensor);
  if (readPin == HIGH)
  {
    digitalWrite(DCwater_pump, HIGH);
    Serial.println("DC Pump is ON Now!!");
    delay(3000);
    digitalWrite(DCwater_pump, LOW);
    delay(500)
  }
  else
  {
    digitalWrite(DCwater_pump, LOW);
    Serial.println("DC Pump is OFF Now!!");
    delay(500);


  }
}

working_sanitizer_new.ino (445 Bytes)

working_sanitizer_modified_code.ino (493 Bytes)

Is there a pullup resistor on this pin?
pinMode(IRsensor, INPUT);

If not, change this to INPUT_PULLUP

TheMemberFormerlyKnownAsAWOL:
Please post your code.
In code tags

I think it is automatically showing the code in tags, when I am viewing this post.
I have uploaded them as attachments .ino files.
thanks.

No, it is not automatic. A Moderator added them for you. Next time, use the </> button on the menu to post your code.

Take a look at the state change example in the IDE

CrossRoads:
Is there a pullup resistor on this pin?
pinMode(IRsensor, INPUT);

If not, change this to INPUT_PULLUP

I changed the code as instructed and tried both the codes and it did not change anything. The project is working the same. I am unable to run the pump for a specified time after the IRSensor detects an obstacle / hand.

Did you change the code to detect the state-change?

If so, post the changed code.

TheMemberFormerlyKnownAsAWOL:
Did you change the code to detect the state-change?

If so, post the changed code.

I have looked at the state change example in the IDE and also went through many guides on youtube explaining about it and how to use it but I am unable to integrate it in my project, actually I did not understand it that perfectly. Can you guide me, if possible ?

The trick is to not turn on the pump when the trigger input is HIGH, but to turn on the pump when the trigger input becomes HIGH.

That’s what the state change example shows.

In my IR-sensor module it is in high state when there is nothing near it .... it goes to low when there is something near it... i am also not that good at coding but let me know if this works...

const int IR = 2;
void setup()
{
pinMode(IR, INPUT);
pinMode(8, OUTPUT);
}
void loop()
{

if (digitalRead(IR)==LOW)
{
digitalWrite(8, HIGH);
delay(3000);
digitalWrite(8, LOW);
delay(500);
}
else
{
digitalWrite(8, LOW);
delay(500);

}
}

@cosmic_bot that’s not a state change, and hardly different to the original code.

Please remember to use code tags when posting code.

@TheMemberFormerlyKnownAsAWOL.... i understand state change.... i've only changed the sensor to low...I just hope it works. sorry, if i am wrong.

Hi, I modified the code according to your needs.
If your hands trigger the sensor, it will pump for only 3 seconds, even if the sensor is still triggered.
It will be triggered, if you remove your hands and trigger the sensor again.

Some things to mention:

CrossRoads:
Is there a pullup resistor on this pin?
pinMode(IRsensor, INPUT);

If not, change this to INPUT_PULLUP

This shouldn't be necessary in this case, because the sensor (probably) has one built in.

drkarthikv:
I am using Arduino Nano, an IR obstacle sensor and a submersible pump.

How do you operate the pump? If you trigger it directly via the digital pin,
you may have a broken arduino in the near future. The maximum output current for one digital
pin is 40mA. Pumps usually draw way more than that, especially while starting.

If you use a BJT or logic level MOSFET (overkill), your project will last longer.

Now the code:

#define IRsensor 2
#define DCwater_pump 8

int pumpState = 0;
int lastPumpState = 0;

void setup()
{
  pinMode(IRsensor, INPUT);
  pinMode(DCwater_pump, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  pumpState = digitalRead(IRsensor);
  if (pumpState != lastPumpState) {
    if (pumpState == HIGH) {
      digitalWrite(DCwater_pump, HIGH);
      Serial.println("DC Pump is ON Now!!");
      delay(3000); //3000ms = 3 seconds, change this according to your needs.
      digitalWrite(DCwater_pump, LOW);
      Serial.println("DC Pump is OFF Now!!");
    }
    delay(50);
  }

  lastPumpState = pumpState;
}

Let me know if it worked for you.

Terima kasih. Ini sangat berguna!

Moderator edit
Google translate says that the language is Indonesian and says
"thanks. This is very useful!"

Sir I need altrasonic sensor code same project please send me code rajningar@gmail.com

Please help me for same sanataization code for 2 seconds

rajningar:
Please help me for same sanataization code for 2 seconds

We can’t help until you tell us what you’ve got and what you’ve tried.

Below thread will have some solutions:

thanks..

Dear sir
how are you

i have a same problem i checked for one week on the net but im not find solution until i read your topic and i madz change on your code

now its working with me

i will give a gift im using IR sensor

#define IRsensor 2
#define DCwater_pump 8

int pumpState = 0;
int lastPumpState = 0;

void setup()
{
pinMode(IRsensor, INPUT);
pinMode(DCwater_pump, OUTPUT);
Serial.begin(9600);
}

void loop() {
pumpState = digitalRead(IRsensor);
if (pumpState != lastPumpState) {
if (pumpState == LOW) {
digitalWrite(DCwater_pump, HIGH);
Serial.println("DC Pump is ON Now!!");
delay(3000); //3000ms = 3 seconds, change this according to your needs.
digitalWrite(DCwater_pump, LOW);
Serial.println("DC Pump is OFF Now!!");
}
delay(50);
}

lastPumpState = pumpState;
}