Trouble with simple code, i think

Hey all,

I am relatively new to micro controllers and am having some problems with my relatively easy code. I have an LCD screen that I want to display values. I have down how to control the LCD. What I want to do is compare values, the actual value (from a thermistor) and a target value (user selected). The LCD screen I am using has 5 buttons and I want to use the oriented vertically. The analog read from them is 1433 (up) and 3303 (down). I was trying to get myself started by just getting one part of the code to work which was a positive increase of value every time I press the up button and then have the value displayed to verify my work. Here is my code so far:

// include the library code:
#include <LiquidCrystal.h>

int tempUpPin = 5;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
int TU = 0;
int val = 0;

pinMode(tempUpPin, INPUT);

void loop() {

int temp = 70; //set a baseline
int TU = analogRead(0); //set analong read variable for temp up
if (TU == 1433){
int val = temp + 1;
lcd.setCursor(0,0); // set cursor top left corner
lcd.print(“Value:”); // print value
lcd.setCursor(0, 1); // set cursor bottom left corner
lcd.print(val); // print increased value
delay(50); // delay 50 ms

Nothing is displayed on my LCD with this code, any suggestions?
Thanks all

 if (TU == 1433){

Well, a ten bit ADC...

The analog read from them is 1433 (up) and 3303 (down). I


The analog read from them is 1433 (up) and 3303 (down).

The analogRead function returns a value in the range 0 to 1023. Neither of the values you show are in that range. Where did those numbers come from?

analogRead() only returns 0-1023, so the == 1433 can never be true.


Thats the values I get when I checked them. I pressed the buttons and with the analog read and wrote all the numbers down.

Where did you check them and with what...OK I just read what you did.


You can't get those values from analogRead() unless there's a bug in it.

Can you replace loop with

void loop() {

 int TU = analogRead(0);   //set analong read variable for temp up
 Serial.println (TU, DEC);

And tell us the results when you press the buttons.


I used the LCD screen and the printed the values from analogRead (0), pressed each button and recorded the displayed value. Of the five buttons I got, 7393, 5023, 1433, 3303, and 0023.

See above, I just edited.

I suspect you aren't printing the numbers correctly to the LCD.


The new values are 739, 502, 143, 330 , and 0. What was that DEC variable for?

What was that DEC variable for?

Just to print in decimal.

Those values look better, so you have a bug in the LCD printing.


I added the last 2 lines...

void loop() {

int TU = analogRead(0); //set analong read variable for temp up Serial.println (TU, DEC); lcd.setCursor(0,0); lcd.print(TU); }

And still get my same wacky values, it appears the numbers that don't change, stay on the screen, hence why all my values had a extra 3 on the end. How do I de-bug something like this? Is my screen messed up?

I've not used the LCD library, does lcd.print() accept an int?

Maybe it should have a string as the argument.


OK I see, you have a 3 there from another print and it's staying there. You need to clear the screen or at least the few characters at that position.


Well at any rate, back to my original question. How can I set up my code to keep incrementing the user input every time a button is pressed. It seems to my like when the loop repeats it wont hold the set value, but return to the default.

you need a global variable to hold the users target value.
like int tempUpPin = 5; then it will hold its value between calls of loop().

I'm guessing the value is 70 regardless of the button pressing.

Do you want "temp" to increase?

If so move it out of loop() and make it a global.

Also save the new value back into temp, you are not doing that at present. And get rid of val, it's doing nothing.


 int temp = 70; 
void loop() {
 //// other code

  lcd.setCursor(0,0);    // set cursor top left corner
  lcd.print("Value:");   // print value
  lcd.setCursor(0, 1);   // set cursor bottom left corner
  lcd.print(temp);       // print increased value
  delay(50);             // delay 50 ms

 //// other code


It wont revert back to back to 5? Say I advance it 5 times so that I want an output of 10, when the loop repeats wont it throw a 5?

Time to post the current code again I think. You haven't mentioned rolling over from 10 to 5 before.

If you want to count 5, 6, 7, 8, 9, 10 , 5 then

temp++; if (temp == 11) temp = 5;


But I want to have it selectable.. the target range is gonna be 100 - 120 so if I set a default to start at say 90, and I press the button 20 times to get 110, wont every loop bring it back to 90?

Probably best to give some insight, on the task.

It ends up being a motor control variable, moving an object in either direction based on the measured temperature, and the desired temperature. So I want it to be able to move back and forth at any given time and hold the position that satisfies the given parameter.

Is it quite possible that what I wanna do can not be done? I just thought that maybe a potentiometer scaled to represent my desires inputs would work best. Wouldn't look, or function like my original intention, but its a temporary solution...