Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pin numbers beside channel number for analogRead(). #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kroimon
Copy link

@kroimon kroimon commented Feb 2, 2012

I propose to change the values of the A0, A1, ... constants to be the pin number, not the ADC channel number.
This way you can do things like
digitalRead(A0);
where you would otherwise have to find out the pin number of the pin labeled "A0" on your board.

The patch should be completely backwards-compatible.

@mbolivar
Copy link
Contributor

mbolivar commented Feb 2, 2012

+1.

For what it's worth, LeafLabs did exactly this in libmaple years ago, and we're glad we did.

We keep "D0", "D1" around for historical purposes, and also because it's convenient to use them when you want to unambiguously refer to a pin, but pins are just numbered from 0 to n, and that's it.

This is really convenient considering that on a lot of MCUs, tons of pins are capable of analog input, and it makes layout way easier to allow gaps between analog pins.

@tochinet
Copy link

I believe Arduino did this also long time ago. In 0022, I do things like the following all the time.

#define TempPin A0
Temp=AnalogRead (TempPin);

On the other hand, I always used

#define Switch1 4
#Define BlueLed 13

And not "D4" or "D13"

In addition, on the MEGA there are pins way beyond 20 (to 54) and I checked that the analog pins 0-15 are defined as 54-69. That's the way it should be everywhere IMHO.

@tochinet
Copy link

One second thought though : how can this be "backwards compatible" at all ? I mean, how can this NOT break the previous uses of A0-A5 in Wiring if we change the values ? Does anyone have a trace of where is this used ?

In any case, it should be clearly flagged in the history file "revisions.txt"

@kroimon
Copy link
Author

kroimon commented Mar 30, 2012

Well... until now, the Ax constants could only be used correctly with the analogRead() function, so I don't think they are used elsewhere.
The old values of the constants still work as expected, so in case someone has the old constant values stored somewhere (e.g. in EEPROM) they still work.

This patch would make Wiring code a little more compatible with Arduino sketches which made this change long time ago. It also makes it a lot more intuitive to read digital values from the analog pins as you don't have to remember two different pin namings for one single pin.

kaklik pushed a commit to kaklik/Wiring that referenced this pull request Apr 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants