Pages: [1]   Go Down
Author Topic: 16x2 LCD displays all single digit integers as 'x0' rather than '0x'  (Read 1480 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I'm playing around with an LCD module, and I've got it working all fine, except for one little thing: when it should be displaying numbers from 1-9, it displays 10-90 rather than 01-09. What is the best way to solve this problem?

Here is my code:
Code:

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

int des = 0;


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.print("des");  // Print a message to the LCD.
  Serial.begin(9600);
  }

void loop() {
 
 des =  (analogRead(1)/34.14);

 
 
 
  lcd.setCursor(0, 1);
  lcd.print(des);
  delay(10);

}

The hardware is all working fine, it's just the software. Also, when I start the project  up, if the value of des is less than 2 digits, it reads correctly, that is, 1,2,3,4 etc. Once the value goes above 2 digits however, if des returns to a one digit number, a zero is tacked onto the back.

Thanks for your help.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26943
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You would seem to need to define des as a floating point # for the math involved
analogRead, which is 0 to 1023, divided by 34.14

Instead, use the mapping function to map 0-1023 into 1-30.
http://arduino.cc/en/Reference/Map

basically does this:
if analogRead is 0-30, des  = 1
31-60, des = 2
61-90, des = 3
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately that didn't work. It seems like once the LCD has displayed two digits, it doesn't want to get rid of the second and so just displays a 0.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I solved it, I forced the Arduino to 'print' a blank square to the LCD if des <=9. Kind of annoying, but it worked.
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 35
Posts: 4299
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I solved it, I forced the Arduino to 'print' a blank square to the LCD if des <=9. Kind of annoying, but it worked.
You have discovered that once you display a character on the LCD it stays there until overwritten.   A more generic way to handle this situation when you have to update some data periodically is to:
(1) set the cursor to the position of the first character.
(2) display as many spaces as the largest number of characters that could possibly be displayed.
(3) reset the cursor back to the position of the first character.
(4) display your new data.


Don
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed my code to your suggestion, thanks for that. You learn something new every day, hey?
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7188
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is what I do if I need some well-defined texts,
Code:
char msg[];
int x;
x=analogRead(0);
sprintf(msg,"Input:%4d",x);
lcd.print(msg);

The above way eliminate the need to print out blank spaces, it always outputs up to 4 digits for the analog read and has left blank padding if input is less than 4 digits. It also eliminates the overloaded method of lcd.print(int) or lcd.print(float), which are very expensive on the FLASH. In most of my programs, I never use lcd.print(any data type) except for lcd.print(string).
You can do %04d if you want zero padding
Logged


Pages: [1]   Go Up
Jump to: