Array of strings locks up program

I have a sketch to measure frequency and I want to display the name of the note. As my input comes from a celtic harp I have approx 40 strings (although digital strings that represent names of real life strings is a bit confusing :wink: ). I decided to give them frequencies and names with two arrays.

Everything locks up! :o I painstakingly commented all lines and found that the instruction that gives problems is the definition "String stringName etc." - I mean - when I activate it the display does not show the initializing screens and no further results come out. As soon as I comment it returns to normal. No problem with the int array. No messages at compile time though it looks like a memory problem - writing over some reserved area or such.

Any hints on how to go ahead?
Tks
F

…

//variables for tuning *** Mib Eb scale
//frequenza nota "esatta" moltiplicata per dieci
int stringFreq[40] = {1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1};

//nome della nota
String stringNAME[40] = {"A# LA#5","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd",
"abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd",
"abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd",
"abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd","abcd"};

void setup(){

Serial.begin(9600);
Serial.println("Inizialize...");

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Impostare l'indirizzo i2c annotato in precedenza
display.clearDisplay(); //Pulisce il buffer da inviare al display
display.setTextSize(2); //Imposta la grandezza del testo
display.setTextColor(WHITE); //Imposta il colore del testo (Solo bianco)
display.setCursor(0,0); //Imposta la posizione del cursore (Larghezza,Altezza)
display.println("aaaaa"); //Stringa da visualizzare
display.display(); //Invia il buffer da visualizzare al display
delay(1000);

...

The general advice here is not to use Strings with the limited amount of memory available on most Arduinos but to use zero terminated arrays of chars (aka C strings) instead

Please post a complete program that illustrates the problem and please use code tags when you do

With the alternative char definition it works.

I suppose that zero terminated means that the string lenght must be one less than the definition and can be implicit or do I have to add it at the end of each string?

My strings are all long 9 chars or less. "-2 Eb MIb"

char stringNAME[40][10] = {"-2 Eb MIb","abcd","abcd ...

Thanks
Flavio

You could simplify things by using an array of pointers to the strings
An example :

char * names[] = {"-2 Eb MIb", "abcd", "efgh"};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int name = 0; name < sizeof(names) / 2; name++)
  {
    Serial.println(names[name]);
  }
}

void loop()
{
}

I tried, the line:

char * stringNAME[] = {"-2 Eb MIb","-2 F  FA","-2 G  SOL","-2 Ab LAb","-2 Bb SIb","-2 C  DO","-2 D  RE","-1 Eb MIb","-1 F  FA","-1 G  SOL",
                           "-1 Ab LAb","-1 Bb SIb","-1 C  DO","-1 D  RE","** Eb MIb","** F  FA","** G  SOL","** Ab LAb","** Bb SIb","** C  DO",
                           "** D  RE","+1 Eb MIb","+1 F  FA","+1 G  SOL","+1 Ab LAb","+1 Bb SIb","+1 C  DO","+1 D  RE","+2 Eb MIb","+2 F  FA",
                           "+2 G  SOL","+2 Ab LAb","+2 Bb SIb","+2 C  DO","+2 D  RE","+3 Eb MIb","+3 F  FA","+3 G  SOL","+3 Ab LAb","+3 Bb SIb"};

It gives a warning error at compile time as follows:
Tuner_v1_conOLED.ino:141:144: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
"+2 G SOL","+2 Ab LAb","+2 Bb SIb","+2 C DO","+2 D RE","+3 Eb MIb","+3 F FA","+3 G SOL","+3 Ab LAb","+3 Bb SIb"};
^

I can revert to "char stringNAME[40][10]" but am still not sure how to null terminate each individual string.
Flavio

am still not sure how to null terminate each individual string

When you use double quotes to delimit the string, the zero is automatically added at the end.

flavios:
I tried, the line:

char * stringNAME[] = {"-2 Eb MIb","-2 F  FA","-2 G  SOL","-2 Ab LAb","-2 Bb SIb","-2 C  DO","-2 D  RE","-1 Eb MIb","-1 F  FA","-1 G  SOL",

"-1 Ab LAb","-1 Bb SIb","-1 C  DO","-1 D  RE","** Eb MIb","** F  FA","** G  SOL","** Ab LAb","** Bb SIb","** C  DO",
                          "** D  RE","+1 Eb MIb","+1 F  FA","+1 G  SOL","+1 Ab LAb","+1 Bb SIb","+1 C  DO","+1 D  RE","+2 Eb MIb","+2 F  FA",
                          "+2 G  SOL","+2 Ab LAb","+2 Bb SIb","+2 C  DO","+2 D  RE","+3 Eb MIb","+3 F  FA","+3 G  SOL","+3 Ab LAb","+3 Bb SIb"};




It gives a warning error at compile time as follows:
Tuner_v1_conOLED.ino:141:144: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
"+2 G SOL","+2 Ab LAb","+2 Bb SIb","+2 C DO","+2 D RE","+3 Eb MIb","+3 F FA","+3 G SOL","+3 Ab LAb","+3 Bb SIb"};

Use:

const char * const stringNAME[] = {"-2 Eb MIb", "-2 F  FA", "-2 G  SOL", "-2 Ab LAb", "-2 Bb SIb", "-2 C  DO", "-2 D  RE", "-1 Eb MIb", "-1 F  FA", "-1 G  SOL",
                       "-1 Ab LAb", "-1 Bb SIb", "-1 C  DO", "-1 D  RE", "** Eb MIb", "** F  FA", "** G  SOL", "** Ab LAb", "** Bb SIb", "** C  DO",
                       "** D  RE", "+1 Eb MIb", "+1 F  FA", "+1 G  SOL", "+1 Ab LAb", "+1 Bb SIb", "+1 C  DO", "+1 D  RE", "+2 Eb MIb", "+2 F  FA",
                       "+2 G  SOL", "+2 Ab LAb", "+2 Bb SIb", "+2 C  DO", "+2 D  RE", "+3 Eb MIb", "+3 F  FA", "+3 G  SOL", "+3 Ab LAb", "+3 Bb SIb"
                      };

It gives a warning error at compile time

What is "it" ?
Please post your complete program

gfvalvo:
Use:

const char * const stringNAME[] = {"-2 Eb MIb",...

It works perfectly - no errors at compile time. Now that you have stated it, yes they are constants and not variables.

To extract the full description I use the following lines after making all description exactly long 9. I tried with '<sizeof((stringNAME*[aa])' as it was more "elegant" but it does not work.*
* *     stringN = "";      for (int aa = 0; aa < 10; aa++){        stringN += (stringNAME[i][aa]);      }* *
I did not enclose earlier the complete sketch as it exceed the total lenght allowed. I have now a much shorter sample but the question is satisfied and the issue closed.
Many thanks.
Flavio

        stringN += (stringNAME[i][aa]);

I see that you have gone back to the use of Strings for this. Why not use C strings throughout ?

I needed to store the description in a separate string to get it out of the loop and display it.

I should confess my ignorance of C and just say that this was the first way it worked… I would know no better. :wink: I am learning "on the job". And some critical moments were overcome thanks to this forum.
Flavio

UKHeliBob:

        stringN += (stringNAME[i][aa]);

I see that you have gone back to the use of Strings for this. Why not use C strings throughout ?

I needed to store the description in a separate string to get it out of the loop and display it.

I have no idea what that means, but why do you need to copy a string from the array to another one and why do it character by character anyway ? Note that I am referring to strings not Strings