Pages: [1]   Go Down
Author Topic: Thermostat code  (Read 1653 times)
0 Members and 1 Guest are viewing this topic.
Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys,
I know this isn't gonna be the hardest among all the problems posted here but I just can't figure out why this isn't working.
Basically I've been trying to make a simple thermostat with a LM35 temperature sensor,a 10K rotary potentiometer and a lcd screen.
The potentiometer would give the desired temperature on a range from 0 to 30 °C ,and if the temperature returned from the LM35 was lower than the one calculated from the pot ,a led would light up.
The desired temperature and the actual one would respectively be shown up on the lcd screen.
Here's the code:
Code:
#include <LiquidCrystal.h>
int led=7; //led pin


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

  lcd.begin(16, 2);
  pinMode(led,OUTPUT);
  Serial.begin(9600);  //I've added this to monitor the two temperatures on serial screen,so i would have know it wasn't just a lcd problem
}

void loop() {
  float temp; //actual temperature
  int fine;  //desired temperature
 
  lcd.setCursor(0,0);

  fine=((30*analogRead(5))/1024); //calculating the desired temp.
  lcd.print("Temp finale:");  //printing it on lcd and serial monitor
  lcd.setCursor(14,0);
  lcd.print(fine);
 
  Serial.print("Temp finale:");
  Serial.println(fine);
 
  lcd.setCursor(0, 2);
 
  lcd.print("Temp:");
  Serial.print("Temp attuale:");

  temp=(5.0 * analogRead(0) * 100.0) / 1024; //calculating the actual temp.
  lcd.setCursor(11,2);
  lcd.print(temp);
  Serial.println(temp);
 
 if(temp<fine){ //lighting up the led
    digitalWrite(led,HIGH);
  }
  else
    digitalWrite(led,LOW);
 
  delay(2000); //update frequency,otherwise the numbers on the lcd would flicker
 
}

The problem is that if the pot is rotated completely to 0,the lm35 will work correctly. When i start rotating it,the actual temperature will start dropping and then,from a certain point of the pot (about 1/3) would start rising until getting about 37°C
with the desired temp. on 30 °C.
I've tested the code and the hardware in many ways and everything works fine singularly;but whenever i put the code for the desired temperature and the actual one together,everything goes 'crazy'. I'm sure it's not an lcd problem 'cause it gives the same problem even on lcd screen.
I'll put a fritzing file for the wiring,
thanks in advance.

