NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Finding the length of a string for LCD updating

July 20, 2011
by carlhako
carlhako's Avatar

Hi

I will start with my problem

Say I write this to the LCD screen on its own line.

"Output: 100"

later on that output has changed from 100 to 10, instead of clearing the whole screen and re writing to the LCD I use the function to select that same line in the LCD and this time write

"Output: 10"

What I see on the LCD at this point is still "Output: 100" that last 0 is still there. The easiest solution is just outputting a couple of space on the end this wont work in my situation tho where I want to use the whole 16 characters long (2x16 lcd)

What I am trying to do is work out how many characters long my string is and then generate a for loop and write spaces till ive hit the 16 characters. I have come up with this bit of code which actually works but I am sure there is a more efficient way of achieving this.

uint16_t temp_int;
int length;
temp_int = 500;
char lcd_len[16];
temp_len = sprintf(lcd_len,"test %u",temp_int);

temp_len ends up with what I am after "8" the amount of characters but I am wasting a char array 16 bytes big.

Is there a more efficient way of doing this without wasting the ram? I have no need for the array anywhere else in my program.

Thanks

Carl

July 20, 2011
by Noter
Noter's Avatar

Why not just write the 16 spaces to the lcd line with your for loop before printing the data?

July 20, 2011
by Robotnik
Robotnik's Avatar

putting the int in a 16 char long field like this should work...

sprintf(buffer,"%-16d",number);
July 20, 2011
by Robotnik
Robotnik's Avatar

err I mean 10 char long field in your example. So it pads the string with spaces to the end of the lcd.

July 20, 2011
by carlhako
carlhako's Avatar

Noter I will give that a go. That sounds like the simplest solution lol. Sometimes there are such basic solutions. There is a function to clear the entire lcd but you can see it clear and refresh its not a smooth update.

I would still like to figure out a easy way to calculate the actual length of a string without having to create a char just for that purpose. This would be for making sure I dont go over the 16 character limit for eg.

Robotnik your solution still requires a char with 10 elements? I am not sure if this will work as I will be writing various things to the lcd, mixing strings, ints etc. Say if i want to write 15 characters to the lcd and buffer is only 10 chars long this wont work?

July 20, 2011
by Noter
Noter's Avatar

There are many string functions. strlen() is the one that returns the length of a string.

July 20, 2011
by carlhako
carlhako's Avatar

I tried strlen() but the problem is my variables are in a unsigned int 8 or 16bits big, I would need to stick everything into a char first which is what i was trying to avoid. I dont think there is anyway around it.

July 20, 2011
by Noter
Noter's Avatar

I'm unsure how you are storing a string in 16 bit integers but for the 8 bit type just cast it to a type char for the function ...

iLength = strlen((char*)a_uint8_array);
July 20, 2011
by carlhako
carlhako's Avatar

Ah that makes sense. I will try that out.

I am not storing them into a uint16_t array just a standard uint16_t variable. with a value of 1200 for example.

July 20, 2011
by Noter
Noter's Avatar

Unless you don't already have strings and just have 8/16 bit numbers. You'll have to format them as a string before you can anything with them. Using sprintf() is a good way to go or there are string conversion functions too. They'll have to be formatted to some type of string for the display. If the line is already blank then no need to worry about clearing it to the end after the string is printed.

July 20, 2011
by Noter
Noter's Avatar

Something like this ... I have not tested this code -

stdin = stdout = &lcd_stream;

...

lcd_goto_position(iLine,0);
for(i=0;i<16;i++) lcd_write_byte(' ');   // clear the line
printf_P(PSTR("%d"),iValue);
July 20, 2011
by Noter
Noter's Avatar

Oops - Guess another position is needed since clearing moved the cursor away -

lcd_goto_position(iLine,0);
for(i=0;i<16;i++) lcd_write_byte(' ');   // clear the line
lcd_goto_position(iLine,0);
printf_P(PSTR("%d"),iValue);
July 21, 2011
by carlhako
carlhako's Avatar

Found the solution it was right under my nose

int length;
uint16_t temp = 5000;
length = fprintf_P(&lcd_stream, PSTR("test %u"), temp);

length will = 9 in this example.

the variable "length" needs to be declared as an int. This wont work if you want to find out how long your string is before printing to the LCD.

Post a Reply

Please log in to post a reply.

Did you know that Pulse Width Modulation (PWM) can be used to control the speed of a motor digitally? Learn more...