Pages: [1]   Go Down
Author Topic: analog pin name in variable  (Read 607 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 444
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Say just for example I wanted to read all of the analog pins as digital with a loop. For example:

Code:
int buttonPin[6] = {
  A0,A1,A2,A3,A4,A5};
void setup(){
Serial.begin(9600);
}

void loop(){
  for (int i=0;i<6;i++){
    Serial.println(digitalRead(buttonPin[i]));
  }
  delay(1000);
}

I went ahead and just tried this and it worked... Is this the proper way to do this?

Thanks.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The A0, A1, etc. "names" are just #define's into existence. The value depends on the board. For a 328 based board, A0 and 14 are equivalent. So, what you did was perfectly acceptable.
Logged

California
Offline Offline
Sr. Member
****
Karma: 3
Posts: 444
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ohhhhh I was really surprised that it worked. That makes perfect sense. Thanks.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may want a second loop in setup to make sure they're inputs ( they are, by default, but if you wanted outputs, you could lose some hair chasing that down)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: