WHILE condition does not stop PUMP and SOLENOID

Hello, I am new to arduino. I want to drain the water of tank to a given level "FCE_VYPOUSTENI" at preset time. I have two water level sensors (UPPER and LOWER). The sensors are SEN0368. When I run the code than PUMP and SOLENOID work properly but when the condition is reached "while (VYMENA_VODY_LOWER_senzor_inPIN == 0);" the pump and solenoid still working....
The sensors works well (when I run serial monitor the 0/1 appears as expected). I am lost. Anyone have any ideas?

CODE:

#include <Wire.h> 		  	           
#include <DS3231.h> 			          
#include <LiquidCrystal_I2C.h> 		          

#define SOLENOID 3                            
#define PUMP 8                      
#define VYMENA_VODY_LOWER_senzor_inPIN 5    	
#define VYMENA_VODY_LOWER_senzor_modePIN 6  

int den;				                            
int hodina;				                          
int minuta;				                          
int sekunda;			                        
                      
bool VYMENA_VODY_LOWER_senzor = 1;        

DS3231 rtc; 			                          
RTCDateTime datumCas;                     
LiquidCrystal_I2C lcd(0x27, 20, 4);         

void setup () {
  Serial.begin(9600);			               
  Wire.begin();			                     
  rtc.begin();                            
  //rtc.setDateTime(__DATE__, __TIME__);  
  //rtc.setDateTime(__DATE__, "12:34:56");
  lcd.init();				                     
  lcd.backlight();			                  
  digitalWrite(SOLENOID, LOW);              
  digitalWrite(PUMP, LOW);        
  pinMode(VYMENA_VODY_LOWER_senzor_inPIN, INPUT); 
  pinMode(VYMENA_VODY_LOWER_senzor_modePIN, OUTPUT); 
  digitalWrite(VYMENA_VODY_LOWER_senzor_modePIN, VYMENA_VODY_LOWER_senzor); 
  }
void FCE_HODINY () {                        
  datumCas = rtc.getDateTime();          

  den = datumCas.day;
  hodina = datumCas.hour;
  minuta = datumCas.minute;
  sekunda = datumCas.second;

  lcd.setCursor(0,0);			
  lcd.print("Cas: ");			
  lcd.setCursor(10, 0); 
  lcd.print(hodina); lcd.print(":"); 
  if (minuta <10) {lcd.print(0); lcd.print(minuta);} 
    else {lcd.print(minuta);} lcd.print(":");
  if (sekunda <10) {lcd.print(0); lcd.print(sekunda);} 
    else {lcd.print(sekunda);} 
  }

void FCE_VYPOUSTENI () {
  if (minuta == 20 && sekunda == 0) 
    do {pinMode(SOLENOID, HIGH); pinMode(PUMP, HIGH);}     
    while (VYMENA_VODY_LOWER_senzor_inPIN == 0); 
    }

void loop () { 
  FCE_HODINY ();
  FCE_VYPOUSTENI (); 
}

You are missing a digitalRead by the look of it. Currently, your loop compares the PIN number to zero rather than whether it is high or low.

do {pinMode(SOLENOID, HIGH); pinMode(PUMP, HIGH);}     
    while (VYMENA_VODY_LOWER_senzor_inPIN == 0); 

Please explain what you are trying to do here. Did you mean to use digitalWrite() rather than pinMode() ?

When will VYMENA_VODY_LOWER_senzor_inPIN ever equal 0 ? Did you mean to use digitalRead() to get the state of the pin ?

1 Like

hello, thank you. I have already tried (sorry this is my first version of sketch) use "digitalWrite" instead of "pinMode". but with the same result.

Did you fix the missing digitalRead()) too ?

Please post your full revised sketch

Hello,

Correction: when I use digitalWrite nothing gonna happen (when pinMode then PUMP and SOLENOID works but still the condition WHILE not quit PUMP an SOLENOID)

