I'm creating a UI using a 4x20 LCD screen. My code worked fine in small scale, but as I'm expanding the UI, I'm running into a glitch that I can't seem to figure out. I'm attempting to store the lines of text to display in an n x 4 array of strings, with each of the 4 columns holding the text that should be displayed on the respective line of the screen for a given menu number (ie: row number). Each string is 20 characters or less, since that is all that can be displayed on each line of the screen.
Then a for loop generates each screen update as needed later in the code:
for (int i = 0; i < 4; i++) {
lcd.setCursor(0,i);
lcd.print(menArray[menu][i]);
As I said, this worked fine when I had 10 or fewer screens. I am now to the point where I have upwords of 15 in the code. The lower screen numbers work fine, but then somewhere (it changes depending on exactly how many screens I include in the array when I compile) in the screen #9-11 range, one screen will begin to print fine, but then a bunch of random text and symbols will be displayed. If I try to go to a screen number higher than that one, it will be completely blank. If I hardcode a print command (for example: if (menu == 15) { lcd.print("test");}) it will show up on that screen fine, which is leading me to believe that it may be a problem with the array itself. Is there such a thing as an array getting too big as long as there is still enough program memory available for it or something similar?
Any advice or suggestions would be greatly appreciated.
60 String objects take up a lot of SRAM. If you are using a UNO, you are most likely running out.
Three questions. Why wrap a small string in a large String object? Can you simplify your program to make use of fewer menus? Can you store the menus in PROGMEM?
Is there anyway to check how much SRAM is being used by my code? I was unaware of the difference between SRAM and flash(progmem) memory until now. When I compiled the sketch, the status window at the bottom of the environment said I was only using up ~9k of the 32k available, but I now realize that is referring to the flash memory, not the SRAM.
Fewer menus is most likely out of the question, as I still need to add some more once I figure this out.
My thought for using the large array was so that I could easily see/modify all the text that would be displayed on each menu in one place and to also make the function that contains the for loop for writing to the screen simple and versatile.
Unless you have any other suggestions, I will probably look at modifying my code so that it will go through a bunch of if loops to determine what menu is active and then call the writing function while passing in just the text that's needed for that screen.
Unless you have any other suggestions, I will probably look at modifying my code so that it will go through a bunch of if loops to determine what menu is active and then call the writing function while passing in just the text that's needed for that screen.
It does not matter where in the code you declare a string. It ends up in SRAM for the life of the application. Define a global string, it goes in SRAM. Define a string inside an if block, it goes in SRAM.
Notice that there is a big difference in the amount of SRAM and the amount of Flash between a string and a String. They are not the same things.
There is a FreeMemory function you can search for. There are macros and functions for getting data out of SRAM and into Flash memory (PROGMEM).