Hi there!
I'm trying to use for a very big project the analog input to be output using a loop for definition. For native digital pins it works great because there only a number,and for analog input there is the need to add A before the number. I tried this method:
#define ABC A
void setup()
{
for (int i=0;i<9;i++) pinMode(ABCi,OUTPUT);
for (int i=0;i<9;i++) pinMode(ABC(i),OUTPUT);
for (int i=0;i<9;i++) pinMode(ABC[i],OUTPUT);
for (int i=0;i<9;i++) pinMode(ABC{i},OUTPUT);
}
void loop()
{
}
But it didn't worked.....is there another way to do this?
The reason that i'm asking is because i'll to do this alot (playing with the analog pins definition) and one line of code using For will be great,even then a dedicated function
You can iterate through the analog pins like this:
for (int i=A0; i<=A5; i++)
{
pinMode(i, OUTPUT);
}
To be more general, it would be better to store your pin numbers in an array and iterate through the elements in the array, rather than iterating through the pin numbers directly.
I didn't want to create array- ss you can see my tru to use define is that the compiler will substitue the ABC to
A then it will look like this: A0,A1....
yeah,ok,got it-the compiler doesn't work the way i am,i must have an array because the universe commanded me to....
You don;t have to be so harsh. the fact that i wrote the i want to do something in a specific way doesn't mean it possible-it just says that's the way i want to. you can just write "you can't because XYZ" in more of a nice way...
btw PeterH your way works-thanks
pretty weird because your defining int for the For loop that's isn't an integer because of the A...but works nonetheless
kimkash:
You don;t have to be so harsh. the fact that i wrote the i want to do something in a specific way doesn't mean it possible-it just says that's the way i want to.
We weren't being harsh. We just said what would and wouldn't work.
What you're missing is that the values A0, A1 etc are integer constants. You can use them anywhere you could use a literal number - in the bounds of a FOR loop, or defining values in an array of pin numbers, for example.
I believe better way would be to use int array since A0-A8 are not necessarily continuous. While looping from A0 to A8 probably works in this case, it's just good practice to learn to avoid such assumptions to avoid nasty surprises, IMHO.
What you were trying to do is a C preprocessor string concatenation, but in that case both of the values need to be preprocessor literals, while your other argument is run-time variable like other people have pointed out.
kimkash:
yeah,ok,got it-the compiler doesn't work the way i am,i must have an array because the universe commanded me to....
You don;t have to be so harsh. the fact that i wrote the i want to do something in a specific way doesn't mean it possible-it just says that's the way i want to. you can just write "you can't because XYZ" in more of a nice way...
If what was said has offended you that much, I shudder to think of what would happen if PaulS made an appearance. In other words, grow thicker skin.