Need help with efficient memory use
bulkhead
Posts: 405
I believe this line of code is what gives me the out of memory error when it is run for about 10 seconds. I think it's something to do with my method of converting the int to a string to be written on the serial LCD. Is there a better way to do this?
public static void checkUltrasonic() { LCD.clearScr(); LCD.write("US: "); LCD.write(ultrasonicSensor.getCm()+""); }
Comments
First the string buffer
I don't know if the LCD class you are using has a method for write(stringBuffer), if not, I can dig out the code from the class I wrote for the LCD I normally use.
or you can use the format class available below.
groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/util/text/
I usually use the buffer when converting ints to strings, and the format class for more complex formating tasks.
Post Edited (Jon Keinath) : 8/8/2006 2:53:24 AM GMT
then the format class does it all in a single statement
char[noparse]/noparse buf = new char[noparse][[/noparse]10]; //textbuffer, select appropiate size
Format.sprintf(buf,"US: %03d",ultrasonicSensor.getCm());
LCD.write(buf);
All you need to know·is that buf·holds a null terminated string (aka ASCIIZ).
So you need a method LCD.write(char[noparse]/noparse s) in your lcd class
void write(char[noparse]/noparse s) {
· int i=0;
· while (s[noparse][[/noparse]i]!=0) write(s[noparse][[/noparse]i++]); //call the write(char c) method for each char
}
regards peter