Shop OBEX P1 Docs P2 Docs Learn Events
Need help with efficient memory use — Parallax Forums

Need help with efficient memory use

bulkheadbulkhead Posts: 405
edited 2006-08-08 13:36 in General Discussion
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

  • Jon KeinathJon Keinath Posts: 146
    edited 2006-08-07 23:38
    Do you need to have a +"" in the last line? The combination (+) of two strings causes the memory error.
  • bulkheadbulkhead Posts: 405
    edited 2006-08-08 02:29
    Well, if I just do LCD.write(int) it will write an int command to the lcd, not print an int on the screen. I have added more of these statements throughout my program (printing int values) and it runs out of memory even sooner now. How else can I convert the int to a string?
  • Jon KeinathJon Keinath Posts: 146
    edited 2006-08-08 02:48
    There are a couple different ways -
    First the string buffer
    static StringBuffer buff = new StringBuffer(2); //2 = length of buffer
    .
    .
    .
    //then in the program
    buff.clear();
    buff.append(variable); //int (or other) variable
    buff.append("Some text");
    LCD.write(buff);
    
    


    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
  • bulkheadbulkhead Posts: 405
    edited 2006-08-08 05:46
    Forgot about StringBuffers, I just tried it out and it worked great! They make such a big difference in memory usage when compared with strings. THe parallax LCD class I was using already had a write(StringBuffer) method so it worked fine. Thanks.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-08 13:36
    If you need to format your output (for example: a value 000 to 999)
    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
Sign In or Register to comment.