comparing two numbers returning they are differente

I'm trying to display the last temperature registered in an NTC Sensor only if that temperature is not equal to the last one displayed. The code is suppost to follow this logic:

  • Show current temperature;
  • If current temperature is different than the last one show it to the serial monitor;
temperatura_c = (int)temperatura_c;
   if(!(ultima_temperatura == temperatura_c)){
      ultima_temperatura = temperatura_c;
      Serial.print("Current Temperature: ");
      Serial.println(temperatura_c);
      // print out the value you read:
      Serial.print("last Temperature: ");
      Serial.println(ultima_temperatura);
      Serial.println("#################################");
      delay(2000);
    
   }

The ouput of the program:

Why is this saying that 13 is different than 13?
Is my if statment wrong?

temperatura_c = (int)temperatura_c;

That is useless.

Why is this saying that 13 is different than 13?

Because 13.0000000000098 is not the same as 13.0000000000099.

Thank you Paul for your answer.

If I try to compare two floats I can get 12.123456789 and the next temperature can be 12.12345678 and it will always show the last temperature because it'll always be different (mathematically speaking).

Is there any way to make the program convert 12.123456789 to 12 and only 12 not 12.0, 12.000001 or 12.0001?

Is there any way to make the program convert 12.123456789 to 12 and only 12 not 12.0, 12.000001 or 12.0001?

float realTemp = 12.123456789;
int tempAsInt = realTemp;
Serial.print("tempAsInt = ");
Serial.println(tempAsInt);

will print tempAsInt = 12 every day of the week.

It's dangerous to mix types when checking for equality unless you understand the rules for type promotion pretty well, even then it's bad style and will get you in trouble if promotion results in comparison of floats.

I'm still having problems with displaying the last variable.

This is my current code:

int ultima_temperatura;

//Note here that temperatura_k is a float
//I have tried casting it to int but withtout luck
int temperatura_c = temperatura_k - 275.15;

if(!(ultima_temperatura == temperatura_c)){
      ultima_temperatura = temperatura_c;
      Serial.print("Current Temperature: ");
      Serial.println(temperatura_c);
      // print out the value you read:
      Serial.print("last Temperature: ");
      Serial.println(ultima_temperatura);
      Serial.println("#################################");
      delay(2000);
}

It is still showing the same 13 over and over again. I would like it to show 13 once and only show another number is the number is different than 13.

Thank you for bearing with me.

Try this:

