Pin number means digital or analog?

For a function argument that is a pin, and I provide a value of 3 (for example), how does the code know if that is the analog pin 3 or the digital pin 3?
Thanks.

3 is for digitalpin

you can use A3 for analog (A3 is a #define in Arduino library)

For a function argument that is a pin, and I provide a value of 3 (for example), how does the code know if that is the analog pin 3 or the digital pin 3?

The function knows. analogRead() doesn't read from digital pins. digitalWrite() doesn't write to analog pins.

digitalWrite() doesn't write to analog pins.

Sure ?

digitalWrite(A0, HIGH); and digitalWrite(14, HIGH); // should both work ( and have the same effect on an UNO )

Magic is rather that both analogRead(0); andanalogRead(A0); do work.
That's for convenience, I guess: analogRead(14); will work on an UNO the same but can't be explained why :wink:

digitalWrite(A0, HIGH); and digitalWrite(14, HIGH); // should both work ( and have the same effect on an UNO )

That a digital pin and an analog pin share the same location, but vastly different functionality, seems to have escaped you.

Magic is rather that both analogRead(0); and analogRead(A0); do work.
That's for convenience, I guess: analogRead(14); will work on an UNO the same but can't be explained why

Unless you look at the analogRead() function, and see how it maps a pin number in the Arduino sense to an actual pin on the AtMega328 chip. That function maps 0 and 14 to the same physical location on the AtMega328 chip.

PaulS:
I want to follow up on the way I read your post. I'm still tyring to figure out how it all works.

Unless you look at the analogRead() function, and see how it maps a pin number in the Arduino sense to an actual pin on the AtMega328 chip. That function maps 0 and 14 to the same physical location on the AtMega328 chip.

I pictured that from the bootload Arduino pins would be mapped to Atmel pins. But above sounds like a command does a connection at runtime. Maybe I'm reading your post wrong.

Have a look at the appropriate board for you in this thread.
You can see how pins and functions correlate.
It is the IDE (the compiler) that looks for numbers and assigns the pin of the processor (and so the board) to those.

johnkauffman:
For a function argument that is a pin, and I provide a value of 3 (for example), how does the code know if that is the analog pin 3 or the digital pin 3?

For pinMode(), digitalRead(), digitalWrite() and analogWrite() it means digital pin 3.
For analogRead() it means analog input pin 3.

If you use the name A3 instead of 3:
For pinMode(), digitalRead(), digitalWrite() and analogRead() it means analog input pin 3.
For analogWrite() it means analog input pin 3 but since that is not a PWM pin (on the UNO, at least) you will only get LOW and HIGH instead of PWM.

It is good practice, when talking about an analog input pins, to use the names A0, A1... To do otherwise can cause your code to behave differently on other models, particularly switching from UNO to Leonardo which maps the names to numbers differently.

Thanks, MAS3. That diagram will be several hours of interesting study. I'm seeing how the Ax pins connect through.

Thanks, John. There is never a penalty or problem for using the "Ax" designation instead of x or 13+x? Thanks.

johnkauffman:
There is never a penalty or problem for using the "Ax" designation instead of x or 13+x?

You mean "14+x" but no, there is no penalty and several benefits:

With pinMode(), digitalRead(), digitalWrite() and analogWrite() you can't use 3 if you mean A3. Only analogRead() will interpret 3 as A3.

With the Leonardo you can't use 17 (14+3) to mean A3. The analog input pins start at 18 on the Leonardo. (See: /Applications/Arduino 1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/leonardo/pins_arduino.h)

I pictured that from the bootload Arduino pins would be mapped to Atmel pins.

No. The bootloader is concerned about getting a HEX file into program memory. It does nothing to what is in the HEX file. The HEX file itself must, therefore, be responsible for mapping the Arduino pin numbers to ATMEL pin numbers.