Pages: [1] 2   Go Down
Author Topic: Trouble with simple code, i think  (Read 2050 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 300
Posts: 26219
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (TU == 1433){

Well, a ten bit ADC...

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Quote
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 Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

See above, I just edited.

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Maybe it should have a string as the argument.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8497
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 2
Posts: 110
Kittens eat Arduinos
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: