Pages: [1]   Go Down
 Author Topic: 16x2 LCD displays all single digit integers as 'x0' rather than '0x'  (Read 1426 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 36
 « on: April 24, 2011, 12:05:44 am » Bigger Smaller 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() {

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.

 Logged

Global Moderator
Boston area, metrowest
Online
Brattain Member
Karma: 524
Posts: 26473
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
 « Reply #1 on: April 24, 2011, 12:13:18 am » Bigger Smaller 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
Newbie
Karma: 0
Posts: 36
 « Reply #2 on: April 24, 2011, 12:31:52 am » Bigger Smaller 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
Newbie
Karma: 0
Posts: 36
 « Reply #3 on: April 24, 2011, 01:22:55 am » Bigger Smaller 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
Karma: 32
Posts: 4258
 « Reply #4 on: April 24, 2011, 08:15:07 am » Bigger Smaller 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.

Don
 Logged

Offline
Newbie
Karma: 0
Posts: 36
 « Reply #5 on: April 27, 2011, 01:38:02 am » Bigger Smaller Reset

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

Central MN, USA
Offline
Tesla Member
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
 « Reply #6 on: April 27, 2011, 09:16:18 am » Bigger Smaller Reset

Here is what I do if I need some well-defined texts,
Code:
char msg[];
int x;