Rewriting code for working with ultrasonic sensor

I have a liquid dispensing system which works with an IR sensor module (goes high and low), and a motor for dispensing the liquid. The liquid is dispensed when the IR sensor goes low (statusSensor in the code) and is not dispensed when the sensor goes high. Also, if the sensor is continuously interrupted for a few seconds (set by Mtime in the code), the motor is turned off to prevent wastage of liquid or false sensor detection. Also, a float switch is used to determine liquid levels in the tank, and the motor is automatically turned off if the switch(switchsensor in the code) goes high.

Now, i want to implement the same using an ultrasonic sensor (HRC module) instead of IR. I believe that for ultrasonic, I have to send send a signal from the trigger pin and wait for the reflection on the echo (something like low to high) pin to determine the distance to take further action. Now, I am confused on how to implement the timer part of cutting off the motor if the sensor is continuously interrupted for a few seconds, since I have to generate a signal, wait for it to be received and then calculate the distance. In case of IR sensor it was simple where I could monitor the sensor status (low) for a few seconds if continuously low (by starting a timer when the motor started first). Please find below my code with IR sensor. Please suggest on how to implement the same with ultrasonic sensor.

const int out1 = 5; //motor
const int IRSensor = 3; // connect ir sensor to arduino pin 6
const int LED = 6; // conect Led to arduino pin 9
const int Switch = 2;
const int Mspeed = 175;//0-255
const int Mtime =1500;//500-1000
int statusSensor=1; //static
int switchSensor=1; //static
unsigned long prevTime;
volatile int count =0;
volatile int count1=0;
volatile int count2 =0;
volatile int count3=0;
volatile int buttonstate=0;
volatile int lastbuttonstate=0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
  pinMode(out1,OUTPUT);
  pinMode (IRSensor, INPUT); // sensor pin INPUT
  pinMode (LED, OUTPUT); // Led pin OUTPUT
  pinMode(Switch, INPUT_PULLUP);
}


void loop() {
  // put your main code here, to run repeatedly:
  delay(100);
  switchSensor = digitalRead (Switch);
  statusSensor = digitalRead (IRSensor);
  if (count1==1) 
  { prevTime = millis();
    Serial.println(prevTime);
  }
  if (millis() - prevTime >= Mtime) 
  { switchoffmotor();
    //Serial.println("going to else");
  }
  checksensors();
   
}

void checksensors(){
  buttonstatus();
  if (statusSensor == 0 and switchSensor == 0 and count3==0){
   
     analogWrite(out1,Mspeed);
     //digitalWrite(out1,HIGH);
     count1=count++;
     Serial.println(count);
     digitalWrite(LED, LOW);
  }
   else if (statusSensor == 0 and switchSensor == 1) {
      
      //digitalWrite(out1,LOW);
      digitalWrite(LED, HIGH);
      analogWrite(out1,0);
      count=0;
      count1=0;
      prevTime = millis();
      }
   else if (statusSensor == 1 and switchSensor == 0) {
      
      //digitalWrite(out1,LOW);
      digitalWrite(LED, LOW);
      analogWrite(out1,0);
      count=0;
      count1=0;
      prevTime = millis();
      }
    else {
      //Serial.println ("ir high");
      //digitalWrite(out1,LOW);
      digitalWrite(LED, HIGH);
      analogWrite(out1,0);
      count=0;
      count1=0;
      prevTime = millis();
      buttonstatus();
      }
} 
void switchoffmotor(){

    
      //Serial.println ("ir high else");
      analogWrite(out1,0);
      count=0;
      count1=0;
      prevTime = millis();
      count2++;
      count3=count2;
    
      //analogWrite(out1,0);
  }

void buttonstatus(){
  buttonstate=digitalRead (IRSensor);
   if (buttonstate != lastbuttonstate) {
    // if the state has changed, 
    if (buttonstate == LOW) {
      count3=0;
    }
    delay(50);
   }
   lastbuttonstate = buttonstate;
}

I don't know what a "HRC"-module exactly is. I do have some HC-SR04- ultrasonic sensors.

Without a datasheet how the sensor works it i svery hard to make suggestions.
The HC-SR04-sensors works this way

Initiate a distance measuring though a pulse on the triggerpin
measure time it takes for the ultrasonic-pulse to come back reflected from the surface.
measuring the runtime of the pulse is done with the command pulsein

here is a basic demo-code that shows the basic principle

const int echoPin = 2;
const int trigPin = 3;
long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);  
  Serial.begin(9600);
}
void loop() {
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(1000);
}

inside your program you have to measure all the time through looping. switching the motor on/off depends
on the measured distance and the measured time with a distance smaller than ....

best regards

Stefan

As an addition. your IR-Sensor "gives back" low or high.
instead of the digitalRead(IRSensor) you write another function US_Sensor().

inside the function US_Sensor the measuring is done and there is an if-statement that gives back a value 0 or 1
which represents near-enough / too far away.

instead of

statusSensor = digitalRead (IRSensor);

you code

statusSensor = US_Sensor();

then the rest of your code will stay the same.

best regards

Stefan

StefanL38:
I don't know what a "HRC"-module exactly is. I do have some HC-SR04- ultrasonic sensors.

Without a datasheet how the sensor works it i svery hard to make suggestions.
The HC-SR04-sensors works this way

Initiate a distance measuring though a pulse on the triggerpin
measure time it takes for the ultrasonic-pulse to come back reflected from the surface.
measuring the runtime of the pulse is done with the command pulsein

here is a basic demo-code that shows the basic principle

const int echoPin = 2;

const int trigPin = 3;
long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
  Serial.begin(9600);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(1000);
}



inside your program you have to measure all the time through looping. switching the motor on/off depends 
on the measured distance and the measured time with a distance smaller than ....

best regards 

Stefan

My bad. it is HC-SR04. Thanks for your inputs.

So whatever you have put here in the loop function, I put this in another function called US_sensor() which will in turn return a 1 or a 0 based on the distance I set. And I put statusSensor=US_sensor() instead of digitalRead(IR sensor).

Also, I have a buttonstatus() function in my code posted above for IR sensor which checks if the IR sensor status has changed. But in case of ultrasonic sensor, in the buttonstatus(), should I monitor echopin, i.e buttonstate=digitalRead(echopin) or should I read US_sensor() ?

StefanL38:
As an addition. your IR-Sensor "gives back" low or high.
instead of the digitalRead(IRSensor) you write another function US_Sensor().

inside the function US_Sensor the measuring is done and there is an if-statement that gives back a value 0 or 1
which represents near-enough / too far away.

instead of

statusSensor = digitalRead (IRSensor);

you code

statusSensor = US_Sensor();

then the rest of your code will stay the same.

best regards

Stefan

Can you please reply to my post above ?