But now i get an AnalogPin, so it Returns me 10xx.
How do i Change 10 to A?
AFAIK the Compiler turns all Ax to specific INT Values (eg A0 <-> 14 on Arduino Uno).
But the Values are Different for each Arduino Version, so i cant do
if (Port = 1000) Port = 14;
and i would have to do it for 16 Analog out on a Arduino MEGA → wasted Runtime.
Ah, right, digital read on analog pins. I see where you're going now.
Yes, they will be different on the different boards.
But, you'll be compiling the code each time for each board, won't you?
You can use compiler directives to change the offset you add to the pin number (Pin - 1000 + offset) depending on which board you're compiling on. You could do it by the chip on the board (#ifdefAVR_ATmega328P).
For analogRead you normally just pass the analog port number, not the Axx compiler macro.
For analogRead, you can use either.
Hypothetical question...
Suppose you craft a board with 8 digital and 16 analog pins.
Digital read can go from 0-23 (0-7 being digital, 8-23 being analog). Analog read can go from 0 to 15.
A0 equates to 8, A1 to 9, etc through to A15 being 23.
Which analog pin would you expect to read when doing analogRead(A3) ?
That is why you don't use Axx for analogRead.
Admittedly it's not a problem on the "common" boards, but there's nothing to stop a board like that being made by someone, so it's a good idea to not get into the habit of using Axx for analog reads.
pilzkopf:
ATM i'm Thinkin 'bout using the IF terms.
Seems a lot easier and its only a Debug Software so its not time critical.
i was hoping that A in general was defined as 14 (depending on Board) so i could just say
if > 1000 than -1000 + A.
Are the A pins "in line" on all boards? so 14 to xx without interrupts?
Than we could use
Port - 1000 + A0, so 1000 would be A0, 1001 would be A1 and so on.
seems workin on arduino uno, anyone could Say for other?
That should work, yes.
More specifically:
if (port >= 1000) {
port = port - 1000 + A0;
}
So, if A0 is 14, you enter 1000 you end up with 14. You enter 1001 you end up with 15, etc.
You could have some upper bounds checking with
if (port > A0 + NUM_ANALOG_PINS - 1) {
Serial.println("Unknown analog pin");
return;
}
NUM_ANALOG_PINS is defined in pins_arduino.h in the board variant. On the Uno it is defined as 6. So entering 1006 would give you 1006 - 1000 + 14 = 20. A0+NUM_ANALOG_PINS-1 = 14 + 6 - 1 = 19. 20 > 19, so fail.
The "digital" pin number is the only really unique pin identifier.
Drop the whole 1000+ thing, and if the user enters Axx then just add A0 to the xx part of the input value. That way it's always internally represented directly as the digital pin number.