I've created a menu for a file chooser around an Arduino Mega 2560. The Problem is, that when I move through the menu, the RAM of the arduino Fills with data, and after half of the files, the arduino restarts, because the ram was full. Is there a way to clean the RAM of the arduino from the last printed lines from the LCD Panel? (I want not to clean the Array with the Menu lines, only the cache of the Data sent to the LCD, if there is one.)

These are parts of my code (the full code is to much)

//converts a string to a char array, for the lcd.print function
char* ConvertStringToCharArray(String string) {
    char* c= new char[string.length() + 1];
    strcpy(c, string.c_str());
    return c;
}

//truncates a string, if its longer than the maxLength
String TruncateString(String value, int maxLength)
{
    if (value.length() <= maxLength) {
        return value;
    }
    else {
        return value.substring(0, maxLength);
    }
}

/*I've written a function for Printing out Parts of an array with the menu lines (Arguments: l=Object for the LCD; menupos= position in the array, to get the lines; menuname=Header of the menu;menustring=string Array with all menu lines; menustring_length=length of the menustring array):*/
void SetMenuPos(LiquidCrystal l, int menupos, String menuname, String* menustring, int menustring_size) {
    l.clear();//clears the lcd
    if (menupos == menustring_size - 1) {/*if the last position of the menu is checked, i want to display '>' in the last line*/
        l.home();
        l.print(ConvertStringToCharArray(TruncateString(menuname, 16))); //convert a truncatet string of the menu line to char array and print it to the lcd, i've truncate the string, else it would be to long. I'm repeating this process until everything is on the LCD
        l.setCursor(1, 1);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos - 2], 15)));
        l.setCursor(0, 3);
        l.print(F(">"));
        l.setCursor(1, 2);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos - 1], 15)));
        l.setCursor(1, 3);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos], 15)));

    }
    else if (menupos == 0) {/*if the first position of the menu is checked, i want to display '>' in the first line*/
        l.home();
        l.print(ConvertStringToCharArray(TruncateString(menuname, 16)));
        l.setCursor(1, 1);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos], 15)));
        l.setCursor(0, 1);
        l.print(F(">"));
        l.setCursor(1, 2);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos + 1], 15)));
        l.setCursor(1, 3);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos + 2], 15)));

    }
    else {/*if any other position of the menu is checked, i want to display '>' in the middle line*/
        l.home();
        l.print(ConvertStringToCharArray(TruncateString(menuname, 16)));
        l.setCursor(1, 1);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos - 1], 15)));
        l.setCursor(0, 2);
        l.print(F(">"));
        l.setCursor(1, 2);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos], 15)));
        l.setCursor(1, 3);
        l.print(ConvertStringToCharArray(TruncateString(menustring[menupos + 1], 15)));

    }
}

I hope, this is enough information

0 Answers