I tried this (with inserted "Serial.print(digitalRead(VYMENA_VODY_LOWER_senzor_inPIN) );" and when serial monitor is running the expected 0/1 is displayed depending on whether the sensor detects water...

code:

#include <Wire.h> 		  	           
#include <DS3231.h> 			          
#include <LiquidCrystal_I2C.h> 		          

#define SOLENOID 3                            
#define PUMP 8                      	
#define VYMENA_VODY_LOWER_senzor_inPIN 5    
#define VYMENA_VODY_LOWER_senzor_modePIN 6  

int den;				                            
int hodina;				                          
int minuta;				                          
int sekunda;			                        
                      
bool VYMENA_VODY_LOWER_senzor = 1;          

DS3231 rtc; 			                          
RTCDateTime datumCas;                       
LiquidCrystal_I2C lcd(0x27, 20, 4);         

void setup () {
  Serial.begin(9600);			                
  Wire.begin();			                      
  rtc.begin();                            
  //rtc.setDateTime(__DATE__, __TIME__);  
  //rtc.setDateTime(__DATE__, "12:34:56");
  lcd.init();				                     
  lcd.backlight();			                  
  digitalWrite(SOLENOID, LOW);              
  digitalWrite(PUMP, LOW);        
  pinMode(VYMENA_VODY_LOWER_senzor_inPIN, INPUT); 
  pinMode(VYMENA_VODY_LOWER_senzor_modePIN, OUTPUT); 
  digitalWrite(VYMENA_VODY_LOWER_senzor_modePIN, VYMENA_VODY_LOWER_senzor); 
  }
void FCE_HODINY () {                        
  datumCas = rtc.getDateTime();           

  den = datumCas.day;
  hodina = datumCas.hour;
  minuta = datumCas.minute;
  sekunda = datumCas.second;

  lcd.setCursor(0,0);			
  lcd.print("Cas: ");			
  lcd.setCursor(10, 0); 
  lcd.print(hodina); lcd.print(":"); 
  if (minuta <10) {lcd.print(0); lcd.print(minuta);} 
    else {lcd.print(minuta);} lcd.print(":");
  if (sekunda <10) {lcd.print(0); lcd.print(sekunda);} 
    else {lcd.print(sekunda);} 
  }

void FCE_VYPOUSTENI () {
  if (minuta == 9 && sekunda == 20) 
    do {pinMode(SOLENOID, HIGH); pinMode(PUMP, HIGH); Serial.print(digitalRead(VYMENA_VODY_LOWER_senzor_inPIN)); Serial.println();}     
    while (digitalRead(VYMENA_VODY_LOWER_senzor_inPIN == 0)); 
    }

void loop () { 
  FCE_HODINY ();
  FCE_VYPOUSTENI (); 
}
while (digitalRead(VYMENA_VODY_LOWER_senzor_inPIN == 0));

I think that you probably meant

while (digitalRead(VYMENA_VODY_LOWER_senzor_inPIN) == 0);
1 Like

oops...

Are you understand nothing about the logic of this code line?

if I put the brackets like this, the result is always 1...

I'm new to arduino, I think it should work like this, I don't understand why it doesn't work...

like this:

while (digitalRead(VYMENA_VODY_LOWER_senzor_inPIN == 0));

or this ?

while (digitalRead(VYMENA_VODY_LOWER_senzor_inPIN) == 0);

Sorry, but in neither case the result will be "always 1"

It doesn't

It is because you are not reading the state of the pin. You are reading the state of VYMENA_VODY_LOWER_senzor_inPIN == 0 which will always be false, ie LOW or 0

thank you for your help, but I think I read the state of the pin:
#define VYMENA_VODY_LOWER_senzor_inPIN 5

When I do serialPrint I get 0 or 1 if water is or is not detected....
Serial.print(digitalRead(VYMENA_VODY_Dolni_senzor_inPIN));

I apologize if I'm talking nonsense, but I'm new to arduino. Thanks for any help.

Are you seen a digitalRead() insude of your print statement? - it read the state of the sensor.
And if we return to the while condition - there is no digitalRead()

digitalRead(VYMENA_VODY_Dolni_senzor_inPIN)

does read the state of the pin, but in your code you have

digitalRead(VYMENA_VODY_LOWER_senzor_inPIN == 0)

which is not the same thing at all

I have no idea, I tried this but the same result:

  int xyz;
  if (minuta == 22 && sekunda == 30) 
    do {pinMode(SOLENOID, HIGH); pinMode(PUMP, HIGH); xyz = digitalRead(VYMENA_VODY_LOWER_senzor_inPIN); Serial.print(xyz); Serial.println();}     
    while (xyz == 0);

xyz is still "1"...

Is this what you meant ?

do {
    digitalWrite(SOLENOID, HIGH);
    digitalWrite(PUMP, HIGH);
    Serial.print(digitalRead(VYMENA_VODY_LOWER_senzor_inPIN));
    Serial.println();
} while (digitalRead(VYMENA_VODY_LOWER_senzor_inPIN) == 0);

Thank you for your time and I appreciate your help. When i use DigitalWrite nothing gonna happen. When I use pinMode then PUMP works but the WHILE condition will not stop the PUMP. Maybe the question is why DigitalWrite does not work...

I dont know if it can help but there is sketch for the sen0368 I used:

I also control the PUMP and SOLENOID (both 12V) by relay but I think it has no impact on the issue.

You set the mode of the pin (INPUT or OUTPUT) not whether the pin is HIGH or LOW

If you use digitalWrite() to set the state of the pin, ie whether it is HIGH or LOW but only if it has been set to a mode of OUTPUT using pinMode() which is usually done in setup()

Please write a sentence explaining what the do loop should actually do

If the preset time occurs, then the pump and solenoid valve are switched on. The water tank is emptied until the liquid sensor detects liquid (changes its state).
The problem is that the pump and solenoid valve do not turn off, so the tank empties further.... which is undesirable.
The sensor responds correctly over the serial line, even the signal light on the sensor turns on/off.