IF temperature > 24 --> hot hot hot

EDIT1: put the whole code in here as said in the forum rules. Sorry for posting things like a cowboy.

Hi there,

I've been fiddling around with my Arduino Uno board for a couple of weeks now. I went through the standard lessons to blink LED's and such. I also bought an 2x16 LCD display and i thought I'd make myself a fun thermometer. This worked following the tutorials online. The TMP36 temperature sensor works splendid (However i miswired it first and it became very hot, unpluged it after 3 seconds) and i got the display to print "(temperature) °C".

What I tried to do now was to put a message on the second line of my display when temperature exceeds 24° which i did as so:

#include <LiquidCrystal.h>

// Arduino pins used for LCD
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
    // initialize the LCD display
    lcd.begin(16, 2);
}

void loop() {
    float temperature = 0.0;   // stores the calculated temperature
    int sample;                // counts through ADC samples
    float ten_samples = 0.0;   // stores sum of 10 samples
  
    // take 10 samples from the MCP9700
    for (sample = 0; sample < 10; sample++) {
        // convert A0 value to temperature
        temperature = ((float)analogRead(A0) * 5.0 / 1024.0) - 0.5;
        temperature = temperature / 0.01;
        // sample every 0.1 seconds
        delay(100);
        // sum of all samples
        ten_samples = ten_samples + temperature;
    }
    // get the average value of 10 temperatures
    temperature = ten_samples / 10.0;
    // display the temperature on the LCD
    lcd.setCursor(0, 0);
    lcd.print(temperature);
    lcd.print(" \337C");
    ten_samples = 0.0;
    lcd.setCursor (0,1);
if (temperature > 24) lcd.print("Hot Hot Hot");
}

Now, even when the temperature drops below 24°c the text remains on the screen. so i tried adding:

if (temperature < 24) lcd.print("ice ice baby');

But this only gives a flickering screen changing quickly from ice ice baby to hot hot hot and vice versa. i tried to reset it and then once above 24°c the text remained Hot Hot Hot even when after the first increase of temp. the temp. dropped back below 24.

How can I solve this? Does it have something to do with the wrong wired TMP36? maybe it does not accurately read the temp. anymore since i almost fried it?

At the top of this section there are two related sticky threads in bold.
Please read and understand them

Ok I think I know where the problem lies: if i use the same amount of characters (none being space) it will replace if the temperature drops/increases.

if (temperature > 24) lcd.print("Hot Hot Hot");
if (temperature < 24) lcd.print("ice ice baby!!!!');

However the first 10 seconds the TMP36 acts out of control after putting my warm finger on it, switching between 22 and 24 degrees like a blinking led. I can live with the fact that it is sensitive and with a little bit of patience and acclimatisation this code does the trick.

Nonetheless, How can I do the same trick with an empty second line? in other words: how do I clear the Hot Hot Hot line when temp drops below 24°c?

lomohelm:
Nonetheless, How can I do the same trick with an empty second line? in other words: how do I clear the Hot Hot Hot line when temp drops below 24°c?

Either clear the lcd or set the cursor and print enough blank spaces.

lomohelm,

if (argument)
{
action;
}

'nuff said
TomJ

I'm beginner as well but I'll try to help. You have less than 24 degrees and greater than 24 degrees there. May want to add less than or equal to 24 degrees. Once its 24 degrees exactly what would happen?

if (temperature <= 24)

For clearing the hot hot hot you simply set the cursor to where you need to and

lcd.print("            ");

Just prints spaces. Was this what you're trying to do? Remember that when you want to print the hot hot hot you'll need to set the cursor back to the correct position.

@arrch good call haha! I missed your post.

I'm not sure if there's a better way to do that but it works. Hopefully someone else with more experience can chime in. :slight_smile:

lomohelm:
Ok I think I know where the problem lies: if i use the same amount of characters (none being space) it will replace if the temperature drops/increases.

if (temperature > 24) lcd.print("Hot Hot Hot");

if (temperature < 24) lcd.print("ice ice baby!!!!');

Assuming you want to display one thing or the other, you should use if/else rather than two if statements. (As coded above, it does nothing when the value is exactly 24.)

No if statement will work without CURLY BRACES.

Tumbleweed:
No if statement will work without CURLY BRACES.

Incorrect, but including them is always recommended.

if (temperature > 24) lcd.print("Hot Hot Hot");
if (temperature < 24) lcd.print("ice ice baby!!!!');

What is it supposed to do when (temperature == 24) ? Or maybe if (temperature >= 24) is hot?

Why not have a "Just right baby!" in between Hot and Cold? Ice is way colder than 23 C.

if (temperature < 24) lcd.print("ice ice baby!!!!');

There's something wrong with the syntax at the end of that line above. You start the text to print with " and end with '.

I don't think he was posting actual code just idea like I was. :slight_smile:

Try reading the first post again.

I don't see how that code would flicker quickly between the two texts unless you call once a second quickly. But it depends on how exactly you added that second line into the sketch because that line wouldn't even compile. You need an if else and make it so that at least when anything should change on screen, it also clears the line that used to have text on it, or writes on top of it, but not both at the same time.

Secondly, from what I've seen, temperature sensors can produce spikes or drops. Suddenly the reading would just drop by up to several degrees in my tests. You can add a check on passed time so that your printed line possibly cannot flicker too quickly between states.

A subtle issue that may bite you here is that three exclamation points mean something special to the bootloader - you may want to change your ice message to contain fewer.

Thanks heaps for the help!

I'm at work now but I will try your suggestions later tonight.

You may also want to take multiple readings before you display a value,

Take an average of the temperature over the last 10 or so readings then display that value. I believe there's a useful guide of smoothing on the arduino site.