Go Down

### Topic: 16x2 LCD displays all single digit integers as 'x0' rather than '0x' (Read 2390 times)previous topic - next topic

#### bowlerhatman

##### Apr 24, 2011, 07:05 am
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: [Select]
`[// include the library code:#include <LiquidCrystal.h>int des = 0;// initialize the library with the numbers of the interface pinsLiquidCrystal 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.

#1
##### Apr 24, 2011, 07:13 am
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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### bowlerhatman

#2
##### Apr 24, 2011, 07:31 am
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.

#### bowlerhatman

#3
##### Apr 24, 2011, 08:22 am
I solved it, I forced the Arduino to 'print' a blank square to the LCD if des <=9. Kind of annoying, but it worked.

#### floresta

#4
##### Apr 24, 2011, 03:15 pm
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.

Don

#### bowlerhatman

#5
##### Apr 27, 2011, 08:38 am
I changed my code to your suggestion, thanks for that. You learn something new every day, hey?

#### liuzengqiang

#6
##### Apr 27, 2011, 04:16 pm
Here is what I do if I need some well-defined texts,
Code: [Select]
`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