Pages: [1] 2   Go Down
Author Topic: IF temperature > 24 --> hot hot hot  (Read 1013 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
#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:

Code:
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?




 
« Last Edit: March 14, 2013, 05:58:25 pm by lomohelm » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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?




Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

alabama
Offline Offline
Full Member
***
Karma: 1
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lomohelm,
Code:
if (argument)
{
action;
}
'nuff said
TomJ
Logged

Einstein once said you don't really understand anything until you can explain it to your Grandmother

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
if (temperature <= 24)

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

Code:
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. smiley

« Last Edit: March 14, 2013, 06:48:40 pm by noobdude » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12520
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

alabama
Offline Offline
Full Member
***
Karma: 1
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No if statement will work without CURLY BRACES.
Logged

Einstein once said you don't really understand anything until you can explain it to your Grandmother

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No if statement will work without CURLY BRACES.

Incorrect, but including them is always recommended.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4679
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.

Code:
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 '.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think he was posting actual code just idea like I was. smiley
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4679
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try reading the first post again.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks heaps for the help!

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

Logged

Pages: [1] 2   Go Up
Jump to: