Pages: [1]   Go Down
Author Topic: String variable makes Arduino hang (???....)  (Read 1197 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I am new in this forum, this my first posting and I am not native English. Please do let me know if something in my posting is against the communciation rules in this forum.  smiley-eek-blue

I am stuck in a problem with a larger project. Its about a Midi Controller and so far everything I have programmed works very well. Now I simply needed to add another String variable array (which I use as containers for LCD content and that works perfect so far) but when I only add that variable declaration my Mega 2560 will stop execution. Compilation is ok and Arduino V0022 reports 'Done Uploading' but then it will hang.
Here is the code section with the variable declaration that includes the one variable that makes the difference. Its only declarations:
 
.....
//String Arrays for the display of certain variables:
String VarLCDString; //a string variable that contains the string to be displayed on the LCD for a certain value
String SGrpRepMode[4] = {"Nothing","OFF    ","Retrig.","Last   "};
String SBtnAction[5] = {"Auto  ","Latch ","Moment","",""};
String SBtnGrp[9] = {"---        ","1","2","3","4","5","6","7","8"};
String SCmdType[9] = {"--- none ---","Prog. Change","Contr.Change","Note ON/OFF ","Song Select ","Song Control ","Btn. Trigger","Ped. Trigger","Set PedInput"};
String SYesNo[2] = {"No ","Yes"};
String SBtnTrigger[131]; //0-127 is for values, 128-130 are filled in Setup
String SSongControl[3] = {"Stop ","Start","Cont "};
String SActInput[5] = {"off","1","2","3","4"};
String SSetPedInput[6] = {"off","1","2","3","4","   "};
String SParVal[130]; //For Midi commands in which 0-127 is the Midi value and 128 is a String displaying 'empty/nothing'.
.......

The last line is the one. Everything with an array size > 20 will make it hang.

But making the array smaller like
String SParVal[10]; //For Midi commands in which 0-127 is the Midi value and 128 is a String displaying 'empty/nothing'.
does work well.

Sketch Size is reported to be 24556 bytes of  a 258048 maximum (<10%).

Questions:
Is there a limitation in memory or maximum number of String Variables or whatsoever I could be exceeding?
If so: Where and how could I find out?
Any other ideas?

Thanks on beforehand!!
MidiMan
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4094
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad you asked this, I had a similar problem, not hangs but all kinds of weird and unexplained behavior.  I was going to post an inquiry but never did.  Instead, I got rid of the string stuff and used functions like strcpy, strcat, itoa, etc., and all is well now.  Maybe not quite as elegant but really not a whole lot more work or anything.

So not an answer for you, but you may not be alone and I'll be interested in other replies on this topic!
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sketch size is the amount of Flash memory your program uses, your strings and other variables are stored in your Arduinos (very limited) RAM memory.
The size of the RAM depends on which Arduino board you use. To rubb salt in the wound the RAM is alo used by some internal memory structures that Arduino uses when running your sketch. You use the RAM from one end, Arduino uses it from the other, if you use so much that you eat into the RAm Arduino uses for internal purposes unpredictable things will happen, usually in the shape of a crash / unresponsive system.

Try to allocate smaller arrays / strings and see if it works. If your program does not use all the Flash memory you can store some of your strings there. Check  PROGMEM in the reference. Just remember only string constants can be stored that way, nothing that changes can be put there.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8620
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've not used the String class but it's likely you're running out of RAM, all those strings are copied into RAM when the program starts.

Search for PROGMEM and start putting some of the strings into that.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

many thanks for fast and informative response, thats great !! Is there any information available about how much RAM is actually being used by String variables? I mean, I have defined quite some String arrays in my code but I have no idea how much memory that will allocate.

Counting the total number of characters in these Strings it isn't much at all considering that other people use the Arduino String Class for longer texts such as EMails. That is what is strange to me.

When I for example define:

String SBtnTrigger[131]; //0-127 is for values, 128-130 are filled in Setup

and later I fill only 3 of these Strings with content:

  SBtnTrigger[128]= ("On ");
  SBtnTrigger[129]= ("Off");
  SBtnTrigger[130]= ("---");

Ho much memory does that consume?

MidiMan

Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're not really giving enough of the picture to give a proper answer - there could be more strings in prints that we can't see, but that the compiler can.
Logged

Per Arduino ad Astra

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,
I found the MemoryFree library and by using this function I could answer my own question:

By adding String variable arrays in different size and checking the free memory at some places in the code I found out, that a String variable per default (=empty) uses 10 bytes of memory. Thus, an array like this one will occupy 1000 bytes of memory:

String MyString[100];

I could trace the error in my program and it became clear, that adding the String Array in a size of  > [10] simply killed my remaining memory.

Thanks to everybody who helped finding out !!   smiley-cool
MidiMan
Logged

Pages: [1]   Go Up
Jump to: