How to loop over analog pins?
Yes, the analog pins must be addressed using A0, A1,... when using them for digital I/O.
Depending on the board you are using A0,A1,etc. are mapped to different values (for instance it looks like A0 is 18 on some boards but 14 on others.
One solution for looping over the analog pins would be this:
static const uint8_t analog_pins[] = {A0,A1,A2,A3,A4};
// Setup pins for input
for (int i = 0; i < 5; i++) { //or i <= 4
digitalRead(analog_pins[i]);
}
If you are using the analog pins only with the analogRead()
call you can use 0,1,... instead of A0,A1,...
At least an Uno/Megas/leonardos, all the values mapped to analog pin numbers are consecutive, so
for (int i = A0; i < A4; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
will set A0, A1, A2, and A3 to OUTPUT, and then LOW.