0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« on: March 13, 2011, 09:53:08 am » |
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); 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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: March 13, 2011, 10:03:09 am » |
if (TU == 1433){
Well, a ten bit ADC... The analog read from them is 1433 (up) and 3303 (down). I Seriously?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #2 on: March 13, 2011, 10:03:15 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #3 on: March 13, 2011, 10:05:45 am » |
analogRead() only returns 0-1023, so the == 1433 can never be true.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #4 on: March 13, 2011, 10:16:02 am » |
Thats the values I get when I checked them. I pressed the buttons and with the analog read and wrote all the numbers down.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #5 on: March 13, 2011, 10:17:31 am » |
Where did you check them and with what...OK I just read what you did. BUT 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. ______ Rob
|
|
|
|
« Last Edit: March 13, 2011, 10:24:02 am by Graynomad »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #6 on: March 13, 2011, 10:23:01 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #7 on: March 13, 2011, 10:25:05 am » |
See above, I just edited.
I suspect you aren't printing the numbers correctly to the LCD.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #8 on: March 13, 2011, 10:30:37 am » |
The new values are 739, 502, 143, 330 , and 0. What was that DEC variable for?
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #9 on: March 13, 2011, 10:34:28 am » |
What was that DEC variable for? Just to print in decimal. Those values look better, so you have a bug in the LCD printing. ______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #10 on: March 13, 2011, 10:36:17 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #11 on: March 13, 2011, 10:36:45 am » |
I've not used the LCD library, does lcd.print() accept an int?
Maybe it should have a string as the argument.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6826
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #12 on: March 13, 2011, 10:38:14 am » |
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.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #13 on: March 13, 2011, 10:40:04 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Full Member
Karma: 2
Posts: 110
Kittens eat Arduinos
|
 |
« Reply #14 on: March 13, 2011, 10:59:38 am » |
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().
|
|
|
|
|
Logged
|
|
|
|
|
|