I am wanting to create a temporary list of input switches that are On in an array named onList[12], sourced by swInput[] array with 10-12 items. When the switch is turned off, the onList[] will be updated to remove the On item.
But, when I have a selected array value like swInput[i] (i has a 'i' value from an I/O board read loop, say 3 for example), using the following code, nothing stores in the onList[] array. Serial.print of the swInput[i] value is fine.
Serial.print(swInput[i]); // prints correct value, i=3: "Sw 13"
onList[i] = swInput[i]; // fails to store to onList[] using active 'i' value
Serial.print(" is ON, "); // prints correctly
Serial.println(onList[i]); // prints odd special character
I have searched for a method to do this exact thing but finding nothing helpful.
any advice appreciated.
This is an array of pointers to characters. What the compiler will do is store all those strings in memory somewhere. The contents of swinput[] are not the strings, but rather the addresses of where each string can be found in memory.
char onList[13];
This is an array of characters. It can hold up to 12 characters plus the required trailing null character. It can effectively hold one, and only one, character string. If you try to put a pointer to a string into that array, the compiler may allow it, but the result is almost certain to be unprintable.
Do you really need to store the string for each switch, or just the status of the switches? You could have an array of integers, one for each switch. Set the value for each element to zero when the switch is off, and something other than zero when the switch is on.
Doing it this way, you could then do something like this:
for (int i=0;i<12;i++){
if (swstatus[i] != 0) {
Serial.print(swInput[i]);
Serial.println(" is ON")
}
}