# Programming error in my thermostat sketch

Hi,

I am a newbie, trying to make a simple temperature monitor. What I do is to measure the temperature, write it on the LCD screen, also check if the mmeasured temperature is the highest one measured since the beginning of the program, and if it is so, write it as the new maximal temperature.

Here is part of the sketch:

void loop(){

float derece; float maxderecenew; float maxdereceold; float voltaj; voltaj = analogRead (temp) * 0.004882814; derece= (voltaj-0.5)* 100.0; maxdereceold = 0; ekran.print ("Oda "); ekran.print (derece); ekran.print (" derece"); if (derece > maxdereceold) { maxderecenew=derece; } else { maxderecenew=maxdereceold; } ekran.setCursor (0,1); ekran.print ("Maximum "); ekran.print (maxderecenew); ekran.print (" C"); delay (2000); ekran.clear ();

I tried to create 2 variables, maxdereceold and maxderecenew. There is also tha variable for the actual temperature (derece). If the actual temperature is higher than the maxdereceold (the highest temperature recorded), then maxderecenew (the highest temperature recorded and written) should be changed. When I run the program both derece and maxderecenew get tje same values and change together. Where is my error?

Use code tags. Post your code. Make persistent values global or static.

THis logic is wrong

``````  if (derece > maxdereceold)
{
maxderecenew=derece;
}
else
{
maxderecenew=maxdereceold;
}
``````

If the current tempertaure is greater then the greatest recorded so far - remember this new maximum. If the current temperature is NOT greater than the last greatest ... do nothing. This "else" in your code is the root of your problems.

Note AWOLs comment: By placing the variables inside the loop() function they will have new "random" values on every loop. Place them outside.

And as it only is a fraction of your code, the above suggestion is just a suggestion

I am sorry, I forgot to use the code tags. Won’t happen again.

Thanks for the replies. I have tried to modify the code according to your suggestions. Here is the code in its integrity:

``````#include <LiquidCrystal.h>
LiquidCrystal ekran(12,11,5,4,3,2);
int temp = 0;
int red = 8;
int sari = 7;
float derece;
float maxderecenew= 0;
float voltaj;

void setup (){
ekran.begin (16,2);
ekran.clear ();
pinMode (sari, OUTPUT);
pinMode (red, OUTPUT);
}

void loop(){

voltaj = analogRead (temp) * 0.004882814;
derece= (voltaj-0.5)* 100.0;
ekran.print ("Oda ");
ekran.print (derece);
ekran.print (" derece");
if (derece > maxderecenew)
{
maxderecenew=derece;
}
ekran.setCursor (0,1);
ekran.print ("Maximum ");
ekran.print (maxderecenew);
ekran.print (" C");
delay (2000);
ekran.clear ();
if (derece<26){
digitalWrite (sari, LOW);
digitalWrite (red, LOW);
}
else if (derece >28){

digitalWrite (red, HIGH);
digitalWrite (sari, LOW);
}
else if (26<derece<28){

digitalWrite (sari, HIGH);
digitalWrite (red, LOW);
}
}
``````

In the old code I think having two variables (maxdereceold and maxderecenew) for the same purpose was quite unnecessary. I changed that. I also changed where my variables are declared. It is working now.

One additional question, if you have time. You can see that I have added a 2000 ms delay when refreshing the lcd. If i use a lower value, the lcd flickers too much. However when I add this delay, the led lights are also delayed: When temperature is over 28 degrees, the red light should turn on, and it does immediately if I use a small delay, but if I use a long delay, it isn’t immediate. Where should I put the delay, so that it only affects the lcd refresh rate and not the LED reaction time?

Study the concepts in the Blink Without Delay example sketch and get rid of delay() altogether.

I haven't studied it but I presume your new sketch is now capturing the max temperature properly?

...R

else if (26<derece<28){

is not correct logic as 26<derece computes to 0 or 1… always < 28

replace with “else” only

replace with "else"

Replace with "&&"

By the way, I made an interesting observation (at least, interesting for me): The Arduino is powered by the USB port. I was leaving for a few hours so I put the computer to sleep mode. Immediately, there has been a 2 degrees decrease in the temperature sensor's reading,. When I woke the system up, there has been an immediate increase. I assume the reason is a difference in the voltage provided to Arduino when the computer is in sleep mode (there probably is a decrease). This is a problem, as I cannot be sure which value is the correct one and it isn't reliable. Is it possible to correct this problem?

You're performing an analog read to get the temperature. That relies on a stable analog reference voltage. If the supply voltage changes and causes the analog reference to change, this will affect the analog read results. You could fix the problem by providing a stable analog reference voltage, or switching to a digital temperature sensor.

PeterH:
You’re performing an analog read to get the temperature. That relies on a stable analog reference voltage. If the supply voltage changes and causes the analog reference to change, this will affect the analog read results. You could fix the problem by providing a stable analog reference voltage, or switching to a digital temperature sensor.

Does “Providing a stable analog reference voltage” mean using an external power source? If I used batteries and they started to drain after a few days, would the same thing happen again?

Another interesting observation is about the values the temperature sensor reads: They are always the same numbers (25.20 , 27.64, 27.15, 26.66, 26.17, etc. They all seem to be 0.5 degrees apart. I assume this is due to the sensitivity of the temperature sensor being 0,5 degrees. Are there any more sensitive sensors out there (like 0,1 or even better 0,01 degrees) ?

..due to the resolution of the ADC.. It will be a litt better if u use 3.3V referense The sensor itself is within ?? (see datasheet)