Auto Hand Sanitizer Dispenser - Code Help

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;
}

    if (pumpState == HIGH) {    >>>>>>>>>>// changes to 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;
}

critycal_:

critycal_:
Let me know if it worked for you.

his is very useful ...
I changed the code from high to low and succeeded for 3 seconds automatically stopping to keep the detected hand... thank you