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);
Serial.begin(9600);
}
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
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.
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?
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.
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.
eg
int temp = 70;
void loop() {
//// other code
temp++;
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
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...