if(!(ultima_temperatura != temperatura_c)){ // if temperatures are Not Equal, then do the updating & printing

Take the absolute value of the difference of the two floats, and if it is less than, say, 0.001, assume the values are identical.

Try this:

That will evaluate to true if the temperatures are equal.

OP: Why not make ultima_temperatura an int, too?

Or, put temperatura_c back to float, and use

  if(abs(temperatura_c - ultima_temperatura) > 1.0E3)
   {
      // The temperature changed by at least 0.001 degrees C
   }

Technically, the program is probably doing what you intend.

What you PROBABLY want is to print out the temperature if you've crossed some kind of threshhold.

if (abs(newTemp - oldTemp) > .5) { //do something }

That makes more sense (to me) than casting to an integer and checking if it changed.

Also, if you use "int" to change 12.1 to 12, keep in mind, it will change 11.9 to 11 and 12.9 to 12. It's probably not exactly what you want. It doesn't round. It takes the integer.

You'll get a change if the temp changes from 11.99999999999 to 12.000000001 but not if the temp changes from 12.0000000001 to 12.9999999999.

We are, of course, exaggerating the differences between the values that you are comparing. The characteristics of your temperature sensor will dictate what discrete steps there will be between temperature readings.

PaulS:
That will evaluate to true if the temperatures are equal.

OP: Why not make ultima_temperatura an int, too?

Or, put temperatura_c back to float, and use

  if(abs(temperatura_c - ultima_temperatura) > 1.0E3)

{
      // The temperature changed by at least 0.001 degrees C
  }

I have tried that. I have changed everything to float and did

if(abs(temperatura_c - ultima_temperatura) > 1.0E1){
      ultima_temperatura = temperatura_c;
      Serial.print("Current Temperature: ");
      Serial.println(temperatura_c);
      // print out the value you read:
      Serial.print("last Temperature: ");
      Serial.println(ultima_temperatura);
      Serial.println("#################################");
      delay(2000);
    
   }

You should note that 1.0E1 and not 1.0E3. I have tried showing up to 30 digits and the number is exactaly the same.

13.14351231231230000000000000

and

13.14351231231230000000000000

Also I'm not really concerned in beeing too specific at temperature changing. I would like it to show only if it has changed 1ºC. I'm not really interested in 0.1ºC changes or 0.01ºC.

Oh my god. I'm getting really pissed at it right now.

I have tried converting everything that uses temperatura in my code to int still no luck.

I have this:

const float Ra = 1000.0;
const float ganho = 1.2;
const float R0 = 3300.0;
const float beta = 4090.0;
const float T0 = 298.15;
const int ntc = A1;

float temperatura_k = 1 / ((log((Ra*(ganho*5/vout2)-1)/R0))/beta+(1/T0));

Where every variable is constant besides vout2 that is:

int sensorValue = analogRead(ntc);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
float vout2 = voltage;

The rest of the code follows:

float temperatura_c = temperatura_k - 275.15;

if(!(temperatura_c == ultima_temperatura)){
      ultima_temperatura = temperatura_c;
      Serial.print("Current Temperature: ");
      Serial.println(temperatura_c);
      // print out the value you read:
      Serial.print("last Temperature: ");
      Serial.println(ultima_temperatura);
      Serial.println("#################################");
      delay(2000);
    
   }

I'm really don't know where I'm going wrong.

You just might need to show the whole code.

I think we're all assuming you're getting the next temperature correctly. You don't actually show where the temp is read. The way it looks now, you just have temp_c, set ultima_temp to temp c, and keep looping. Where are you reading the temperature?

You should do a test where you just "make up" or input temperatures using the serial monitor, and see if you get the behavior you want.

if(abs(temperatura_c - ultima_temperatura) > 1.0E1){

You want to show a new temperature value only when the difference is greater than 1.0 degrees?

I have tried converting everything that uses temperatura in my code to int still no luck.

You seem to have misspelled int, using 'f', 'l', 'o', 'a', 't'.

//Declaracao constantes para poder utilizar no calculo da temperatura em funcao do vout
const float Ra = 1000.0;
const float ganho = 1.2;
const float R0 = 3300.0;
const float beta = 4090.0;
const float T0 = 298.15;
const int ntc = A1;

//Delcaracao constantes para os LED's
const int LED_t = 8;
const int led_Verde = 9;
const int led_Amarelo = 10;

//Declaracao constantes para as temperaturas de referencias
const int t_ref1 = 4;


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(LED_t, OUTPUT);
  pinMode(led_Verde, OUTPUT);
  pinMode(led_Amarelo, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(ntc);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);

  float vout2 = voltage;
  float ultima_temperatura;
  
  
  float temperatura_k = 1 / ((log((Ra*(ganho*5/vout2)-1)/R0))/beta+(1/T0));

 
  float temperatura_c = temperatura_k - 275.15;
  

  
   if(temperatura_c <= t_ref1){
      digitalWrite(led_Amarelo, LOW);
      digitalWrite(led_Verde, LOW);
      digitalWrite(LED_t, LOW);
   }
   if(temperatura_c > (t_ref1 + 5)){
      digitalWrite(led_Amarelo, LOW);
      digitalWrite(led_Verde, HIGH);
      digitalWrite(LED_t, LOW);
   }
   if(temperatura_c > (t_ref1 + 8)){
      digitalWrite(led_Amarelo, HIGH);
      digitalWrite(led_Verde, LOW);
      digitalWrite(LED_t, LOW);
   }
   if(temperatura_c > (t_ref1 + 10)){
      digitalWrite(led_Amarelo, LOW);
      digitalWrite(led_Verde, LOW);
      digitalWrite(LED_t, HIGH);
   }

   
   if(!(temperatura_c == ultima_temperatura)){
      ultima_temperatura = temperatura_c;
      Serial.print("Current Temperature: ");
      Serial.println(temperatura_c);
      // print out the value you read:
      Serial.print("last Temperature: ");
      Serial.println(ultima_temperatura);
      Serial.println("#################################");
      delay(2000);
    
   }

Here is the entire thing. Don't mind the comments since it's in Portuguese.

I'm not trying to "push the problem" to you guys. I'm just really lost. I have been sitting around for 3 hours trying to figure this out but I'm not getting near the answer.

You're back to the situation where you're comparing two floats instead of doing a threshhold test.

Replace

if(!(temperatura_c == ultima_temperatura))

with

if (abs(temperatura_c - ultima_temperatura) > .5)

Anyway, that works fine when I test it.

I just thought of something :

Are you actually trying to do this. . .

if(!(temperatura_c == ultima_temperatura)){
      Serial.print("Current Temperature: ");
      Serial.println(temperatura_c);
      // print out the value you read:
      Serial.print("last Temperature: ");
      Serial.println(ultima_temperatura);
      Serial.println("#################################");
      delay(2000);
      ultima_temperatura = temperatura_c;    
   }

is that what's hanging you up?

 static float ultima_temperatura;

AWOL:

 static float ultima_temperatura;

It didn't work out. I'll probally leave this as it is since I have to move on on project.
Thank you all for your help and bearing with me.

Wish you all a happy christmas and a happy new year.