LCD 16x2 display not correct potentiometer values

Hi, I am new to Arduino and as it must be I face some problems with LCD 16x2.
I try to make elecitrc heaters control with Arduino Uno. With potentiometer it will be possible to adjust temperature which must be kept on room and Arduino will control it. On LCD display I want to display room temperature and setted room temperature.

The problem is that if I increase potentiometer, LCD shows correctly, like:
1
2
3
4
etc.
but if I decrease potentiometer values it shows:
13
12
11
10
90 (instead of 9)
80 (instead of 8)
70 (instead of 7)
meanwhile I checked setted temp via Serial.println , value is OK.

Here is my sketch

// connected 10K potenciometer to adjust screen brightness. 
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int tempC;
int reading;
int tempPin = 1;
int pot = 0;
int relay1 = 6;
int relay2 = 7;
int relay3 = 13;

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Set T");
  lcd.setCursor(0,1);
  lcd.print("Room  T");
  
  pinMode (relay1, OUTPUT);
  pinMode (relay2, OUTPUT);
  pinMode (relay3, OUTPUT);
}

void loop() 

{
  int valueT = analogRead (pot);    // reading pot values
  /* needed temp adjustment values 0 - 15C
  potentiometer max value is jumping from 1022 to 1023 
  and max temp is changing from 14 to 15. To stabilize it
  max pot value 1023 is +1 and max temp is + 1 */
  int setTemp = map (valueT, 0, 1024, 0, 16);    //mapping it to set possible temperature range
  
  analogReference (INTERNAL);
  reading = analogRead(tempPin);
  delay (50);
  reading = analogRead(tempPin);  // reading analog temperature sensor LM35
  tempC = reading / 9.31; //converting voltage to temperature. 
  analogReference (DEFAULT);
  
  // set cursors on LCD to display data 
  lcd.setCursor(12, 0);
  lcd.print(setTemp);
  lcd.setCursor (15, 0);
  lcd.print ("C");
  lcd.setCursor(12, 1);
  lcd.print(tempC);
  lcd.setCursor (15, 1);
  lcd.print ("C");
  Serial.println (setTemp);
  // heater control
  
  
  delay (500);
  
}

Any ideas why it is happening and how to solve it?

Thank You in advance!

I don't have a lot of programming experience but what I see is a variable "tempC" that should be a float
declared as an "int" .
What's up with that ?
How can you use an int for a temp ?
I know you only have 16 adjustment values but if you are going to display temp wouldn't you
want to display it as a float ? (as you have shown it "9.31")

 analogReference (INTERNAL);
  reading = analogRead(tempPin);
  delay (50);
  reading = analogRead(tempPin);  // reading analog temperature sensor LM35
  tempC = reading / 9.31; //converting voltage to temperature. 
  analogReference (DEFAULT);

What's the analogReference about (INTERNAL/DEFAULT) ?
What is that for ?
Also, since you are asking for help, it might be a good idea to actually explain your wiring
of the settemp pot. Everyone knows an Arduino does not measure resistance, so you
must be converting it to voltage so how about stating the value of the pot and how you
have wired it so everyone is on the same page ?

Any ideas why it is happening and how to solve it?

It is because anytime you send a character to the LCD it will remain there until it is overwritten.

void loop()
. . .
  lcd.setCursor(12, 1);
  lcd.print(tempC);
. . . 
delay(500);

Lets say that the first time around the loop the temperature is 10.
You will send 1 0 to the display and the display will show '10'

Now the temperature drops to 9.
You send 9 to the display and the display shows 90.
The 9 comes from the value you just sent and the 0 is left over from the previous display of 10.

The most straightforward way to deal with this is:

void loop()
. . .
  lcd.setCursor(12, 1);
  lcd print("    ");
  lcd.setCursor(12, 1);
  lcd.print(tempC);
. . . 
delay(500);

Don

raschemmel:
I don't have a lot of programming experience but what I see is a variable "tempC" that should be a float
declared as an "int" .
What's up with that ?
How can you use an int for a temp ?
I know you only have 16 adjustment values but if you are going to display temp wouldn't you
want to display it as a float ? (as you have shown it "9.31")

What's the analogReference about (INTERNAL/DEFAULT) ?
What is that for ?
Also, since you are asking for help, it might be a good idea to actually explain your wiring
of the settemp pot. Everyone knows an Arduino does not measure resistance, so you
must be converting it to voltage so how about stating the value of the pot and how you
have wired it so everyone is on the same page ?

Thank you for replay.
int for temperature I left when I build datalogger. And it was not allowed by IDE to choose float for temp when data was used in String. I change it to float...
For analogReference I used this method Arduino Playground - HomePage because I use LM35 temp sensor. Sorry that was not indicated in sketch.
Regarding wirring. I measure voltage value from potentiometer (connected to analog0 pin) and map it to my wanted temperature range.

floresta:

Any ideas why it is happening and how to solve it?

It is because anytime you send a character to the LCD it will remain there until it is overwritten.

void loop()

. . .
  lcd.setCursor(12, 1);
  lcd.print(tempC);
. . .
delay(500);



Lets say that the first time around the loop the temperature is 10.
You will send 1 0 to the display and the display will show '10'

Now the temperature drops to 9.
You send 9 to the display and the display shows 90.
The 9 comes from the value you just sent and the 0 is left over from the previous display of 10.

The most straightforward way to deal with this is:


void loop()
. . .
  lcd.setCursor(12, 1);
  lcd print("    ");
  lcd.setCursor(12, 1);
  lcd.print(tempC);
. . .
delay(500);




Don

So simple, so effective. I understand.
I appreciate that!
Thank You!

I guess what I was asking is the value of your pot and the voltage you connected it to (3.3V or 5V) ? (you only have two choices)
BTW, are you familiar with the DS18B20 ONEWIRE digital temp sensor ? (12 bit resolution)

I connected pot to 5V if I correctly understood the question..
Yes, I read about DS18B20 temp senors and I have already ordered them. Meanwhile, I still play with LM35....
If you can recommend good link for begginer to start with DS18B20, please share it..
Thank You.

This library seemed to be the most reliable.
http://www.pjrc.com/teensy/td_libs_OneWire.html
I have a bunch of DS18B20 working programs at home , some for lcds , both I2C and 4-bit, and have had as
many as 6 sensors on the same bus working with this library. When you have more that two sensors it
is more practical to use serial output so you can see all the info.
If you don't care about the 64-bit unique ROM code in each device you can comment out that and just display the
temp.
I found that it helps to have a 4.7k ohm pullup resistor and the midde pin and also that "parasite" mode is
very tricky and doesn't always work. It is more practical to just connect power to pin-3 than trying to use
parasite mode.
The attached file is one of the ones I have used. It came from the following link.
Here is the link for the forum tutorial:
http://playground.arduino.cc/Learning/OneWire
Here is another tutorial:
http://www.hobbytronics.co.uk/ds18b20-arduino
This page shows the pullup resistor:

The most important advice I can give you regarding the DS18B20 is that don't expect the same results
from different programs or different libraries. There is also a Dallas Semiconductor Library and some of
those examples did not work (don't know why). Keep good records ! Every time you try a new or different
program, record the results. Some will actually work without the pullup resistor, some won't. Some will
work with multiple sensors only if they all have pullups. Some will work with only one pullup for all the
sensors. I still haven't figured that out yet.
Here is a more concise description:

OneWire.zip (15.3 KB)

And it was not allowed by IDE to choose float for temp when data was used in String. I change it to float...

Where is the String ?
Read this post on setting decimal position for floats in a print statement.
http://forum.arduino.cc/index.php?topic=45943.0