Hi i would like to know if this is possible to change variable name with a for loop , and if not have an explanation.
If it is possible could you please learn me the way.
See the examples above for what i want to know:
THIS CODE
byte i;
byte Example1[5] = {1, 2, 3, 4, 5} ;
byte Example2[5] = {11, 12, 13, 14, 15} ;
byte Example3[5] = {31, 32, 33, 34, 35} ;
void setup() {
Serial.begin(115200);
Serial.println("HELLO");
Serial.println("");
}
void loop() {
for (i = 0; i < 5; i++) {
Serial.println(Example1[i]);
}
for (i = 0; i < 5; i++) {
Serial.println(Example2[i]);
}
for (i = 0; i < 5; i++) {
Serial.println(Example3[i]);
}
Serial.println("END");
Serial.println("");
delay(5000);
}
TO THIS CODE
byte i;
byte n;
byte Example1[5] = {1, 2, 3, 4, 5} ;
byte Example2[5] = {11, 12, 13, 14, 15} ;
byte Example3[5] = {31, 32, 33, 34, 35} ;
void setup() {
Serial.begin(115200);
Serial.println("HELLO");
Serial.println("");
}
void loop() {
for (n = 1; n < 4; n++) {
for (i = 0; i < 5; i++) {
Serial.println(Example(n)[i]); // dont work
//Serial.println((Example)(n)[i]); // dont work either
//Serial.println(Example(n)[i]); // dont work either
//Serial.println(Example+n[i]); // dont work either
// and i tried many other ways
}
}
Serial.println("END");
Serial.println("");
delay(5000);
}
Obviously this dont work , but what is the correct way ?
No, it doesn't work that way. Variable names aren't strings. When the code is compiled, the variable names all go away and are replaced by simple memory addresses. So the code when it is running has absolutely no idea what you named your variables.
You obviously understood in the first example you posted that the answer was to put things into an array. The answer to the second example is the same. You now need an array of arrays, a 2 dimensional array.