how to print on LCD from right to left..?

Normally when we write to lcd the numbers are on the left side,
If the number is variable the numbers automatically shifts to the right.

But I want to display it in the other way..
the 0 should be on the left side, and as the number increases it should shift to the left side.
How do I do it..??

the 0 should be on the left side

What 0? Cite some examples, for clarification.

You probably will have to format the number as a string, yourself (using sprintf), and display that string.

Lets say just this code

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);

}

void loop() {
  lcd.setCursor(0, 0);
   lcd.print(millis()/1000);
}

This code prints the increments of numbers and as the number keeps increasing they gradually prints the next unit on the next block...

I want it to print on the extreme right, as they increase the first unit will shift to the left accordingly to display the next unit...

like when you type numbers on your phone the number gradually shifts to the rleft making space for the next number..

What you are looking for is called right justified printing, as opposed to the default left justified printing. You'll have to code that yourself. The method that comes to mind is:

Determine the length of the string you wish to print (if this is a numerical value, you'll have to convert it to a string to determine the length, or write your own routine that can determine the character length of the numeric value). There are C library functions already available to perform the char string conversion for you. Either sprintf() or itoa() will work. sprintf() is more flexible, but creates a larger binary. There's also a C library function that will tell you the length of a char string, strlen().

Once you know the length of your string, move the cursor to lcd_character_width - string_length, and print your string from that position.

What you are looking for is called right justified printing, as opposed to the default left justified printing. You'll have to code that yourself.

The coding that you will have to do consists of changing the LCD 'Entry Mode' from 0x06 to 0x07 so that the 'display shift' is turned on. Check out the really interesting demonstration at Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos .

Don.

Joy:
Lets say just this code

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
 lcd.begin(16, 2);

}

void loop() {
 lcd.setCursor(0, 0);
  lcd.print(millis()/1000);
}





This code prints the increments of numbers and as the number keeps increasing they gradually prints the next unit on the next block...


I want it to print on the extreme right, as they increase the first unit will shift to the left accordingly to display the next unit...


like when you type numbers on your phone the number gradually shifts to the rleft making space for the next number..

I think your description is confusing people (or at least me). It is not clear what you mean by
"unit", "first unit", "next unit", and "next block".
Can you give specific examples?

I'm trying to figure out if you are wanting to simply print right justified numbers or you want to
print the digits backwards and have the individual digits scroll to the left as they print the final
right justified number.

If all you want to do is print a right justified number, use sprintf() to create
the string then print the string to the display.

For that sized display, it would be something like

char buf[17];
void loop() {

  sprintf(buf, "%16d", millis()/1000);
  lcd.setCursor(0, 0);
   lcd.print(buf);
}

--- bill

Bill: He describes what he wants to do in reply #2:

I want it to print on the extreme right, as they increase the first unit will shift to the left accordingly to display the next unit...

like when you type numbers on your phone the number gradually shifts to the left making space for the next number..

This appears (to me) to be exactly what implementing the 'display shift' capability of the LCD controller does as shown in the fourth animation in the link I mentioned in reply #4. My phone doesn't work that way, but some do.

Don

Thanks Don,
I totally missed that last sentence about the phone display.

The phone display example kind of sounds like right justification.

I think it was that first post that really threw me.
"... [start] on the left side, and as the number increases it should shift to the left side".

When starting on the left I couldn't figure out how to shift any further to the left side
maybe that first "left" should have been "right"?

--- bill

Thank you all for your replies... :slight_smile:

What I want to do exactly, Don got it correctly... :slight_smile:
Thanks Don..

This is what I want to do..

Typing Hakan and the letters shifts left side :slight_smile:

Thank you bill...

Your code just worked the way I wanted right away..

char buf[17];
void loop() {

  sprintf(buf, "%16d", millis()/1000);
  lcd.setCursor(0, 0);
   lcd.print(buf);
}

Now I have written this code to display the pot value..It prints on the left and gradually shifts right as the value increases...

But I am unable to do it your way, printing the value on the right and gradually shift left as the value increases..

char buf[17];
void loop() {

  sprintf(buf, "%16d", millis()/1000);
  lcd.setCursor(0, 0);
   lcd.print(buf);
}

I can help you with what you have to do but you (or someone else) will have to fill in the details.

  1. You have to reposition the cursor to the position where you want the characters to first appear. This was set to the middle of the line on the 'Hakan' example. You do this with lcd.setCursor().
  2. You have to have the text be displayed from right to left instead of the default left to right. You do this with lcd.rightToLeft() and later on you undo it with lcd.leftToRight().
  3. You have to turn on autoscrolling so that the new characters push the old ones away from the cursor location. You do this with lcd.autoscroll(). You undo this later with lcd.noAutoscroll().

Now you are ready to send your information to the LCD.

Don

This is a simple, easy and elegant solution:

Use the sizeof operator.

lcd.setCursor(20 - sizeof(max1), 3);
lcd.print(max1, 2);

drewk88:
This is a simple, easy and elegant solution:

Use the sizeof operator.

lcd.setCursor(20 - sizeof(max1), 3);
lcd.print(max1, 2);

Unfortunately that doesn't work - the number of bytes that a variable occupies in memory has nothing to do with the number of characters needed to display the value of the variable.