* Termostato.fzz (10.77 KB - downloaded 15 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  fine=((30*analogRead(5))/1024); //calculating the desired temp.
Integer arithmetic will drive you crazy, until you figure out that that is the problem, and deal with it.

Embedding function calls in statements like this makes it very difficult to troubleshoot. As you've been able to print other values, you should have been able to print the potentiometer reading, except that it is embedded in the math.

Isolate both calls to analogRead(), storing the value read, and then print that value. Does turning the potentiometer actually affect the reading of the temperature sensor? It shouldn't.
Logged

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

I modified the code to this:
Code:
int readingP,readingS;
 
  lcd.setCursor(0,0);
 
  readingP=analogRead(5);
  fine=((30*readingP)/1024); //calculating the desired temp.
  lcd.print("Temp finale:");  //printing it on lcd and serial monitor
  lcd.setCursor(14,0);
  lcd.print(fine);
 
  Serial.print("Temp finale:");
  Serial.println(fine);
 
  lcd.setCursor(0, 2);
 
  lcd.print("Temp:");
  Serial.print("Temp attuale:");
  readingS=analogRead(0);
  temp=(5.0 * readingS * 100.0) / 1024; //calculating the actual temp.
  lcd.setCursor(11,2);
  lcd.print(temp);
  Serial.println(temp);
,but it still gives that problem.
I even tried printing the single readings without processing them,and yeah the sensor one is affected by the pot.
Could it be due to a little short I made for mistake the other day? It lasted 1-2 seconds though,and everything else works fine anyway,so I actually don't think it's that..but I just can't figure out what's not working.
Logged

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

Code:
LiquidCrystal lcd(., ., 5, ., ., .);

Code:
int tempratureGoal = ((30 * analogRead(5)) / 1024);

Pin 5 is doing double duty how?


Yeah right ,I didn't notice it! But changing the two pin to A0 and A5 didn't change anything though..
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Could it be due to a little short I made for mistake the other day? It lasted 1-2 seconds though,and everything else works fine anyway,so I actually don't think it's that..but I just can't figure out what's not working.
Are all the analog pins affected? If so, it's time to go shopping.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post the post change code.
Logged

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

Quote
Could it be due to a little short I made for mistake the other day? It lasted 1-2 seconds though,and everything else works fine anyway,so I actually don't think it's that..but I just can't figure out what's not working.
Are all the analog pins affected? If so, it's time to go shopping.
Yup they're all affected in the same way..even if I remove the pot reading code and just get the temperature itself,but still rotate the pot it goes nut..dammit!
Logged

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are reading a pot that reads from 0V to 5V.  The Arduino's A2D reads the voltage in 1024 steps, thus each step represents 5/1024 ~= 5mV
This maps to 0 = 0oC and 1023 = 30oC.

The LM35 reads 10mV for each degree C, thus 250mV corresponds 25oC.  This means the highest reading should be no more than 300mV.
300mV will be a reading of about 60.

I would read in the raw A2D reading and then use the map() function to compare apples to apples.

So something like this:
Code:
 fine = analogRead(5); //read the raw temp data
  fine = map(fine, 0, 1023, 0, 30); // adjust the raw reading to a scale from 0 to 30

then later

Code:
 
int temp;

temp = analogRead(0); //read the actual raw temp.  should be 0 to 60 for the 0 to 30[sup]o[/sup]C range
temp /= 2;                 // adjust a2d to our range

I think that works. Let me know if I'm crazy.
Logged

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

You are reading a pot that reads from 0V to 5V.  The Arduino's A2D reads the voltage in 1024 steps, thus each step represents 5/1024 ~= 5mV
This maps to 0 = 0oC and 1023 = 30oC.

The LM35 reads 10mV for each degree C, thus 250mV corresponds 25oC.  This means the highest reading should be no more than 300mV.
300mV will be a reading of about 60.

I would read in the raw A2D reading and then use the map() function to compare apples to apples.

So something like this:
Code:
 fine = analogRead(5); //read the raw temp data
  fine = map(fine, 0, 1023, 0, 30); // adjust the raw reading to a scale from 0 to 30

then later

Code:
 
int temp;

temp = analogRead(0); //read the actual raw temp.  should be 0 to 60 for the 0 to 30[sup]o[/sup]C range
temp /= 2;                 // adjust a2d to our range

I think that works. Let me know if I'm crazy.
I don't think you're crazy,I'd rather say I'm just unlucky ,'cause it still doesn't work :/
Anyway just to let you know,the short I was talking about was that I reversed the +Vin and Gnd pins of the lm35 ,causing it to incredibly overheat ,I even burned my finger removing it. Strangely it still works fine,but if i put any combination of analog pins with the sensor and the pot they just affect each other..what did I do?  smiley-roll-sweat

EDIT: And in case I fried the Arduino,where is the problem most likely located in? Should I get a brand new board,or just a new ATMega chip?
« Last Edit: February 14, 2013, 03:49:44 pm by Dadu » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And in case I fried the Arduino,where is the problem most likely located in? Should I get a brand new board,or just a new ATMega chip?
Most likely just a new chip (with bootloader already installed).
Logged

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

Quote
And in case I fried the Arduino,where is the problem most likely located in? Should I get a brand new board,or just a new ATMega chip?
Most likely just a new chip (with bootloader already installed).
And by chance,is there any way I could be sure of what is actually broken? (I mean between the chip and the board)
I've been running few tests to see the "reactions" of the analog reading plugging in various variable power sources and they all work fine singularly. Some of them go smooth even if plugged among some other but not in the case of the sensor (which could have just burned itself instead,though I don't figure out why does it still work fine singularly) . Thanks in advance for your help,even if i'm going little OT now..
Logged

Pages: [1]   Go Up
Jump to: