-1

So, I want to make a hand sanitizer. I have an ultra-sonic sensor.

Right now it works like this: when distance is lower than 5cm, it pours liquid.

What I want to do is: when distance < 5 cm, pour for 2 seconds. After the 2 second have passed, wait another 3 seconds before measuring again.

Ex: I put my hand in front on sensor, it pours liquid for 2 seconds. After that it waits for 3 seconds then it's ready to pour again if distance < 5 cm.

Thank you, it would be of much help. I really don't know how to implement a timer like this.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Aside: it would be better to wait until there is nothing with 5 cm as well as the time limit. – Weather Vane Dec 15 '20 at 10:44
  • So i only want to pour for 2 seconds. Like the ones you see at the malls. Then to be ready to pour again after 3 seconds and so on. This would be the loop. – Mihai Cozmuța Dec 15 '20 at 10:54
  • What I mean is, wait not only for 3 seconds, but until there is nothing with range, before resetting the cycle. Draw a flow chart. – Weather Vane Dec 15 '20 at 11:06
  • do you have access to a websearch service like www.google.com? we expect you to show some own effort. arduino related questions should contain code. please read [ask] – Piglet Dec 16 '20 at 07:45

1 Answers1

1
#define DISTANCE_IN_MM          50u
#define DISPENSE_DURATION_IN_MS 2000u
#define DISPENSE_TIMOUT_IN_MS   3000u

// Function declarations.
//
uint16_t ultrasonic_sensor_get_distance();
void     turn_dispenser_on();
void     turn_dispenser_off();

void loop()
{
    // Poll sensor distance. If under threshold, pour.
    //
    if (DISTANCE_IN_MM >= ultrasonic_sensor_get_distance())
    {
        turn_dispenser_on();
        delay(DISPENSE_DURATION_IN_MS);
        turn_dispenser_off();
        delay(DISPENSE_TIMOUT_IN_MS);
    }
}
earthling
  • 620
  • 6
  • 20