Hello everyone
I use the following code to control water level in a 21 cm tank
The system contains an ultrasonic sensor: pin 8 + 9
Humidity sensor pin A0
And two relays "
Pin 7 pump operator
And pin 10 activates solenoid
Pin-13-Buzzer
The problem is already starting in the pump activation loop
Normally I need the water level not to exceed 14 cm, which means that the pump will turn off by 14 cm
For some reason the system works in the opposite way, the pump is turned on when the level is above 14 cm and falls below it
All connections are normal, the tragir reaches the relay incorrectly
Would appreciate help
#include<LiquidCrystal.h> // include the library code for lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //
#define echopin 9 // echo pin
#define trigpin 8 // Trigger pin
int maximumRange = 50;
long duration, distance, level;
void setup() {
lcd.begin(16,2);
Serial.begin (9600);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT );
pinMode (4, OUTPUT);
pinMode (13,OUTPUT);
pinMode (7,OUTPUT);
pinMode (10,OUTPUT);
}
void loop ()
{
{
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
duration=pulseIn (echopin,HIGH);
distance= duration/58.2;
level=21-distance;
delay (50);
// Serial.println(distance);
Serial.print(level);
Serial.println(" cm");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("water level :");
lcd.print(distance);
delay(100);
}
if (level <= 13 ){
digitalWrite (7,HIGH);// connect to relay(motor)
digitalWrite (13,HIGH);
lcd.setCursor(0,1);
lcd.print("Motor Started");
delay(100);
}
else if (level >=14) {
digitalWrite (7,LOW); // connect to relay(motor)
digitalWrite (13,LOW);
lcd.setCursor(0,1);
lcd.print("Tank is full");
delay(100);
}
int s = analogRead(A0); //לקחת דוגמה
Serial.print(s); Serial.print(" - ");
if(s >= 1000) {
Serial.println("Sensor is not in the water or DISCONNECTED");
digitalWrite(10, LOW);
Serial.println("*20");
}
if(s < 1000 && s >= 600) {
Serial.println("Tank is safe");
digitalWrite(10, LOW);
Serial.println("*20");
}
if(s < 600 && s >= 370) {
Serial.println("Tank is HUMID - Warning");
lcd.setCursor(0,0);
lcd.print("Warning !!!");
digitalWrite(13, HIGH);
digitalWrite(10, HIGH);
Serial.println("*21");
delay(2000);
digitalWrite(10, LOW);
Serial.println("*20");
digitalWrite(13, LOW);
}
if(s < 370) {
Serial.println("Sensor in Water - Overload");
lcd.setCursor(0,0);
lcd.print("!!! Overload !!!");
digitalWrite(10, HIGH);
Serial.println("*21");
digitalWrite (7,LOW); // התחברות לממסר(pump) כיבוי משאבה
Serial.println("*10");
digitalWrite(13, HIGH);
delay(4000);
dig
}italWrite(13, LOW);
delay(50);
}