Hi Everyone,
I tried to change value of a global array but it did not work, can you please advice me on what is the problem with this? I marked “//PROBLEM::” where the code part has problem.
The FieldCaption alway stays with “Level” after the first loop while the previous array item value has been changed successfully to value other than just “Level”!
Please share your help on this. I am a newbie in C++ and Arduino programming language. So hard to deal with char and string!
Thank you in advance!
typedef struct FormData {
char* FieldCaption;
unsigned int Type; // Data type 1,DD;2,DDD;3,HH;4,MIN;5,MM;6,NNN;7,YYYY;
unsigned int Address; // EEPROM address
};
FormData SubMenu[10];
int SubMenuSize=0;
const int MAX_SUB_MENU=10;
void setup(){
Serial.begin(9600);
}
void loop(){
String txtMenu = "Week day,2,7;On hour,3,8;On minute,4,9;Off hour,3,10;Off minute,4,11;Level,6,12";
LoadSubMenu(txtMenu);
}
void LoadSubMenu(String SubMenuText){
// This procedure will break the SubMenu array for later use
// given: String command
int numArgs = 0;
int beginIdx = 0;
String arg, fldCap;
char charBuffer[20];
boolean found = true;
int idx = 0;
int sub_idx; // for taking out of the text part
while (found)
{
idx = SubMenuText.indexOf(";", beginIdx);
if (idx == -1) {
found = false;
idx = SubMenuText.length();
}
arg = SubMenuText.substring(beginIdx, idx);
if (arg == "") break; // escape at nothing left
sub_idx = arg.indexOf(",");
//Field Caption
fldCap = arg.substring(0,sub_idx);
fldCap.toCharArray(charBuffer, 20);
SubMenu[numArgs].FieldCaption = charBuffer;
//PROBLEM::if check here the array item is properly filled with new value of FieldCaption
Serial.println(numArgs);
Serial.println(SubMenu[numArgs].FieldCaption);
// Field Type
arg = arg.substring(sub_idx + 1);
sub_idx = arg.indexOf(",");
fldCap = arg.substring(0,sub_idx);
SubMenu[numArgs].Type = fldCap.toInt();
//EEPROM Address
fldCap = arg.substring(sub_idx + 1);
SubMenu[numArgs].Address = fldCap.toInt();
beginIdx = idx + 1;
numArgs++;
}
// Remember max submenu item
SubMenuSize = numArgs;
for (int i=0; i<SubMenuSize; i++){
//PROLEM:: but here the FieldCaption stay the same with value of: "Level", other changes as expected
Serial.println(SubMenu[i].FieldCaption);
Serial.println(SubMenu[i].Type);
Serial.println(SubMenu[i].Address);
delay(1000);
}
// cleaning the remaining array items
for (beginIdx = numArgs; beginIdx < MAX_SUB_MENU; beginIdx++){
SubMenu[beginIdx].FieldCaption="";
}
}
If I change the struct a bit to the following:
typedef struct FormData {
String FieldCaption;
unsigned int Type; // Data type 1,DD;2,DDD;3,HH;4,MIN;5,MM;6,NNN;7,YYYY;
unsigned int Address; // EEPROM address
};
The later serial printing out is properly done as expected. However, the weird thing I spotted out here is:
Why the array element was correctly assigned previously, and just in one procedure flow, nothing change at all, its value then changed to just the last element value of “Level” in the later serial println.
I doubt the problem came up with struct but I have very limited knowledge on this and can not explain myself.
Please help to bright my mind with thanks.