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 |
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.
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 |
Why not just write the 16 spaces to the lcd line with your for loop before printing the data? |
July 20, 2011 by Robotnik |
putting the int in a 16 char long field like this should work...
|
July 20, 2011 by Robotnik |
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 |
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 |
There are many string functions. strlen() is the one that returns the length of a string. |
July 20, 2011 by carlhako |
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 |
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 ...
|
July 20, 2011 by carlhako |
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 |
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 |
Something like this ... I have not tested this code -
...
|
July 20, 2011 by Noter |
Oops - Guess another position is needed since clearing moved the cursor away -
|
July 21, 2011 by carlhako |
Found the solution it was right under my nose
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. |
Please log in to post a reply.
Did you know that spinning a permanent magnet motor makes a voltage? Learn more...
|