Go Down

Topic: Trouble with simple code, i think (Read 2434 times) previous topic - next topic

Albino Cobra

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

AWOL

Code: [Select]
if (TU == 1433){


Well, a ten bit ADC...

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

Seriously?
"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.

PaulS

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?

Graynomad

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Albino Cobra

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

Graynomad

#5
Mar 13, 2011, 04:17 pm Last Edit: Mar 13, 2011, 04:24 pm by Graynomad Reason: 1
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: [Select]
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
Rob Gray aka the GRAYnomad www.robgray.com

Albino Cobra

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.

Graynomad

See above, I just edited.

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Albino Cobra

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

Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

Albino Cobra

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?

Graynomad

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

Maybe it should have a string as the argument.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

Albino Cobra

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.

dafid

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy