I want to store a lot of strings for displaying it on an LCD, so first I do this:
prog_char string_0[] PROGMEM = "1-Tempo";
prog_char string_1[] PROGMEM = "2-Lider gordo";
prog_char string_2[] PROGMEM = "3-Envolvente";
prog_char string_3[] PROGMEM = "3b-Forma de env";
... etc etc
prog_char string_34[] PROGMEM = "C#";
prog_char string_35[] PROGMEM = "C";
prog_char string_36[] PROGMEM = "B";
prog_char string_37[] PROGMEM = "A#";
prog_char string_38[] PROGMEM = "A";
PROGMEM const char *menuOption[] = {string_0,string_1,string_2,string_3,... etc etc ,string_38};
char buffer[16];
then when I want to display one of the strings on the LCD:
strcpy_P(buffer, (char*)pgm_read_word(&(menuOption[21])));
lcd.print(buffer);
and that seems to work fine.
but when I want to return one of those strings back from a function I get a strange behavior
String getNoteName(int pitch){
int oct = ((pitch-33)/12)+1; //33=A1
int noteNumb = pitch-((12*(oct+2))-3);
noteNumb=noteNumb+38; //Index 38 is A, 37 is A#, 36 is B and so on...
String noteName;
strcpy_P(buffer, (char*)pgm_read_word(&(menuOption[noteNumb]))); // <--- THIS LINE IS TROUBLE!
Serial.println(buffer);
noteName = String(buffer);
noteName.concat(oct);
return noteName;
What that function does is you give it the note number and it returns the note name, for example "A4". The problem is that the line I point there is, somehow, changing the value for menuIndex when I do this:
strcpy_P(buffer, (char*)pgm_read_word(&(menuOption[menuIndex])));
Serial.println(buffer);
lcd.print(buffer);
also when I print buffer to Serial it prints this:
? ?? ?? ?? ?? ?? ?X ? ?ü ?C ?? ?? ?? ?? ?? ??acB6·?ا9hV®º«U?<·ÌWc½míýu>ör1?
Any idea???? Very weird, I am not writing on the memory, always reading... or that is what I intend...
Could it be that I am getting low on memory? my sketch is 19.300 bytes according to the bottom of the compiler
Thanks!