A float variable ignores a conditional if

Hello guys, I have one little problem when a float variable (volumen) is equal to 2, its purpose is that once it is equal to 2, send a notification, reset the variable and make a digitalWrite. The fact is that it doesn’t do this when the variable is equal to 2, it simply keeps increasing its number ignoring the conditional if. The sensor is a YF-s201 flow sensor, I’m using the most recent Blynk library, it’s a ESP32 DevBoard, type server is Cloud. The device connect well to the Blynk App

#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

BlynkTimer timer;


char auth[] = "xxxxx";


char ssid[] = "xxxxx";
char pass[] = "xxxxx";

const int electrovalvPin = 4;
int automaticState = LOW;

volatile int NumPulsos; 
const int sensorPin= 27;    
float factor_conversion=7.11; 
float volumen=0;
long dt=0; 
long t0=0; 
byte sensorInterrupt = 0; 


BLYNK_CONNECTED() {
  Blynk.virtualWrite(V4, automaticState);
}

BLYNK_WRITE(V4) {
  automaticState = param.asInt();
  if (automaticState == HIGH) {
    if (volumen == 2){ //----------Problem section------------
      digitalWrite (electrovalvPin, LOW);
      Blynk.notify(String("Tu planta ha sido regada a la cantidad de agua recomendada, Litros usados: ") + volumen + String("L"));
      volumen = 0;  
    }
  }
}

void ContarPulsos ()  
{ 
  NumPulsos++; 
} 

int ObtenerFrecuecia() 
{
  int frecuencia;
  NumPulsos = 0;  
  interrupts();  
  delay(1000);  
  noInterrupts(); 
  frecuencia=NumPulsos; 
  return frecuencia;
}

void sendSensor()
{
  float frecuencia=ObtenerFrecuecia(); 
  float caudal_L_m=frecuencia/factor_conversion; 
  dt=millis()-t0; 
  t0=millis();
  volumen=volumen+(caudal_L_m/60)*(dt/1000); 

Serial.print ("Caudal: "); 
  Serial.print (caudal_L_m,3); 
  Serial.print ("L/min\tVolumen: "); 
  Serial.print (volumen,3); 
  Serial.println (" L");
  
   Blynk.virtualWrite(V6, caudal_L_m);
   Blynk.virtualWrite(V7, volumen);
}

void setup()
{
  Serial.begin(9600);

  pinMode(sensorPin, INPUT); 
  pinMode(electrovalvPin, OUTPUT);
  
   attachInterrupt(digitalPinToInterrupt(27), ContarPulsos,RISING);
  t0=millis();

Blynk.begin(auth, ssid, pass);


  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}



Your variable is a type ‘float’ - it will look for a decimal place, try:

if (volumen == 2.0) . . . etc

cul
billd

It doesn’t work. I even try to put something like if (volumen == 2.5) or (volumen == 2.55)

Is the value of volumen displaying correctly in Serial Monitor?

Yes

Have you tried testing for ‘2.000’ three decimal places?

being correct set to HIGH?

billd

I tried five minutes ago, didn’t work. And yes, It sets HIGH correctly, as you can see in the code, everything is ok in that part

You may need to try using greater than or less than, because say your float is 2.0045 but shows as 2.00 (to 2dp) then 2.0045 deos not equal 2.0000

You could try:

if (volumen > 2.0 && volumen < 2.1) {

I just tried it, but it did not work either, the number still increasing and ignoring the conditional if. I even tried it like this: if (volumen >= 2.000 && volumen <= 2.100)

Guys, I have noticed that it works if the automaticState vaule it’s disabled (LOW) and activated again (HIGH) in some seconds: send the notification, make the digitalWrite and reset the volumen variable, but it only works if I do that, the purpose is that it works as long as it is activated (Always in HIGH). By the way, I used the conditional like this: if (volumen >= 2) , why this happen? any help?

Just noticed that your also doing a calculation with a long (dt).

Maybe you should have changed:

volumen=volumen+(caudal_L_m/60)*(dt/1000); 

To:

volumen=volumen+(caudal_L_m/60)*(float)(dt/1000);

It didn’t work, the sensor works fine, but the conditional is still ignored

Looking at your code - what you have described is exactly what your code is supposed to do.

I think what you need to do is put this in a timer:

if (automaticState == HIGH) {
    if (volumen == 2){ //----------Problem section------------
      digitalWrite (electrovalvPin, LOW);
      Blynk.notify(String("Tu planta ha sido regada a la cantidad de agua recomendada, Litros usados: ") + volumen + String("L"));
      volumen = 0;  
    }
  }

If not then that section of code will only be called when the state of V4 changes (I guess it’s a button to set auto mode on/off)

1 Like

And how can I put that part in a timer? just need a reference. And yes, you’re right, it suppose to work as a button to set auto mode. Thanks in advance

1 Like

Forget it, I already get put that part in a timer. Many thanks man!!

1 Like

Maybe the if test should be >= 2.