Skip to main content
Tweeted twitter.com/StackArduino/status/674992052279595008
added 4 characters in body
Source Link
Alex Shroyer
  • 437
  • 2
  • 5
  • 9

I want to do something along the lines of

for (int i = 0; i < 4; i++) {
  analogRead(i);
}

Which appears to work, but the following does not:

for (int i = 0; i < 4; i++) {
  pinMode(i, INPUT);
  pinMode(i + 4, OUTPUT); // should make Analog Pin (i + 4) into an output
  digitalWrite(i + 4, LOW);
  analogRead(i);
}

Instead, it appears to treat the pin addressed by digitalWrite(i + 4, LOW); as one of the digital pins.

Do I really have to explicitly specify A0, A1, A2, ... anytime I want to loop over the analog pins?

I want to do something along the lines of

for (int i = 0; i < 4; i++) {
  analogRead(i);
}

Which appears to work, but the following does not:

for (int i = 0; i < 4; i++) {
  pinMode(i, INPUT);
  pinMode(i + 4, OUTPUT); // should make Analog Pin (i) into an output
  digitalWrite(i + 4, LOW);
  analogRead(i);
}

Instead, it appears to treat the pin addressed by digitalWrite(i + 4, LOW); as one of the digital pins.

Do I really have to explicitly specify A0, A1, A2, ... anytime I want to loop over the analog pins?

I want to do something along the lines of

for (int i = 0; i < 4; i++) {
  analogRead(i);
}

Which appears to work, but the following does not:

for (int i = 0; i < 4; i++) {
  pinMode(i, INPUT);
  pinMode(i + 4, OUTPUT); // should make Analog Pin (i + 4) into an output
  digitalWrite(i + 4, LOW);
  analogRead(i);
}

Instead, it appears to treat the pin addressed by digitalWrite(i + 4, LOW); as one of the digital pins.

Do I really have to explicitly specify A0, A1, A2, ... anytime I want to loop over the analog pins?

Source Link
Alex Shroyer
  • 437
  • 2
  • 5
  • 9

How to loop over analog pins?

I want to do something along the lines of

for (int i = 0; i < 4; i++) {
  analogRead(i);
}

Which appears to work, but the following does not:

for (int i = 0; i < 4; i++) {
  pinMode(i, INPUT);
  pinMode(i + 4, OUTPUT); // should make Analog Pin (i) into an output
  digitalWrite(i + 4, LOW);
  analogRead(i);
}

Instead, it appears to treat the pin addressed by digitalWrite(i + 4, LOW); as one of the digital pins.

Do I really have to explicitly specify A0, A1, A2, ... anytime I want to loop over the analog pins?