Millis and ultrasonic sensor

I am using one ultrasonic sensor for turning on and off the water and soap dispenser.

I wanted to implement something like this:

If sensor detects something
Water will flow for 1 second then stop, check again if the user is still there, then dispose liquid soap for 500 Milliseconds. Then stop then open water again until the user is there. Can someone help me doing this one. Thank you in advance.

Hello
Post your current sketch to see how we can help.

Is this a school or college assignment?

The first help is to search the forum for similar projects.
Then tell us what you need help with.
Paul

school my friend

Here is my code. I use delay here and i wanted to replace it with millis

const int SoapandWater_Trigger = 6;
const int SoapandWater_Echo = 5;

const int Dryer_Trigger = 8;
const int Dryer_Echo = 7;

const int Water = 4;
const int Soap = 3;
const int Dryer = 2;

const int SoapandWater_led = 9;
const int Dryer_led = 10;
const int Buzzer = 11;

long SoapandWater_Hand_Distance, 
     SoapandWater_Sensor_Duration, 
     Dryer_Hand_Distance, 
     Dryer_Sensor_Duration;



void setup() {
 Serial.begin(9600);
 
 pinMode(SoapandWater_Echo, INPUT);
 pinMode(SoapandWater_Trigger, OUTPUT);
 
 pinMode(Dryer_Echo, INPUT);
 pinMode(Dryer_Trigger, OUTPUT);

 pinMode(Water, OUTPUT);
 pinMode(Soap, OUTPUT);
 pinMode(Dryer, OUTPUT);
 pinMode(Buzzer, OUTPUT);

 pinMode(SoapandWater_led, OUTPUT);
 pinMode(Dryer_led, OUTPUT);

 digitalWrite(SoapandWater_led, LOW);
 digitalWrite(Dryer_led, LOW);
 
 digitalWrite(Water, LOW);
 digitalWrite(Soap, LOW);
 digitalWrite(Dryer, LOW);
 digitalWrite(Buzzer, LOW);
 
}


void loop() {
  
  SoapandWater_Distance();
  Dryer_Distance();
  SoapandWater_Off();
  Dryer_Off();

  if(SoapandWater_Hand_Distance <= 23 && SoapandWater_Hand_Distance!=0)
  {
    soap_activated();
    if(SoapandWater_Hand_Distance <= 23 && SoapandWater_Hand_Distance!=0)
    {
    SoapandWater_Activated();
    } 
  }
   else
  {
    SoapandWater_Off();
  }
  
  if(Dryer_Hand_Distance <= 23 && Dryer_Hand_Distance!=0)
  {
    Dryer_Activated();
  }
  else
  {
    SoapandWater_Off();
    Dryer_Off();
  }


}


//FUNCTIONS

//To determine the distance captured by the sensor
long SoapandWater_Distance()
{
  digitalWrite(SoapandWater_Trigger,LOW);
  delayMicroseconds(2);
  digitalWrite(SoapandWater_Trigger,HIGH);
  delayMicroseconds(15);
  digitalWrite(SoapandWater_Trigger,LOW);
  SoapandWater_Sensor_Duration = pulseIn(SoapandWater_Echo, HIGH);
  SoapandWater_Hand_Distance = (SoapandWater_Sensor_Duration/2)/29;
  delay(5);
}

//power on Soap and Water simultaneously
void soap_activated(){
   digitalWrite(SoapandWater_led, HIGH);//indicator if the sensor detects hands
  digitalWrite(Water, HIGH);//to wet hands 
  delay(2000);
  digitalWrite(Water, LOW);
  delay(800);
}
void SoapandWater_Activated()
{
  digitalWrite(Soap, HIGH);//dispose soap
  delay(400);
  digitalWrite(Soap,LOW);
  delay (2000);//to scatter soap in hands
  digitalWrite(Water, HIGH);//dispense water
  delay(20000);//hand washing time must be 20secs
  digitalWrite(Water, LOW);
  digitalWrite(SoapandWater_led, LOW);
  Buzzer_Enabled();
  delay(5000);
}

//turn off Soap and Water
void SoapandWater_Off()
{
  digitalWrite(Water,LOW);
  digitalWrite(Soap, LOW);
  digitalWrite(SoapandWater_led, LOW);
}

//to determine the distance of the hand to the sensor of hand dryer
void Dryer_Distance()
{
  digitalWrite(Dryer_Trigger,LOW);
  delayMicroseconds(2);
  digitalWrite(Dryer_Trigger,HIGH);
  delayMicroseconds(15);
  digitalWrite(Dryer_Trigger,LOW);
  Dryer_Sensor_Duration = pulseIn (Dryer_Echo, HIGH);
  Dryer_Hand_Distance = (Dryer_Sensor_Duration/2)/29;
  delay(5);
}

//power on hand dryer
void Dryer_Activated()
{
  digitalWrite(Dryer_led, HIGH);
  digitalWrite(Dryer, HIGH);
  delay(3000);
}

//power off hand dryer
void Dryer_Off()
{
  digitalWrite(Dryer,LOW);
  digitalWrite(Dryer_led, LOW);
}

It is not as easy as just "replacing" delay() with millis(). In your case I would recommend creating a simple state machine, making it much easier to time the individual operations.

State Machine

BlinkWithoutDelay

Arduino Multiple Things

Several Things at a Time

how is that?

This tutorial may be of help. It gives an example of how to use millis() and a simple state machine that uses a very similar sequence to the one you want.

I will check this out. Thank you.

Hello guys . I'm using ultrasonic sensor for a parking project . I'm having some problems with the millis function. I want to count the time when a car is on the parking (distance<15) and stop counting when it leaves and prints the interval.

So interval is suppose to the time spent by the car on the parking.

Please need help.:pray:

OK, so carefully read #2 in this thread again. :grin: