Ultraonic dispenser

Guys i am making an ultrasonic sanitizer dispenser. i am not very good at programming so i need help from you guys. If hand is placed at say 10 cm from the ultrasonic sensor the microcontroller should activate a relay to dispense for say 2 seconds and then turn off the relay and if the hand is placed continously at 10 cm from the sensor after 2 seconds it should not activate the relay but if once the hand is moved away and again brought near the sensor at 10 cm it should again dispense. How to write a code for this.

Have you managed to get reliable readings from your ultrasound ranger?

const int trigPin = 3;
const int echoPin = 2;
#define pump 8

long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pump, OUTPUT);

}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Distance calculation
distance= duration*0.034/2;
// Printinng the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

if (distance < 10)
{
digitalWrite(pump,HIGH);
delay(1000);
digitalWrite(pump,LOW);
delay(1000);
}
else
{
digitalWrite(pump,LOW);
}
delay(500);
}

This is the program i tested but if the hand is placed below 10 cm or at the same distance continously after dispensing once it is again and again dispensing after 1 second. Is there a way to dispense once and then if the ultrasonic sensor is reads below 10 cm then not to dispense but if the hand is moved away and moved towards to the sensor to dispense again for 1 second

So detect when the distance becomes <= 10cm, and start a timer.
Look at the state change example in the IDE

Please remember to use code tags when posting code

if (distance > 20) dispensed = false;
if (distance < 10 && !dispensed) { dispensed = true; ... }

Welcome to the forum.

on the top of each forum is a sticky post How to Use this Forum.
please open and clik that. read down to #7 about how to use ‘code tags’
to get the box with your code in it.

for the long version…
when you sense a thing, you can create a simple flag and change it .

if (Hand_Detected == 0 ) {
   if (distance < 10)
     {
      Hand_Detected = 1 ; //  change this to 1
      digitalWrite(pump,HIGH);
      delay(1000);
      digitalWrite(pump,LOW);
      delay(1000);
    }
    else
   {
   digitalWrite(pump,LOW);
   }
   delay(500);
   }
} //  end hand_detected

the problem is to count to some time and not interupt the rest of the program.
the PREFERRED method is to use ‘blink without delay’ and to not use delay.

but a cheat is
before setup add
int Hand_Detected, count ;

if ( Hand_Detected == 1 ) {
count ++ ;
delay(1) ;
if (count >= 5000)
{
Hand_Detected=0 ; //
count=0 ;
}

this will add 1 to count each time through the loop and after about 5 seconds it will reset and then be able to see your ultrasonic sensor again.

by using delay(1) it slows your program but does not stop it completely
the count then adds up how many times the delay(1) happened.
it is not too accurate, but it might get you past your current problem.

it is a hack and not clean programming, and not preferred.

int Hand_Detected, count ;

if ( Hand_Detected == 1 ) {
  count ++ ;
  delay(1) ;
  if (count >= 5000)  
     {  
     Hand_Detected=0 ;  //
     count=0 ;
    }
 }

this will add 1 to count each time through the loop and after about 5 seconds it will reset and then be able to see your ultrasonic sensor again.

It doesn't have to as written in the snippet. If the rest of the sketch reads the sensor it will do so every just over 1 ms.

by using delay(1) it slows your program but does not stop it completely

Using delay(1) will waste about 15700 out of every 16000 cycles, which for this sketch works okay.

The sensor can be read in between but I would not tie soap dispensing directly to sensor read. I would use a separate flag to turn soap dispenser off after 2 seconds and use that count variable to determine the 2 seconds... and still need more. Once the soap flag is OFF I would not act on sensor reads until the hands are no longer detected and only then dispense soap on detection and distance.

What can I say except Beginner, think of how many more things the sketch could be running without that wastey delay() call. Answer is literally hundreds of things. Stick with the simpler until you have experience/confidence enough in your code skills to take the next step.