Invalid operands of types 'float()' and 'double' to binary 'operator>' Without modulo

Hi, I'm Vitandin I am learning to code and my main interst is arduino.
I'm trying to
use a HC-SR04 to count the volume of water contained in a bucket,
the shape of the bucket is not regular so i put liter by liter and took note,
then I made an array with this info in which the position on the array matches with the quantity of liters i did not have the sensor first, so I made an adaptation to C++ until they worked but then i adapted it to arduino I.E Serial.println insteado of cout, and it gave me this error:

invalid operands of types 'int()' and 'double' to binary 'operator>'

i first tried by converting the int number to be compared (the medir_distancia variable) into a float
but it was the same but:

invalid operands of types 'float()' and 'double' to binary 'operator>'

I did not find anything like this on google,
So I tried several things, and what """"worked"""" was cutting off the floating number I.E 77 instead of 77.7 , but it does not let me have a 1 liter precision , for example 63.7 and 63.1 are in different "liter steps" .
Could yo please give me an orientation or help me to solve this??
Thank you so much!

const byte trigg = 3;
const byte echo = 2;

float litros[] = {77.7,76.6,75.8,75.0,74.3,73.5,72.8,72.1,71.4,70.7,70.0,69.4,68.1,67.4,66.8,66.2,65.6,65.1,64.4,63.7,63.1,62.4,61.8,61.1,60.5,60.0,59.4,58.8,58.2,57.7,57.1,56.6,56.0,55.4,54.8,54.3,53.7,53.1,52.6,52.0,51.4,50.8,50.3,49.7,49.2,48.6,48.0,47.4,46.8,46.2,45.6,45,44.5,44.0,43.5,42.5};


void setup(){
  Serial.begin(9600);
  pinMode(trigg, OUTPUT);
  pinMode(echo, INPUT);
  digitalWrite(trigg,LOW);

}

void loop(){
  Serial.println(mide_distancia());
int n=0;

if ( mide_distancia > 77.7){
Serial.println("VACÍo");
}
else
{
while (mide_distancia<litros[n])
  {
    n=n+1;
  }
}
if (litros[n] < 42.5)
  { 
    Serial.println("musho");
    
  }
  else{
Serial.println(n);
Serial.println("litros");
}
}

int mide_distancia(){
  gatilla();
  long tiempo = pulseIn(echo,HIGH);
  int distancia = tiempo / 58;
  return distancia;
}

void gatilla(){
  digitalWrite(trigg,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigg,LOW);
}

ps: sorry for my bad English :sweat:

inside the if and while statements, you used
mide_distancia instead of mide_distancia().
Since this is a function, you need to have the brackets after it.

Another thing to note:
In the function, your input value (integer) is divided by an integer. Because of the way c++ works, the quotient is rounded down.

For example, if tiempo = 57, distancia = 0; if tiempo = 59, distancia = 1;
Because of the way integer division works in c++, it will return you a rounded down integer. If you want to return a float or double, you need to convert one of the values to a float for the division to return a float. c++ - Why does dividing two int not yield the right value when assigned to double? - Stack Overflow

GRACIAAAAAAS

Thankyou Soooo Muuuuuchhhh

i put

  int miden_distancia = mide_distancia();

at the begining of the void loop()
then miden_distancia in
every conditional and it worked

:smiley: thank you carrot

1 Like