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?
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?
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.
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.
{
// 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.
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 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.
//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.