... but I don't want to write them out every time I want to call them, as it will make a huge code that any changes I will have to make 15 times for every instance. What I ideally want is something like this:
for (byte i =0; i<15; i++){
CurrentConstant = (i + "q"); // makes CurrentCostant into 0q, 1q, etc.
Serial.println(CurrentConstant);
Delay(250);
}
I know that will not work, but hoping it gets across what I am trying to do. What bizarre combo of Char and String commands do I need to make this work? Or is this something that just can't be done?
I figure I could make this into a 2D array, but wanted to know if this is still possible.
magic_smoke:
but able to call the variable by building its name in a loop.
You cannot create names for variables or constants when an Arduino program is running. Names only exist in the code you write. The compiler replaces them with memory addresses.
You can create an array of values and select the appropriate one in a running program. In an array all the values must be of the same type and each element is identified by its index position.
Another option is to create a struct which allows for variables of different types and allows for the elements to have names. You can also make an array of structs
magic_smoke:
Well, that stinks! Seems like it would be a real useful thing to be able to do.
No, it's a smart design decision. Allowing it would be a disaster. Not only would it be extremely cumbersome to implement, it would open the door to the creation of some of the worst code ever to see the light of a monitor - well known for a long time, and called "self modifying code". Due to the implementation problem, it is only possible in interpretive languages, but it is frowned on for its properties of obfuscation and difficulty in verification of program correctness.
magic_smoke:
Well, that stinks! Seems like it would be a real useful thing to be able to do.
You can do it with interpreted languages like Python and Ruby but interpreted languages are completely unsuitable for a small slow microprocessor such as in in an Uno or Mega
Give a real world example of why you need to do this (on a small microprocessor platform) that can’t be achieved but less devious means (e.g. a simple array).