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