Non-contact Liquid Level Switch

Hi

I am using a FIT0212 Non-contact Liquid Level Switch and a small micro servo motor. I want the servo motor to close a lid of a bin when the level inside is stationary for 10 seconds.

The content I am throwing into the bin triggers the sensor when passing. I want the servo motor to only close the lid when the input from the sensor is consecutively HIGH for 10 seconds.

Any help on this will be much appreciated.

herman007:
Hi

I am using a FIT0212 Non-contact Liquid Level Switch and a small micro servo motor. I want the servo motor to close a lid of a bin when the level inside is stationary for 10 seconds.

The content I am throwing into the bin triggers the sensor when passing. I want the servo motor to only close the lid when the input from the sensor is consecutively HIGH for 10 seconds.

Any help on this will be much appreciated.

I guess, then, everything is working properly except the 10 second stationary test?

Paul

Hi Paul

I implemented a HC-SR04 ultrasonic distance sensor now as well to open the lid with the servo motor. So the lid opens when the distance sensor is triggered and closes after 4 seconds. I want the lid to close and not be able to open again when the content inside is stationary for 10 seconds.

Attached please find the code.

I will really appreciate it if you can help

#include <Servo.h>   //servo library

Servo servo;     
int trigPin = 2;    
int echoPin = 3;   
int servoPin = 9;


int sensorIN = 4;
int sensorOUT = 13;

bool checkTenSeconds();

long duration, dist, average;   
long aver[3];   //array for average


void setup() {       
    servo.attach(servoPin);  
    Serial.begin(9600);
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    pinMode(sensorIN, INPUT);
    pinMode(sensorOUT, OUTPUT);
    servo.write(0);         //close cap on power on
    delay(1000);
    servo.detach();
} 

void measure() {  
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1;    //obtain distance
}

bool checkTenSeconds(int8_t PIN) {
 //uint32_t start=millis();
  while(digitalRead(PIN)== HIGH){ 
    uint32_t start=millis();//
    if(millis() > start + 10000) 
    return true;
  }
  return false;
}

void loop() { 
  for (int i=0;i<=2;i++) {   //average distance
    measure();               
   aver[i]=dist;            
    delay(50);              //delay between measurements
  }
 dist=(aver[0]+aver[1]+aver[2])/3;    //average distance by 3 measurements

if ( dist<30 ) {
 //if hand on the distance 10...30 cm
 servo.attach(servoPin);
  delay(1);
 servo.write(50);  
 delay(4000);       //wait 5 seconds
 servo.write(0);    
 delay(1000);
 servo.detach(); 
}
//Sensor code

  //read the switch value into a variable
  int sensorVal = digitalRead(4);
  //print out the value of the liquid level
  Serial.println(sensorVal);
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  }
  else {
    digitalWrite(13, HIGH);
  }

 if (checkTenSeconds(sensorVal)) {
   servo.write(0);
   exit(0);
   }

}

The first step is to determine the range of dist values you get when the level is stationary. They will not always be in the same range. That will let you know what the possible dist values are for stationary.

Paul