Error using loop to define analog inputs has outputs

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

Thanks for the help!

Forgot the say-none of the 4 examples in the code above worked.....

for (int i=0;i<9;i++) pinMode(ABC[i],OUTPUT);

this is the correct version, but it indexes into an array and you haven't got one. Remove your #define ABC and add

int ABC[] = {0,1,2,3,4,5,6,7,8};

at the top of the program (use pin numbers that make sense to your application).

Oh, and chose a better name than ABC :slight_smile:


Rob

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....

I didn't want to create array

That's nice but the compiler doesn't care about what you want.

A then it will look like this: A0,A1....

Maybe to you, but not to the compiler.

Either use an array or use 9 pinMode() calls in a row.


Rob

The compiler cannot do name substitution at run-time, as you appear to want. The replies above indicate the way to go.

this is the correct version, but it indexes into an array and you haven't got one. Remove your #define ABC and add

Code:
int ABC[] = {0,1,2,3,4,5,6,7,8};

That's not going to work for the stated intent of setting the analogue pins to be digital outputs.

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 :slight_smile:
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.

int pins[]={A0, A1, A2, A3, A4, A5, A6, A7, A8};
for(unsigned i=0; i<9; ++i)
  pinMode(pins[i], OUTPUT);

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.

int pins[]={A0, A1, A2, A3, A4, A5, A6, A7, A8};

"const byte" would be better than "int"

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.