In my code I have 3 different procedures. In the loop I want to determine the index of "+" in each procedure and print this index. So in the end I want to read in the Serial port:
0
1
2
But I want this all to happen with only 1 indexOf. So the String Name before "indexOf" should increment itself. How can I make that happen? I tried it with a int which starts at 1 and increments every time a print happened. But i can't add a string and a int before the indexOf.
I think I need to change the name of the Variable before the indexOf during runtime. How could I do this?
Or do you have any other idea on how to realise this? I can't make an indexOf for every procedure, because it can be many more procedures than just 3.
I think I need to change the name of the Variable before the indexOf during runtime. How could I do this?
Variables do not have names at run time. So, this is NOT what you want to do.
You almost certainly want an array of Strings, not three separate Strings. Then, you can return the index of the array that contains the String of interest.
char procedure[4][3] = {"+11", "1+1", "11+"};
byte procedureIndex;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(procedure[procedureIndex]);
if (procedureIndex >= 2) procedureIndex = 0;
else procedureIndex++;
}
Like Paul says, there are no variable names in the compiled machine code. Variable names only exist in the human-readable C++ code.
Depending on what you're trying to do, maybe[u]Switch/Case[/u] will work for you.
Switch/Case does work from numbers. You can [u]enumerate[/u] which is simply a way of making a constant (a "word") equal to a number, and then you can use that "word" for switch/case. But, that could make your code harder to understand.
...Here's an example of how I used switch/case -
I made some sound activated lighting effects. There are 7 different effects, and I wrote a [u]function[/u] for each effect.
I generate a random number between zero and 6, and that determines which function is called from the switch/case structure. (Each effect runs for about one minute, then back to switch/case to choose another random effect.)